单击按钮时从GridView获取价值 [英] Get value from GridView when a button is clicked
问题描述
我有一个GridView,并且GridView有一个项目模板.现在,我需要获取包含所需值的第一个单元格的值,但是我尝试了以下操作,但不起作用,它返回一个"
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);
这是我在gridview中拥有的代码
Here is the code I have in the gridview
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblDepartmentID" Text='<%#DataBinder.Eval(Container.DataItem,"DepartmentID")%>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
我必须强调,我不想使用GridView_RowCommand或其他GridView事件.我需要在单击同一页面上的按钮时拉出该值.
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.
我该怎么做?
推荐答案
如果您尝试访问 Label
的文本值,则必须首先获取对 Label 的引用code>这样的控件:
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");
,然后使用它的 Text
属性获取所需的值:
and then use it's Text
property to get the value required:
int id = Convert.ToInt32(lbl.Text);
这篇关于单击按钮时从GridView获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!