gpt4 book ai didi

asp.net - 获取隐藏字段值

转载 作者:行者123 更新时间:2023-12-01 22:26:51 24 4
gpt4 key购买 nike

我有一个 GridView 定义如下:

<asp:GridView ID="myGridView" AutoGenerateColumns="false" runat="server"
OnLoad="myGridView_Load" OnRowCommand="myGridView_Command" OnRowEditing="myGridView_RowEditing" OnRowDeleting="myGridView_RowDeleting" DataKeyNames="ID" >
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:BoundField DataField="BirthDate" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Other">
<ItemTemplate>
<asp:LinkButton ID="editLB" runat="server" Text="edit" CommandName="Edit" />
<asp:LinkButton ID="deleteLB" runat="server" Text="delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

当用户单击编辑按钮时,我需要获取 BirthDate 列的值。为了尝试这个,我尝试了以下方法:

protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = gvUsers.Rows[e.NewEditIndex];
DateTime birthDate = (DateTime)(row.Cells[1].Text);

// Does not work
}

我知道这与该列不可见有关。该列必须隐藏。但我需要获得该值 - 我该如何做到这一点?

最佳答案

问题在于,当 BoundField 的 Visibility 属性设置为 false 时,该列不会呈现给客户端。解决方法是在 TemplateField 中使用 HiddenField

<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("BirthDate") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("BirthDate") %>' />
</EditItemTemplate>
</asp:TemplateField>
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView.Rows[e.NewEditIndex];
HiddenField hidden = (HiddenField)row.Cells[0].FindControl("HiddenField1");
DateTime birthDate = Convert.ToDateTime(hidden.Value);
}

编辑

上述方法仍然会呈现表中的列,因此最终会得到一个空列。它有效,但不是最好的解决方案,这是隐藏 BirthDate 字段但仍然在 RowEditing 事件处理程序中获取其值的方法。请记住,生日仍然呈现给客户端,只是不显示。

<style type="text/css">
.hide
{
display:none;
}
</style>

<asp:BoundField DataField="BirthDate">
<ItemStyle CssClass="hide"/>
</asp:BoundField>
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
DateTime birthDate = Convert.ToDateTime(row.Cells[1].Text);
}

关于asp.net - 获取隐藏字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1833532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com