Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
875 views
in Technique[技术] by (71.8m points)

asp.net - 如何在asp.net 4.0后面的代码中获取html chekbox(how to get html chekbox at code behind with asp.net 4.0)

here this is my html markup :

(这是我的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>

and here is my code :

(这是我的代码:)

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

here i achieve get all checkbox selected from html checkbox checked changed event..

(在这里,我实现了从html复选框选中已更改事件中获取所有复选框。)

how i done this task ...please guys help me...

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

  ask by shalin gajjar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your code behind you use CheckBox , but in the aspx you are not using the corresponding control ( <asp:CheckBox ) but a normal html checkbox with runat=server.

(在后面的代码中,您使用CheckBox ,但在aspx中,您没有使用相应的控件( <asp:CheckBox ),而是使用带有runat = server的普通html复选框。)

So you need to use HtmlInputCheckBox

(所以你需要使用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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...