如何在代码隐藏的GridView访问选定绑定列值 [英] How to access a selected Boundfield value from a GridView in code-behind
问题描述
我也有类似的问题,但没有一个答案让我锻炼了这个问题。
我的GridView一个只读域如下
I have seen similar questions but none of the answers helped me to workout this problem. I have GridView with a ReadOnly field as follow.
GridView控件:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="projectID"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
PageSize="5" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"/>
<asp:BoundField DataField="prID" HeaderText="prID" SortExpression="prID"/>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
<EditRowStyle CssClass="GridViewEditRow"/>
</asp:GridView>
正如你所看到的 PRID
绑定列有只读= TRUE
属性。
我试图让 PRID
中的代码隐藏,当用户更新该行中的其他领域。
as you can see the prID
BoundField has Readonly=True
attribute.
I'm trying to get the value of the prID
in code-behind when user is updating the other fields in the row.
后台代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
String d1 = ((TextBox)(row.Cells[2].Controls[0])).Text;
String d2 = ((TextBox)(row.Cells[3].Controls[0])).Text;
// this only works while the field is not readonly
string prIDUpdate = ((TextBox)(row.Cells[1].Controls[0])).Text;
}
注意:我曾尝试使用 GridView1.DataKeys [e.RowIndex]
也 onRowDataBound
的d设定绑定列俱备只在隐藏代码,但我一直没能取得成果。
Note: I have tried using GridView1.DataKeys[e.RowIndex]
and also onRowDataBound
an d setting the BoundField ready only in code-behind but I haven't been able to get results
在此先感谢!
推荐答案
我看到你的DataKeyNames设置中的 GridView控件是这样的。
I saw that your DataKeyNames setting in GridView control is like this
DataKeyNames="projectID"
然后我猜你的键名就是专案编号不是 PRID ,不是吗?
如果是这样,你可以得到的数据所选行作为这一行:
Then I guess that your key name is projectID not prID, isn't it? If so, you could get data for the selected row as this line:
string id = GridView1.DataKeys[e.RowIndex]["projectID"].ToString();
和你也应该添加此列:
<asp:BoundField DataField="projectID" HeaderText="prID" SortExpression="projectID"/>
你尝试了吗?
Did you try that?
在另一种方法,你可以尝试使用模板列,而不是
In other way, you could try to use TemplateField instead
<Columns>
<asp:TemplateField HeaderText="prID" SortExpression="prID">
<ItemTemplate>
<asp:Label ID="lblPrId" runat="server" Text='<%# Bind("prID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
和此代码以获得GridView1_RowUpdating事件处理程序从PRID列数据:
And this code to get data from prID column in GridView1_RowUpdating event handler:
Label lblPrId = row.FindControl("lblPrId") as Label;
string prId = lblPrId .Text;
很抱歉,如果这没有帮助。
Sorry if this doesn't help.
这篇关于如何在代码隐藏的GridView访问选定绑定列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!