RE: checkbox autopostback in datagrid
- From: Muhammad Mosa <MuhammadMosa@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Aug 2006 18:19:01 -0700
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
"pleaseexplaintome@xxxxxxxxx" wrote:
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
}
- References:
- checkbox autopostback in datagrid
- From: pleaseexplaintome
- checkbox autopostback in datagrid
- Prev by Date: RE: Can't create web project
- Next by Date: Re: atlas installation
- Previous by thread: checkbox autopostback in datagrid
- Next by thread: How to change the Crystal Report paper orientation to Horizontal ?
- Index(es):
Relevant Pages
|