checkbox autopostback in datagrid

P

pleaseexplaintome

I have a datagrid with checkboxes and I can check/uncheck the
checkboxes to update a database by calling my oncheckchanged function.

I would like to add popup asking the users if they are sure they want
to proceed.

I have written a javascript function named confirm_duplicate and it
works as expected - it checks/unchecks checkboxes depending on user
response.

The problem is when I use the javascript alert the checkbox
autopostback does not occurr and my oncheckchanged function is never
called. Can anyone provide any clues of what I need to do? Thanks

asp code:

function confirm_duplicate()
{
if (confirm("Are you sure you want to \nchange this
selection?")==true)
return true;
else
return false;
}


<ItemTemplate>
<asp:CheckBox id=chk runat="server" AutoPostBack="true"
onCheckedChanged="oncheckchanged"
Checked='<%#IsCheck(DataBinder.EvalContainer.DataItem, "Duplicate"))
%>'>
</asp:CheckBox>
</ItemTemplate>

C# code:
void myDataGrid_ItemCreated(object
sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox _chk = (CheckBox)e.Item.FindControl("chk");
_chk.Attributes.Add("onclick", "return confirm_duplicate();");

}
}


public void oncheckchanged(object source, System.EventArgs e)
{
code to update database
}


protected bool IsCheck(object objInc)
{
helper code to check/uncheck datagrid column on page load
}
 
G

Guest

I can provide you with a work arround that worked fine with me:
1) 1st the javascript function that is similar to yours with small
modifications:
function MyConfirm(eventTarget,eventArgument)
{
theForm = document.form1
var bool = confirm('are you sure?');
if(bool)
{
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
else
{
return false
}
}
2) 2nd You need to use ItemDataBound Inseated of ItemCreated Event as the
following:
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
CheckBox chk = (CheckBox)e.Item.FindControl("chk");
if (chk != null)
{
string f = "javascript:return MyConfirm('{0}','{1}');";
f = string.Format(f, chk.ClientID,"");
chk.Attributes.Add("onclick", f);
}
}
3) keep AutoPostBack of your checkbox as true.

the rest of your code supposed to work correctly.

Please if anyone has better solution share it with us.

Note:
I've tried the ClientScript.ClientScript.GetPostBackEventReference instead
of my work arround, but it produced some javascript that I couldn't resolve.

Regards,
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top