在Page_Load上禁用gridview内的复选框

时间:2016-03-05 06:12:30

标签: c# asp.net gridview

我想

  

禁用gridview内的gridview复选框。

我试过如下: -

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;
    }
}

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);
    }
}

但它不适合我。知道代码有什么问题

这是我的GridView html:

<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>

4 个答案:

答案 0 :(得分:1)

嗯,你没有说过你正在使用OBOUT ASP.NET Grid,而不是默认的网格控件。在这种情况下,它很重要。但是,我手动重新创建了您的示例,这就是我使其工作的方式。

首先,我将网格代码更改为使用服务器控件来复选框:

<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>

您不必担心重复的ID,因为ASP.NET会在客户端生成适当的ID。用户服务器控件的复选框是必要的,因为如果你使用通用的html标签,它将为每个复选框创建DataBoundLiteralCOntrol,并且不便于在代码后面更改它们的代码(它们被视为静态文本)。如果我们使用服务器控件,我们可以在后面的代码中访问它们的属性 - 在这种情况下,将为每个复选框创建HtmlInputCheckBox。

然后我迭代每一行并查找复选框控件:

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");
                }
            }
        }
    }

我只检查列中具有TemplateId定义的单元格,因为如果找不到任何控件,FindControlsByType将抛出错误。所以我假设每个模板列至少有一个复选框。如果您需要更灵活的方法,您可以定义类似于FindControlsByType的自己的函数 - StackOverflow上有这类函数的示例(例如Get all controls of a specific typeFind all child controls of specific type using Enumerable.OfType<T>() or LINQ)。找到正确的控件后,您可以添加自定义属性。

最后一步是连接到行被限制时调用的事件。为此,我使用了Page_Load事件:

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();
    }

}

我的测试班:

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; }
}

更新: 在与@coder进行对话之后,他告诉我复选框的ID需要像我更改服务器控件之前一样。为了做到这一点,我在下面添加了代码到GrdWorkingCompany_RowDataBound方法:

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

如果控件是客户端,那么复选框的ID将保持不变。

答案 1 :(得分:0)

代码未经过测试,但希望对您有所帮助。

您可以更改复选框并制作这些asp:CheckBox,以便您可以在后面的代码中禁用它,如下所示:

将OnRowDataBound事件处理到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" OnRowDataBound="GrdWorkingCompany_RowDataBound">

<Template>
    <asp:CheckBox ID="cbTest" runat="server" />
</Template>

在您的代码中,您应启用或禁用您的复选框,如下所示:

protected void GrdWorkingCompany_RowDataBound(object sender, GridViewRowEventArgs e)
{
    CheckBox chkbox = (CheckBox)e.Row.FindControl("cbTest");
    chekbox.Enable = false; 
}

方法2,你可以创建一个jquery函数,它将在你的代码后面调用,如下所示:

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
    function DisableAllCheckBox() {
        $('#<%=GrdWorkingCompany.ClientID %>').find("input:checkbox").each(function () {
            $(this).attr("disabled", true);
        });
    }
</script>

然后在你的代码中调用函数:

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);
        Page.ClientScript.RegisterStartupScript(this.GetType(),"TEST", "DisableAllCheckBox()",true);
    }
}

答案 2 :(得分:-1)

100%工作简单只需在页面加载事件之后绑定您的gride视图方法

protected void Page_LoadComplete(object sender, EventArgs e)
{
     DisableFormControls(Form.Controls);
}

从页面加载中删除此方法并使用上面的事件

答案 3 :(得分:-1)

很抱歉,如果有任何语法错误。

function DisableControls() {
        //Get target base & child control.
        var TargetBaseControl =
        document.getElementById('<%= this.grid.ClientID %>');
        var TargetChildControl = "chkA";

        //Get all the control of the type INPUT in the base control.
        var Inputs = TargetBaseControl.getElementsByTagName("input");

        //disable or enable all the checkBoxes in side the Grid.
        for (var n = 0; n < Inputs.length; ++n)
            if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)
                Inputs[n].disabled= false; //i am not sure if this is the correct syntax for disabling
    }