复选框禁用内部GridView控件上的Page_Load [英] Disabling of checkBoxes inside gridview on Page_Load

查看:195
本文介绍了复选框禁用内部GridView控件上的Page_Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想


  

禁用GridView的复选框这里面的GridView。


我试着象下面这样: -

 公共静态无效DisableFormControls(ControlCollection中ChildCtrls)
{
    的foreach(在ChildCtrls控制Ctrl键)
    {
        如果(按Ctrl为Obout.Grid.Grid)
            ((Obout.Grid.Grid)Ctrl键).Enabled = FALSE;
        如果(按Ctrl为HtmlGenericControl)
            ((HtmlGenericControl)Ctrl键).Disabled = TRUE;
    }
}

的Page_Load

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(的Request.QueryString [用户id] = NULL&放大器;!&放大器;的Request.QueryString [用户id] =!)
    {
        Hid_userid.Value =的Request.QueryString [用户id];
        Hid_HR.Value =Y;
        FundiableEnable();        DisableFormControls(Form.Controls);
    }
}

但它不是为我工作。任何想法有什么不对的code

下面是我的GridView控件的HTML:

 < CC1:网格ID =GrdWorkingCompany=服务器EnableTypeValidation =真CallbackMode =真
    ShowFooter =假的AutoGenerateColumns =假AllowAddingRecords =真AllowSorting =假
    WIDTH =100%FolderStyle =〜/风格/表格/ style_12每页=18>
    <柱体和GT;
        < CC1:列ID =Sr_No只读=真的DataField =SrNo的HeaderText =Sr.No。 WIDTH =50像素>
        < / CC1:列>
        < CC1:列ID =点只读=真的DataField =点的HeaderText =点=服务器WIDTH =300像素>
        < / CC1:列>
        < CC1:列ID =chkPoor只读=真的DataField =额定值1的HeaderText =可怜WIDTH =110pxTemplateId =tpltPoor>
        < / CC1:列>
        < CC1:列ID =chkSatisfactory数据字段=Rating2的HeaderText =满意只读=真
            WIDTH =110pxTemplateId =tpltSatisfactory>
        < / CC1:列>
        < CC1:列ID =chkGood只读=真的HeaderText =好数据字段=Rating3WIDTH =110pxTemplateId =tpltGood>
        < / CC1:列>
        < CC1:列ID =chkExcellent的HeaderText =优秀的DataField =Rating4WIDTH =110px只读=真
            TemplateId =tpltEx1>
        < / CC1:列>
    < /专栏>
    < TemplateSettings GroupHeaderTemplateId =GroupTemplate/>
    <模板和GT;
        < CC1:GridTemplate =服务器ID =GridTemplate2>
            <模板>
                <%#Container.Column.HeaderText%GT;
                :其中,I>
                    &所述;%#Container.Value%GT;&下; / I GT; (小于%#Container.Group.PageRecordsCount%GT;<%#Container.Group.PageRecordsCount大于1记载:记录%G​​T;)
            < /模板>
        < / CC1:GridTemplate>
    < /模板>
    <模板和GT;
        &所述; CC1:GridTemplate ID =tpltPoor>
            <模板>
                <输入类型=复选框ID =chkA1<%#(Container.RecordIndex)%GT; NAME =chkAC1的风格=WIDTH:知识+;
                    高度:知识+;VALUE =<%#(Container.RecordIndex)%gt;中的onclick =AppoveCheckA(本)/>< /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltSatisfactory>
            <模板>
                <输入类型=复选框ID =chkA2<%#(Container.RecordIndex)%GT; NAME =chkAC2的风格=WIDTH:知识+;
                    高度:知识+;VALUE =<%#(Container.RecordIndex)%gt;中的onclick =AppoveCheckA(本)/>< /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltGood>
            <模板>
                <输入类型=复选框ID =chkA3<%#(Container.RecordIndex)%GT; NAME =chkAC3的风格=WIDTH:知识+;
                    高度:知识+;VALUE =<%#(Container.RecordIndex)%gt;中的onclick =AppoveCheckA(本)/>< /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltEx1>
            <模板>
                <输入类型=复选框ID =chkA4<%#(Container.RecordIndex)%GT; NAME =chkAC4的风格=WIDTH:知识+;
                    高度:知识+;VALUE =<%#(Container.RecordIndex)%gt;中的onclick =AppoveCheckA(本)/>< /模板>
        < / CC1:GridTemplate>
    < /模板>
< / CC1:网格>


解决方案

好吧,你不说你正在使用ASP.NET OBOUT电网字,而不是默认的网格控件。在这种情况下,它是很重要的。不过,我手动重新创建你的例子,这是我做它的工作。

首先,我改变了你的网格code使用服务器控件的复选框:

 < CC1:网格ID =GrdWorkingCompany=服务器EnableTypeValidation =真CallbackMode =真正的序列化=真
    ShowFooter =假的AutoGenerateColumns =假AllowAddingRecords =真AllowSorting =假
    WIDTH =100%FolderStyle =〜/风格/表格/ style_12每页=18>
    <柱体和GT;
        < CC1:列ID =Sr_No只读=真的DataField =SrNo的HeaderText =Sr.No。 WIDTH =50像素>
        < / CC1:列>
        < CC1:列ID =点只读=真的DataField =点的HeaderText =点=服务器WIDTH =300像素>
        < / CC1:列>
        < CC1:列ID =chkPoor只读=真的DataField =额定值1的HeaderText =可怜WIDTH =110pxTemplateId =tpltPoor>
        < / CC1:列>
        < CC1:列ID =chkSatisfactory数据字段=Rating2的HeaderText =满意只读=真
            WIDTH =110pxTemplateId =tpltSatisfactory>
        < / CC1:列>
        < CC1:列ID =chkGood只读=真的HeaderText =好数据字段=Rating3WIDTH =110pxTemplateId =tpltGood>
        < / CC1:列>
        < CC1:列ID =chkExcellent的HeaderText =优秀的DataField =Rating4WIDTH =110px只读=真
            TemplateId =tpltEx1>
        < / CC1:列>
    < /专栏>
    < TemplateSettings GroupHeaderTemplateId =GroupTemplate/>
    <模板和GT;
        < CC1:GridTemplate =服务器ID =GridTemplate2>
            <模板>
                <%#Container.Column.HeaderText%GT;
            :其中,I>
                &所述;%#Container.Value%GT;&下; / I GT; (小于%#Container.Group.PageRecordsCount%GT;<%#Container.Group.PageRecordsCount大于1记载:记录%G​​T;)
            < /模板>
        < / CC1:GridTemplate>
    < /模板>
    <模板和GT;
        &所述; CC1:GridTemplate ID =tpltPoor>
            <模板>
                <输入类型=复选框ID =chkA1NAME =chkAC1的风格=WIDTH:知识+;高度:知识+;
                    值=<%#(Container.RecordIndex)%GT;的onclick =AppoveCheckA(本)=服务器/>
            < /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltSatisfactory>
            <模板>
                <输入类型=复选框ID =chkA2NAME =chkAC2的风格=WIDTH:知识+;高度:知识+;
                    值=<%#(Container.RecordIndex)%GT;的onclick =AppoveCheckA(本)=服务器/>
            < /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltGood>
            <模板>
                <输入类型=复选框ID =chkA3NAME =chkAC3的风格=WIDTH:知识+;高度:知识+;
                    值=<%#(Container.RecordIndex)%GT;的onclick =AppoveCheckA(本)=服务器/>
            < /模板>
        < / CC1:GridTemplate>
        &所述; CC1:GridTemplate ID =tpltEx1>
            <模板>
                <输入类型=复选框ID =chkA4NAME =chkAC4的风格=WIDTH:知识+;高度:知识+;
                    值=<%#(Container.RecordIndex)%GT;的onclick =AppoveCheckA(本)=服务器/>
            < /模板>
        < / CC1:GridTemplate>
    < /模板>
< / CC1:网格>

您不必担心重复的ID,因为ASP.NET会在客户端生成适当的标识。因此有必要为用户的复选框服务器控件,因为如果你用一般的HTML标记它会为每个复选框创建DataBoundLiteralCOntrol及其不方便改变自己的code在code的后面(它们被视为静态文本)。如果我们使用服务器控件,我们可以在code访问它们的属性后面 - 在这种情况下HtmlInputCheckBox每个复选框将创建

然后我遍历每一行,寻找CheckBox控件:

 私人无效GrdWorkingCompany_RowDataBound(对象发件人,Obout.Grid.GridRowEventArgs E)
    {
        的foreach(在GrdWorkingCompany.Columns Obout.Grid.Column列)
        {
            如果(!string.IsNullOrEmpty(column.TemplateId))
            {
                VAR电池= e.Row.Cells [column.Index]作为Obout.Grid.GridDataControlFieldCell;
                VAR checkBoxControls = cell.FindControlsByType(typeof运算(HtmlInputCheckBox));
                如果(checkBoxControls.Count大于0)
                {
                    变种复选框= checkBoxControls [0]作为HtmlInputCheckBox;
                    checkbox.ClientIDMode = ClientIDMode.Static;
                    checkbox.ID = checkbox.ID + e.Row.RowIndex;
                    checkbox.Attributes.Add(禁用,真);
                }
            }
        }
    }

我只检查电池列已TemplateId定义,因为FindControlsByType将抛出错误,如果它没有找到任何控制。所以,我认为每一个模板列至少有一个复选框。如果你需要更灵活的方式,你可以定义类似FindControlsByType自己的函数 - 有StackOverflow上的那种功能的例子(如的获取特定类型Find使用Enumerable.OfType&LT特定类型的所有子控件; T>()或LINQ )。你发现后,您可以添加自定义属性的权限控制。

的最后一个步骤是箍最多当行被界称为事件。要做到这一点我用Page_Load事件:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    GrdWorkingCompany.RowDataBound + = GrdWorkingCompany_RowDataBound;    如果(!的IsPostBack)
    {
        GrdWorkingCompany.DataSource =新的List<试验>(){新的测试(),新的测试()};
        GrdWorkingCompany.DataBind();
    }}

我的测试类:

 公共类测试
{
    公共字符串SrNo {搞定;组; }
    公共字符串点{搞定;组; }
    公共字符串额定值1 {搞定;组; }
    公共字符串Rating2 {搞定;组; }
    公共字符串Rating3 {搞定;组; }
    公共字符串Rating4 {搞定;组; }
}

更新:
与@ codeR谈话后,他告诉我,复选框的IDS必须exacly我的改变服务器控件之前。为了做到下面添加code此我要GrdWorkingCompany_RowDataBound方式:

  checkbox.ClientIDMode = ClientIDMode.Static;
checkbox.ID = checkbox.ID + e.Row.RowIndex;

有了这个复选框的地方IDS将保持不变,如果控制得客户端。

I want to

disable the gridview checkboxes which is inside gridview.

I tried like below:-

public static void DisableFormControls(ControlCollection ChildCtrls)
{
    foreach (Control Ctrl in ChildCtrls)
    {
        if (Ctrl is Obout.Grid.Grid)
            ((Obout.Grid.Grid)Ctrl).Enabled = false;
        if (Ctrl is HtmlGenericControl)
            ((HtmlGenericControl)Ctrl).Disabled = true;
    }
}

and on Page_load

protected void Page_Load(object sender, EventArgs e)
{ 
    if (Request.QueryString["userid"] != null && Request.QueryString["userid"] != "")
    {
        Hid_userid.Value = Request.QueryString["userid"];
        Hid_HR.Value = "Y";
        FundiableEnable();

        DisableFormControls(Form.Controls);
    }
}

But it is not working for me. Any idea what's wrong with the code

Here is my html of GridView:

<cc1:Grid ID="GrdWorkingCompany" runat="server"  EnableTypeValidation="true" CallbackMode="true"
    ShowFooter="false" AutoGenerateColumns="false" AllowAddingRecords="true" AllowSorting="false"
    Width="100%" FolderStyle="~/Styles/Grid/style_12" PageSize="18">
    <Columns>
        <cc1:Column ID="Sr_No" ReadOnly="true" DataField="SrNo" HeaderText="Sr.No." Width="50px">
        </cc1:Column>
        <cc1:Column ID="Points" ReadOnly="true" DataField="Points" HeaderText="Points" runat="server" Width="300px">
        </cc1:Column>
        <cc1:Column ID="chkPoor" ReadOnly="true" DataField="Rating1" HeaderText="Poor" Width="110px" TemplateId="tpltPoor">
        </cc1:Column> 
        <cc1:Column ID="chkSatisfactory" DataField="Rating2" HeaderText="Satisfactory" ReadOnly="true"
            Width="110px" TemplateId="tpltSatisfactory">
        </cc1:Column>
        <cc1:Column ID="chkGood" ReadOnly="true" HeaderText="Good" DataField="Rating3" Width="110px" TemplateId="tpltGood">
        </cc1:Column>
        <cc1:Column ID="chkExcellent" HeaderText="Excellent" DataField="Rating4" Width="110px" ReadOnly="true"
            TemplateId="tpltEx1">
        </cc1:Column>
    </Columns>
    <TemplateSettings GroupHeaderTemplateId="GroupTemplate" />
    <Templates>
        <cc1:GridTemplate runat="server" ID="GridTemplate2">
            <Template>
                <%# Container.Column.HeaderText %>
                : <i>
                    <%# Container.Value %></i> (<%# Container.Group.PageRecordsCount %><%# Container.Group.PageRecordsCount > 1 ? "records" : "record" %>)
            </Template>
        </cc1:GridTemplate>
    </Templates>
    <Templates>
        <cc1:GridTemplate ID="tpltPoor">
            <Template>
                <input type="checkbox" id="chkA1<%# (Container.RecordIndex)%>" name="chkAC1" style="width: 17px; 
                    height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" /></Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltSatisfactory">
            <Template>
                <input type="checkbox" id="chkA2<%# (Container.RecordIndex) %>" name="chkAC2" style="width: 17px;
                    height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" /></Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltGood">
            <Template>
                <input type="checkbox" id="chkA3<%# (Container.RecordIndex) %>" name="chkAC3" style="width: 17px;
                    height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" /></Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltEx1">
            <Template>
                <input type="checkbox" id="chkA4<%# (Container.RecordIndex) %>" name="chkAC4" style="width: 17px;
                    height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" /></Template>
        </cc1:GridTemplate>
    </Templates>
</cc1:Grid>

解决方案

Well, you didn't say a word that you are using OBOUT ASP.NET Grid and not a default grid control. In this case it's important. However I manually recreated your example and this is how I made it work.

Firstly I changed your Grid code to use server controls for checkboxes:

<cc1:Grid ID="GrdWorkingCompany" runat="server" EnableTypeValidation="true" CallbackMode="true" Serialize="true"
    ShowFooter="false" AutoGenerateColumns="false" AllowAddingRecords="true" AllowSorting="false"
    Width="100%" FolderStyle="~/Styles/Grid/style_12" PageSize="18">
    <Columns>
        <cc1:Column ID="Sr_No" ReadOnly="true" DataField="SrNo" HeaderText="Sr.No." Width="50px">
        </cc1:Column>
        <cc1:Column ID="Points" ReadOnly="true" DataField="Points" HeaderText="Points" runat="server" Width="300px">
        </cc1:Column>
        <cc1:Column ID="chkPoor" ReadOnly="true" DataField="Rating1" HeaderText="Poor" Width="110px" TemplateId="tpltPoor">
        </cc1:Column>
        <cc1:Column ID="chkSatisfactory" DataField="Rating2" HeaderText="Satisfactory" ReadOnly="true"
            Width="110px" TemplateId="tpltSatisfactory">
        </cc1:Column>
        <cc1:Column ID="chkGood" ReadOnly="true" HeaderText="Good" DataField="Rating3" Width="110px" TemplateId="tpltGood">
        </cc1:Column>
        <cc1:Column ID="chkExcellent" HeaderText="Excellent" DataField="Rating4" Width="110px" ReadOnly="true"
            TemplateId="tpltEx1">
        </cc1:Column>
    </Columns>
    <TemplateSettings GroupHeaderTemplateId="GroupTemplate" />
    <Templates>
        <cc1:GridTemplate runat="server" ID="GridTemplate2">
            <Template>
                <%# Container.Column.HeaderText %>
            : <i>
                <%# Container.Value %></i> (<%# Container.Group.PageRecordsCount %><%# Container.Group.PageRecordsCount > 1 ? "records" : "record" %>)
            </Template>
        </cc1:GridTemplate>
    </Templates>
    <Templates>
        <cc1:GridTemplate ID="tpltPoor">
            <Template>
                <input type="checkbox" id="chkA1" name="chkAC1" style="width: 17px; height: 17px;"
                    value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" runat="server"/>
            </Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltSatisfactory">
            <Template>
                <input type="checkbox" id="chkA2" name="chkAC2" style="width: 17px; height: 17px;"
                    value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" runat="server"/>
            </Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltGood">
            <Template>
                <input type="checkbox" id="chkA3" name="chkAC3" style="width: 17px; height: 17px;"
                    value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" runat="server"/>
            </Template>
        </cc1:GridTemplate>
        <cc1:GridTemplate ID="tpltEx1">
            <Template>
                <input type="checkbox" id="chkA4" name="chkAC4" style="width: 17px; height: 17px;"
                    value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" runat="server"/>
            </Template>
        </cc1:GridTemplate>
    </Templates>
</cc1:Grid>

You don't need to worry about duplicate Id because ASP.NET will generate appropriate Ids on client side. It's necessary to user server controls for checkboxes because if you use generic html tags it will create DataBoundLiteralCOntrol for each checkbox and its inconvenient to change their code in code behind (they are treated as static text). If we use server control we can access their attributes in code behind - in that case HtmlInputCheckBox will be created for each checkbox.

Then I iterate each row and look for checkbox control:

private void GrdWorkingCompany_RowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
    {
        foreach (Obout.Grid.Column column in GrdWorkingCompany.Columns)
        {
            if (!string.IsNullOrEmpty(column.TemplateId))
            {
                var cell = e.Row.Cells[column.Index] as Obout.Grid.GridDataControlFieldCell;
                var checkBoxControls = cell.FindControlsByType(typeof(HtmlInputCheckBox));
                if (checkBoxControls.Count > 0)
                {
                    var checkbox = checkBoxControls[0] as HtmlInputCheckBox;
                    checkbox.ClientIDMode = ClientIDMode.Static;
                    checkbox.ID = checkbox.ID + e.Row.RowIndex;
                    checkbox.Attributes.Add("disabled", "true");
                }
            }
        }
    }

I only check for cells in column that have TemplateId defined because FindControlsByType will throw error if it not find any control. So I assume that every template column have at least one checkbox. If you need more flexible approach you could define your own function similar to FindControlsByType - there are examples on StackOverflow for that kind of functions (e.g. Get all controls of a specific type or Find all child controls of specific type using Enumerable.OfType<T>() or LINQ). After you find the right control you can add custom attributes.

The last step is to hoop up to the event called when row gets bounded. To do that I used Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
    GrdWorkingCompany.RowDataBound += GrdWorkingCompany_RowDataBound;

    if (!IsPostBack)
    {
        GrdWorkingCompany.DataSource = new List<Test>() { new Test(), new Test() };
        GrdWorkingCompany.DataBind();
    }

}

My test class:

public class Test
{
    public string SrNo { get; set; }
    public string Points { get; set; }
    public string Rating1 { get; set; }
    public string Rating2 { get; set; }
    public string Rating3 { get; set; }
    public string Rating4 { get; set; }
}

Update: After a conversation with the @coder he told me that ids of the checkboxes need to be exacly as before my change to server controls. In order to do this I below added code to GrdWorkingCompany_RowDataBound method:

checkbox.ClientIDMode = ClientIDMode.Static;
checkbox.ID = checkbox.ID + e.Row.RowIndex;

With this in place ids of the checkboxes will remain the same as if the control was client side.

这篇关于复选框禁用内部GridView控件上的Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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