单击提交按钮时禁用选中的复选框中的一个中继器 [英] Disabling Checked checkboxes in a Repeater when clicking a Submit button

查看:122
本文介绍了单击提交按钮时禁用选中的复选框中的一个中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用选中的复选框,当用户点击在ASP.NET提交按钮。我可以禁用使用JavaScript使用按钮的onclick事件的复选框。该复选框是在一个中继器。当表单通过点击提交按钮提交,没有一个复选框中的code-背后的按钮单击code显示为选中。如何禁用选中的复选框,当按钮被点击提交,仍然可以看到该页面提交时的复选框被选中?有一个变通?我的preference会做到这一点在JavaScript中,但我愿意使用jQuery为好。

 函数disableCheckboxes(){
    对于(VAR计数= 0; I< document.forms [0] .elements.length;我++)
    {
        如果(document.forms [0] .elements [统计] .TYPE ==复选框)
        {
            document.forms [0] .elements [计数] .disabled = TRUE;
        }
    }
}
 <表ID =form1的=服务器>
< D​​IV>
    <表>        < ASP:直放站ID =rptCustomer=服务器OnItemDataBound =DisplayCustomerRepeater_ItemDataBound>
            <&ItemTemplate中GT;
                &所述; TR>
                    &所述; TD>
                        < ASP:复选框ID =checkSubscription=服务器/>
                    < / TD>
                    &所述; TD>
                        &所述;%#((客户)的Container.DataItem).FirstName ++((客户)的Container.DataItem).LastName%GT;
                    < / TD>
                < / TR>
                &所述; TR>
                    &所述; TD>
                        <小时/>
                    < / TD>
                < / TR>
            < / ItemTemplate中>
        < / ASP:直放站>
    < /表>
< / DIV>
< D​​IV>
    < ASP:按钮的ID =btnSubmit按钮的OnClick =btnSubmit_Click
        文本=提交=服务器/>< / DIV>
< /表及GT;保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!this.IsPostBack)
    {
        rptCustomer.DataSource =客户;
        rptCustomer.DataBind();
    }    btnSubmit.Attributes.Add(onclick事件,String.Concat(this.disabled = TRUE; disableCheckboxes();,ClientScript.GetPostBackEventReference(btnSubmit按钮,)));
}
保护无效DisplayCustomerRepeater_ItemDataBound(对象发件人,System.Web.UI.WebControls.RepeaterItemEventArgs E)
{
    ListItemType LT = e.Item.ItemType;
    如果(LT == || ListItemType.Item LT == ListItemType.AlternatingItem)
    {
        客户客户= e.Item.DataItem的客户;
        如果(客户!= NULL)
        {
            复选框chkCustomer = e.Item.FindControl(checkSubscription)的复选框;
            如果(chkCustomer!= NULL)
            {
                如果(customer.IsEligible)
                {
                    chkCustomer.Checked = TRUE;
                }
            }
        }
    }
}
保护无效btnSubmit_Click(对象发件人,EventArgs的发送)
{
    布尔=器isChecked虚假的;    的foreach(在rptCustomer.Items的RepeaterItem项)
    {
        复选框selectedCheckBox =(复选框)item.FindControl(checkSubscription);
        如果(selectedCheckBox.Checked)
        {
            =器isChecked真实;
        }
    }
}


解决方案

禁用表单元素根本不被当表单提交的浏览器发送。

顺便说一句这是与未选中的复选框正是发生为好。禁用的选中的复选框将被准确地处理当表单提交启用选中的。

因此​​,如果禁用所有复选框被提交表单之前,相应的服务器端CheckBox控件都将是上回发未选中。

当你需要重置回选中之前,每个文本框的状态复制到其他一些控制的解决方法。然后你可以用其他的字段,检查你的code后面的状态。

I would like to disable Checked checkboxes when the user clicks the Submit button in ASP.NET. I can disable the checkboxes using JavaScript using the onclick event of the button. The checkboxes are in a Repeater. When the form is submitted by clicking the Submit button, none of the checkboxes show as checked in the code-behind button click code. How can I disable Checked checkboxes when the submit button is clicked and still see that the checkboxes are Checked when the page is submitted? Is there a work around? My preference would be to do this in JavaScript but I'm willing to use jQuery as well.

function disableCheckboxes() {
    for (var count = 0; i < document.forms[0].elements.length; i++) 
    {
        if (document.forms[0].elements[count].type == 'checkbox')
        {
            document.forms[0].elements[count].disabled = true;
        }
    }
}
 <form id="form1" runat="server">
<div>
    <table>

        <asp:Repeater ID="rptCustomer" runat="server" OnItemDataBound="DisplayCustomerRepeater_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:CheckBox ID="checkSubscription" runat="server" />
                    </td>
                    <td>
                        <%# ((Customer)Container.DataItem).FirstName + " " + ((Customer)Container.DataItem).LastName%>
                    </td>
                </tr>
                <tr>
                    <td>
                        <hr />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
</div>
<div>
    <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click"
        Text="Submit" runat="server" /></div>
</form>

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        rptCustomer.DataSource = customers;
        rptCustomer.DataBind();
    }

    btnSubmit.Attributes.Add("onclick", String.Concat("this.disabled=true;disableCheckboxes();", ClientScript.GetPostBackEventReference(btnSubmit, "")));
}
protected void DisplayCustomerRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    ListItemType lt = e.Item.ItemType;
    if (lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
    {
        Customer customer = e.Item.DataItem as Customer;
        if (customer != null)
        {
            CheckBox chkCustomer = e.Item.FindControl("checkSubscription") as CheckBox;
            if (chkCustomer != null)
            {
                if (customer.IsEligible)
                {
                    chkCustomer.Checked = true;
                }
            }
        }
    }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    bool isChecked = false;

    foreach (RepeaterItem item in rptCustomer.Items)
    {
        CheckBox selectedCheckBox = (CheckBox)item.FindControl("checkSubscription");
        if (selectedCheckBox.Checked)
        {
            isChecked = true;
        }
    }
}

解决方案

Disabled form elements are simply not sent by the browser when the form is submitted.

Incidentally that's exactly what happens with an unchecked checkbox as well. A disabled checked checkbox will be processed exactly as an enabled unchecked one when the form is submitted.

So if you disable all the checkboxes before the form is submitted, the corresponding server side checkbox controls will all be unchecked on post back.

As a workaround you would need to copy the state of each text box into some other control before resetting it back to unchecked. And then you can use the other field to check the state in your code behind.

这篇关于单击提交按钮时禁用选中的复选框中的一个中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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