DataGrid with Checkbox: Need both OnCheckedChanged and onclick client script

H

Homa

Hi,

I have a Datagrid that uses as a shopping cart and have a checkbox
template column in it. I can't add both OnCheckedChanged Event and
onclick client script for it.


I want to do two things:

1. I want the checkbox fires CheckedChanged event. I did this as
follows:

In the DataGrid:
<templateColumn>
....
<asp:CheckBox id="chk" runat="server" AutoPostBack=True
OnCheckedChanged="handler" />
....
</templateColumn>

This works fine.....by itself

2. I want to show a confirm box:

In the same CheckBox:
<asp:CheckBox ... onclick="javascript:return confirm('Are you
Sure?');" />

This again, works fine by itself.

But when these two code goes together. Only the confirm button will
show without doing the postback.

When I look that the source, I saw both of them use the onclick,
resulting both javascript combined together:


name="dg:_ctl2:chk"
language="javascript"
onclick="Javascript:return confirm('Are you
sure?');__doPostBack('dg$_ctl2$chk','')"


Can anyone give me some idea about how to solves this?

Thanks,
Homa Wong
 
D

Dave Moore

Hi -
I'm actually trying to solve this same problem. Has anyone found any
solution to this? It's driving me nuts!
Thanks!
 
J

James Friesen

I ran into the same problem, and couldn't find a solution here, so I
managed to find a workaround.

You are correct that .net seems to combine the two functions into one
which creates the problem.

My stategy here was to manually call the autopostback.
First, I set the autopostback on the checkbox to false.

Note: If you now have no controls on the form with sutopostback set
to true, then the __doPostBack function will not be autogenerated.

The easy solution to this is to create a control (give it height=0 and
width=0) with autopostback=true

Or you can copy and paste a __doPostBack method by viewing the source
before you remove the autopostback, but I personally would just create
an extra control

In my .vb file

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.CheckBox1.Attributes.Add("OnClick", "javasctipt:CheckConfirm();")
End Sub

Protected Sub Blah(ByVal Sender As Object, ByVal E As
System.EventArgs)
'do whatever
'Me.Label1.Text = "Blah"
End Sub


In my .aspx file

<script language="javascript">
function CheckConfirm() {

if (confirm("Are you Sure?"))
__doPostBack('CheckBox1','');
}
</script>

<asp:CheckBox OnCheckedChanged="blah" id="CheckBox1" style="Z-INDEX:
102; LEFT: 31px; POSITION: absolute; TOP: 28px"
runat="server"></asp:CheckBox>


Hope that helps
James

P.S. I have by hotmail filter set to junk all emails which are not in
my address book, so if you have any questions, please reply to the
newsgroup
 

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

Top