简体   繁体   English

从asp.net的GridView中获取选定的行

[英]Get selected row from the gridview in asp.net

I wan to get id from the gridview and using this code 我想从gridview获取ID并使用此代码

string UserID = ((Label)grdUser.SelectedRow.FindControl("UserID")).ToString(); 字符串UserID =((Label)grdUser.SelectedRow.FindControl(“ UserID”))。ToString(); Gridview code Gridview代码

<asp:TemplateField HeaderText="userID" Visible="False">
                                <ItemTemplate>
                                    <asp:Label ID="UserID" runat="server" Text='<%# Bind("iduser_Detail") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

Getting error object reference null 获取错误对象参考null

please help me 请帮我

First of all make sure you are writting this in SelectedRow event and then 首先,请确保您在SelectedRow事件中编写了此代码,然后

Most probably you are writting your in wrong event handler. 您很可能在编写错误的事件处理程序。

You seem to be calling it when selecting a row in the grid. 在网格中选择一行时,您似乎正在调用它。 Write this code in selectedrow event of grid. 在grid的selectedrow事件中编写此代码。

The way you will need to access the label is 您将需要访问标签的方式是

string UserID = ((Label)grdUser.SelectedRow.FindControl("UserID")). 字符串UserID =((Label)grdUser.SelectedRow.FindControl(“ UserID”))。 Text ; 文字 ;

You are missing Text property in the code above. 您在上面的代码中缺少Text属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM