Gridview gets the BoundField value set to Visible = False

How can I get the value of a field that is set to Visible = false? in the following way:

<asp:BoundField DataField="ItemID" HeaderText="Line Item ID" Visible="false"/> 

but when I try to get it using

  int temID = Convert.ToInt32(row.Cells[0].Text); 

He cannot find it and throws an exception, but if I make it Visible = "true", it will work.

How to get value if visible = false?

+6
source share
5 answers

In your GridView definition add

 <asp:GridView .... DataKeyNames="ItemID" ...> 

You also need to use OnRowDataBound, not OnDataBound

 <asp:GridView .... DataKeyNames="ItemID" ... OnRowDataBound="GridView_RowDataBound"> 

Then in your code, something like this

 protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { int ItemId = Int32.Parse(YourGridView.DataKeys[e.Row.RowIndex].Values[0].ToString()); } } 

I have not tested this code before posting. But this is a general idea of ​​what you need to do. It may or may not work as it is.

+9
source

Another way without all this code is something like this:

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

on the page or in your css file. And set this class to your boundField:

 <asp:BoundField DataField="Id" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide" /> 

I hope for this help.

+8
source

I struggled a bit with this, but in the end (instead of adding RowDataBound events) I just got the values ​​from DataKeys when I needed it, for example (in VB):

 x = MyGrid.DataKeys(oRow.RowIndex).Values(0).ToString) 

Obviously, this is suitable when values ​​are data keys ... when not this is probably not a good idea.

0
source

try adding attribute

 runat="Server" 
-1
source

If you have IsVisible=false , the control is not displayed to the client at all, and therefore, you cannot return it back. You can use the display:none style to not display it on the client, but still display.

 <asp:BoundField DataField="ItemID" HeaderText="Line Item ID" style="display:none;"/> 

In this case, you can return the value.

An alternative to this would be to use the <input type="hidden" runat="server"> control.

-1
source

Source: https://habr.com/ru/post/894311/


All Articles