当用户单击 ASP.NET 中的提交按钮时,我想禁用选中复选框。我可以使用按钮的 onclick 事件使用 JavaScript 禁用复选框。复选框位于中继器中。当通过单击提交按钮提交表单时,在代码隐藏按钮单击代码中,没有一个复选框显示为选中状态。如何在单击提交按钮时禁用选中的复选框,并且在提交页面时仍然看到复选框被选中?有解决办法吗?我的偏好是在 JavaScript 中执行此操作,但我也愿意使用 jQuery。
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;
}
}
}