如何在 Gridview 中使用单个 LinkBut​​ton 来更新 aspx 页面上其他地方的 TextBox

How to use single LinkButton in a Gridview to update TextBox elsewhere on aspx page

场景:
我的 .aspx 页面上有一个名为 gvpat 的 GridView。此 GridView 有五列。

  1. 小时
  2. 00 分钟
  3. 15 分钟
  4. 30 分钟
  5. 45 分钟

除了 小时 之外的每一列都有一个 LinkBut​​ton。 我想要的是当单击 LinkBut​​ton 时,它将查看 Hour 列以获取小时,然后查看 LinkBut​​ton 所在的列以获取分钟。并将时间放在 .aspx 页面其他位置的名为 txtCustTime 的文本框中。 例如,如果在包含 Hour 1PM 的行上单击了一个按钮,并且该按钮所在的列是 15 Min,那么在 txtCustTime 中输入的值将等于 13:15

以下是 AM/PM

的 GridView 视觉效果 这是上午

这是下午

现在 更改为 PM更改为 AM 按钮只需在按下时重新绑定网格(我写了一个将网格绑定到数据的函数) 正在工作而不是这个问题的一部分

场景结束

我的问题: 当按下 LinkBut​​ton 时,我调用这样的函数:例如,假设用户在 45 Min 列,假设他们在上午 12 点或下午 12 点按下行上的按钮,具体取决于他们查看的是一天中的什么时间。

protected void gvpat_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    if (e.CommandName == "GetTime45")
    {
        //Get rowindex
        int rowindex = Convert.ToInt32(e.CommandArgument);
        //Get Row
        GridViewRow gvr = gvpat.Rows[rowindex];
        //Sets the label value for the linked button that was clicked
        LinkButton lb = e.CommandSource as LinkButton;
        if (gvpat.HeaderRow.Cells[4].Text == "45 Min")
        {
            Label myLabel = (Label)gvr.FindControl("lblhr");
            if (myLabel.Text == "12AM") { txtCustTime.Text = "00:45:00"; }
            if (myLabel.Text == "12PM") { txtCustTime.Text = "12:45:00"; }
        }
    }
}

我的问题是在查看 "AM" 视图时按下 LinkBut​​ton, txtCustTime 正确设置为 00:45:00

但是如果用户正在查看 "PM" 视图并按下 LinkBut​​ton, txtCustTime 被错误地设置为空白。什么时候应该是 12:45:00

注意事项: lblhr - 是在 Hour

下的 GridView 中保存值的控件的名称

你可能想多了。您需要做的就是绑定一系列正确的时间段。因此,如果您有以下 GridView:

<asp:GridView ID="GridView1" runat="server" ItemType="System.DateTime" 
  OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Hour">
            <ItemTemplate>
                <%# Item.ToLongTimeString() %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="00 Min">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Item %>'>O</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="15 Min">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Item.AddMinutes(15) %>'>O</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="30 Min">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%# Item.AddMinutes(30) %>'>O</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="45 Min">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton4" runat="server" CommandArgument='<%# Item.AddMinutes(45) %>'>O</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

注意 ItemType="System.DateTime" 的用法。这将确保我们可以在整个 GridView 和隐藏代码中使用 DateTime 对象。

现在将一些数据绑定到 GridView。

GridView1.DataSource = Enumerable.Range(00, 12).Select(i => new DateTime(2000, 1, 1, i, 0, 0));
GridView1.DataBind();

现在 GridView 有 12 行带有 DateTime 对象。现在剩下要做的唯一一件事就是处理带有 CommandArgument 时间块的 LinkBut​​ton 的点击。只需在后面的代码中将 CommandArgument 转换为 DateTime 对象即可。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    DateTime time = Convert.ToDateTime(e.CommandArgument);
    Label1.Text = time.ToString("HH:mm");
}