Get value from GridView when a button is clicked
I have a GridView , and the GridView has an item template. Now I need to get the value of the first cell which contains the value I need but I have tried the following and does not work, it eturns a ""
int id = Convert.ToInt32(GridView.Rows[index].Cells[0].Text);
Here is the code I have in the gridview
<Columns>
<asp:TemplateField HeaderText="ID"开发者_如何学JAVA>
<ItemTemplate>
<asp:Label runat="server" ID="lblDepartmentID" Text='<%#DataBinder.Eval(Container.DataItem,"DepartmentID")%>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
I have to emphazise that I do not want to use the GridView_RowCommand or other GridView events.. I need to pull this value upon clicking a button on the same page.
How can I do this?
If you are trying to access the text value of Label
then you have to first get the reference to Label
control like this:
Label lbl = (Label)GridView.Rows[index].FindControl("lblDepartmentID");
and then use it's Text
property to get the value required:
int id = Convert.ToInt32(lbl.Text);
精彩评论