【问题标题】:how to get html chekbox at code behind with asp.net 4.0如何使用 asp.net 4.0 在代码后面获取 html 复选框
【发布时间】:2019-12-01 21:28:48
【问题描述】:

这是我的 html 标记:

<asp:TemplateField>
    <HeaderTemplate>
        <div class="form-check">
            <input type="checkbox" id="chkAll" runat="server" class="form-check-input checkAll" onclick="javascript: form1.submit();" onserverchange="Server_Changed" />
            <label class="form-check-label">Roll No</label>
        </div>
    </HeaderTemplate>
    <ItemTemplate>
        <div class="form-check">
            <input type="checkbox" id="chkSelect" runat="server" class="form-check-input" />
            <label class="form-check-label">
                <asp:LinkButton runat="server" CausesValidation="false" ID="lnkID" CommandName="detail"
                    CommandArgument='<%#Bind("ID") %>' ToolTip="View Detail"
                    Text='<%# string.Concat("#",Eval("RollNo"))%>'
                    Font-Underline="true">
                </asp:LinkButton>
            </label>
    </ItemTemplate>
</asp:TemplateField>

这是我的代码:

protected void Server_Changed(object sender, EventArgs e)
{
    CheckBox chkAll = sender as CheckBox;
    if (chkAll.Checked)
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = true;
        }
    }
    else
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = false;
        }
    }
}

在这里,我实现了从 html 复选框选中更改事件中选择的所有复选框..

我是如何完成这项任务的...请大家帮帮我...

【问题讨论】:

    标签: asp.net checkbox c#-4.0


    【解决方案1】:

    在您后面的代码中使用CheckBox,但在aspx 中您没有使用相应的控件(&lt;asp:CheckBox),而是使用带有runat=server 的普通html 复选框。 所以你需要使用HtmlInputCheckBox

    using System.Web.UI.HtmlControls;
    
    protected void Server_Changed(object sender, EventArgs e)
    {
        HtmlInputCheckBox chkAll = sender as HtmlInputCheckBox;
        if (chkAll.Checked)
        {
            for (int i = 0; i <= egrd.Rows.Count - 1; i++)
            {
                GridViewRow row = egrd.Rows[i];
                HtmlInputCheckBox Ckbox = (HtmlInputCheckBox)row.FindControl("chkSelect");
                Ckbox.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i <= egrd.Rows.Count - 1; i++)
            {
                GridViewRow row = egrd.Rows[i];
                HtmlInputCheckBox Ckbox = (HtmlInputCheckBox)row.FindControl("chkSelect");
                Ckbox.Checked = false;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2017-03-05
      • 2023-01-14
      相关资源
      最近更新 更多