473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

checkbox autopostback in datagrid

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.DataI tem, "Duplicate"))
%>'>
</asp:CheckBox>
</ItemTemplate>

C# code:
void myDataGrid_ItemCreated(object
sender,System.Web.UI.WebControls.DataGridItemEvent Args 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
}

Aug 15 '06 #1
1 2692
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.GetPostBackEventReferenc e 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
"pl***************@yahoo.com" 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.DataI tem, "Duplicate"))
%>'>
</asp:CheckBox>
</ItemTemplate>

C# code:
void myDataGrid_ItemCreated(object
sender,System.Web.UI.WebControls.DataGridItemEvent Args 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
}

Aug 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephen | last post by:
I have the following asp:checkbox as a row in my datagrid. and im trying to write code on the onCheckedChanged event of it. The code im trying to write is incorrect as im getting build errors....
1
by: Andre | last post by:
Hi, I need some help with automatically generated checkbox in my datagrid, i'm searching everywhere and can't find a way to do this. In my datagrid i will always have more than one line, with...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have the following problem. I have a datagrid (ID: grdAllActions). This datagrid has two template columns: one column with the dropdownlist control (ID: ddlPS) and...
7
by: Lars Netzel | last post by:
If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true I can catch OnChange event on checkbox but how do I then catch what DataGridItemIndex is? Can I use some Event in the...
2
by: Asha | last post by:
greetings, i'm loading a datagrid and my gird has checkbox, i want these checkboxex to be check based on the value from the db? can someone please show me the code for it? thanks
4
by: Sileesh | last post by:
Hi I have datagrid with 8 items. Out of which 2 items are checkboxes. Data is binded dynamically to the datagrid Based on some values from the database I have to check or uncheck the checkbox...
2
by: Adam Knight | last post by:
Hi all, I have a datagrid with a checkbox in one column. The checkbox is set to autopostback and calls a method named UpdateMailSubscribers. The first click on the checkbox cause the page to...
0
by: pleaseexplaintome | last post by:
I have a datagrid with checkboxes and I am able 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...
0
by: rn5a | last post by:
I have a DataGrid where in the first column of each row is a CheckBox (the CheckBoxes reside in the TemplateColumn of the DataGrid). The CheckBox's AutoPostBack property is set to True & its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.