I have a GridView
defined as follows:
<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>
When a user clicks the edit button, I need to get the value of the BirthDate
column. To attempt this, I have tried the following:
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = gvUsers.Rows[e.NewEditIndex];
DateTime birthDate = (DateTime)(row.Cells[1].Text);
// Does not work
}
I know it has something to do with the fact that the column is not visible. The column must be hidden. But I need to get that value— how can I do this?
#3 jQuery Code To Get hidden field value by type var getValue= $("input[type=hidden]"). val(); alert(getValue); Here in the above jquery code, we select input hidden filed by its type i.e hidden, and then by using jquery . val() we get the hidden field value.
In jQuery to set a hidden field value, we use . val() method. The jQuery . val() method is used to get or set the values of form elements such as input, select, textarea.
How do you toggle a hidden field's true/false value using jquery? $('#myDiv'). on('click'), (function() { var hiddenField = $('#myHiddenField'), val = hiddenField. val(); hiddenField.
The problem is that when the Visibility property of the BoundField
is set to false the column isn't rendered to the client. A work around would be to use a HiddenField
within a TemplateField
instead.
<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);
}
EDIT
The above method still renders the column in the table, so you end up with an empty column. It works but not the best solution, here's a way the hide the BirthDate field but still get its value in the RowEditing
event handler. Just keep in mind that the BirthDate is still rendered to the client, just not displayed.
<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);
}
To hide the content as well as the Header in the Grid use the below code:
and access the column as usual. GridView.SelectedRow.Cells[index].Text;
<style type="text/css">
.hide
{
display:none;
}
<asp:BoundField DataField="MailRoomID" HeaderText="Mailroom ID" ItemStyle-HorizontalAlign="Center" ShowHeader="false" ><ItemStyle CssClass="hide" /><HeaderStyle CssClass="hide"/></asp:BoundField>
I have done something like this (adapting to your example):
string[] keyList = new string[1];
keyList[0] = "BirthDate";
myGridView.DataKeyNames = keyList;
I then bound the GridView with a DataTable that has a column named "BirthDate". The data under this column would be stored under the DataKeyName specified above.
To get the desired value, I would do something like this:
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
DataKeyArray keyList = myGridView.DataKeys as DataKeyArray;
if (keyList != null)
DateTime birthDate = keyList[e.NewEditIndex];
}
I realize that this doesn't involve hidden fields, however.
Check out the following article that shows how to access GridView invisible columns:
http://www.highoncoding.com/Articles/178_Access_GridView_Invisible_Columns.aspx
The idea is to basically use a TemplateField column instead of a BoundColumn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With