【问题标题】:How to use single LinkButton in a Gridview to update TextBox elsewhere on aspx page如何在 Gridview 中使用单个 LinkBut​​ton 来更新 aspx 页面上其他地方的 TextBox
【发布时间】:2018-08-06 13:30:46
【问题描述】:

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

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

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

这里是上午/下午的 GridView 的视觉效果

这是上午

这里是下午

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

场景结束

我的问题: 当一个 LinkBut​​ton 被按下时,我会调用一个这样的函数:例如,假设用户在 45 Min 列中按下了一个 LinkBut​​ton 并假设他们根据他们正在查看的时间,按下第 12AM 或 12PM 行上的按钮。

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”视图并按下链接按钮, txtCustTime 被错误地设置为空白。什么时候应该是 12:45:00

注意事项: lblhr - 是控件的名称,该控件在 Hour 列下的 GridView 中保存值

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你可能想多了。您需要做的就是绑定一系列正确的时隙。所以如果你有以下 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 处理链接按钮的点击时间块。只需在后面的代码中将 CommandArgument 转换为 DateTime 对象即可。

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

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多