场景:
我的.aspx页面上有一个名为gvpat的GridView。此GridView有五列。
G213
除Hour之外的每一列都有一个LinkButton。我想要的是当单击LinkButton时,它将查看小时列以获取小时数,然后查看LinkButton所在的列以获取分钟数。并将时间放在.aspx页面上另一个名为txtCustTime的TextBox中。例如,如果单击包含小时1 1PM的行上的按钮,并且该按钮所在的列是15分钟,则在txtCustTime中输入的值将等于13:15
以下是针对AM/PM的GridView的可视化视图
这是AM
这是PM
现在,更改为PM和更改为AM按钮只需在按下时重新绑定网格(我编写了一个将网格绑定到数据的函数),它正在工作,而不是这个问题的一部分
方案结束
我的问题:当一个LinkButton被按下时,我调用一个如下的函数:例如,假设用户按下了45 Min列中的一个LinkButton,假设他们按下了12AM或12 at行上的按钮,这取决于他们一天中的什么时间。
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“视图时按LinkButton时,txtCustTime被正确设置为00:45:00
但是,如果用户正在查看"PM“视图并按下LinkButton,则txtCustTime会错误地设置为空白。应该是12:45:00
注释:lblhr -是在Hour列下的GridView中保存值的控件的名称
发布于 2018-08-07 03:45:23
你可能想得太多了。您所需要做的就是绑定一系列正确的时隙。因此,如果您有以下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();
现在,包含DateTime对象的GridView有12行。现在剩下的唯一要做的就是用时间块的CommandArgument
来处理LinkButton的点击。在后面的代码中,只需将CommandArgument转换为DateTime对象,就完成了。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DateTime time = Convert.ToDateTime(e.CommandArgument);
Label1.Text = time.ToString("HH:mm");
}
https://stackoverflow.com/questions/51708840
复制相似问题