GridView FindControl问题 [英] GridView FindControl problem

查看:90
本文介绍了GridView FindControl问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好Al1,

以下是我的Gridview的HTML代码

Hello Al1,

Below is my html code for Gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
           DataKeyNames="userId" DataSourceID="SqlDataSource1"
           EmptyDataText="There are no data records to display.">
           <Columns>
               <asp:BoundField DataField="userId" HeaderText="userId" ReadOnly="True"
                   SortExpression="userId" />
               <asp:BoundField DataField="userName" HeaderText="userName"
                   SortExpression="userName" />
               <asp:CheckBoxField DataField="isChecked" HeaderText="isChecked"
                   SortExpression="isChecked" />
           </Columns>
       </asp:GridView>



如何获取复选框值是否已选中
我知道如何使用Item模板执行此操作,但是如何从CheckBoxField中找到该控件.



How to get check box value its checked or not
I know how to do that using Item template but from CheckBoxField how can i find that control .

Thanks in Advance.

推荐答案

获得null的原因是(根据您的评论),您需要在网格视图中访问一行,然后使用找到控制权.但是您可能正在使用网格视图(或按钮)来执行此操作.我想至少是这样.您尚未发布所有代码.

查看以下教程-

http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-cs [
The reason your are getting a null is that (as per your comment), you need to access a row in the grid view and then use find control. But you are probably doing it using the grid view (or the button). I guess so at least. You haven''t posted all your code.

Check out the following tutorial -

http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-cs[^]

Especially this section -

Step 4: Adding Check All and Uncheck All Buttons

private void ToggleCheckState(bool checkState)
{
    // Iterate through the Products.Rows property
    foreach (GridViewRow row in Products.Rows)
    {
        // Access the CheckBox
        CheckBox cb = (CheckBox)row.FindControl("ProductSelector");
        if (cb != null)
            cb.Checked = checkState;
    }
}



本教程可能是您所有问题的关键!



This tutorial might be the key for all your questions!


这篇关于GridView FindControl问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆