Checkbox server/client events
I have a checkbox in repeater control. on changing it's bit value, it should prompt the alert message like do u wish to delete? and if yes is selected, the onselectedchange server event should be fired.
Right now. on clicking the checkbox i am using onclick event to fire the prompt but it is not able to fire the server event.
here is my code
<script language="javascript" type="text/javascript">
function CheckedChange() {
if (confirm('Are you sure you want to delete?'))
return true;
else
return false;
}开发者_如何学Python
</script>
<asp:Repeater ID="repQuestion" runat="server" onitemcommand="repQuestion_ItemCommand" onitemdatabound="repQuestion_ItemDataBound">
<ItemTemplate>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:CheckBox onclick="return CheckedChange();" ID="delete" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "IsDeleted")%>' OnCheckedChanged="chkdelete_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Work around this snippet.
The Repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td><%# Eval("Index") %></td>
<td>
<asp:CheckBox ID="delete" runat="server"
AutoPostBack="true"
Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
OnCheckedChanged="chkdelete_Click"
onclick="javascript:return CheckedChange(this)"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
JavaScript
function CheckedChange(objCheckBox) {
if (confirm('Are you sure you want to delete?')) {
__doPostBack("'" + objCheckBox.id + "'", '');
return true;
}
else {
objCheckBox.checked = false;
return false;
}
}
Page_Load Event
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
//bind gridview here
DataTable dataTable = new DataTable
{
Columns = { "Index", { "IsDeleted", typeof(bool) } }
};
bool temp = true;
for (var i = 0; i < 6; i++)
{
dataTable.Rows.Add((i + 1).ToString(), temp);
temp = !temp;
}
Repeater1.DataSource = dataTable;
Repeater1.DataBind();
}
}
catch (Exception exception)
{
//Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
}
Well, I don't personally like exposing the __doPostBack
but this seems to be the only way in this case.
On an unrelated side note, if you are showing deleted items, its better to show them disabled.
Try by passing the UniqueID (that is the name of the Checkbox).
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td><%# Eval("Index") %></td>
<td>
<asp:CheckBox ID="delete" runat="server"
AutoPostBack="true"
Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
OnCheckedChanged="chkdelete_Click"
onclick="javascript:return CheckedChange(this)"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
JavaScript
function CheckedChange(objCheckBox) {
if (confirm('Are you sure you want to delete?')) {
__doPostBack("'" + objCheckBox.name + "'", '');
return true;
}
else {
objCheckBox.checked = false;
return false;
}
}
Try this
function confirmUser(checkBox)
{
if(checkBox.childNodes[0].checked === false){
alert('Your alert message');
}
}
<asp:Repeater ID="rptSelecteduserRoles" runat="server" OnItemDataBound="rptSelecteduserRoles_OnItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="chkBox" CssClass="fieldLabel" AutoPostBack="False" onchange="confirmUser(this)"runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:Repeater>
精彩评论