Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2005, 11:17 PM
Homa
Guest
 
Posts: n/a
Default DataGrid with Checkbox: Need both OnCheckedChanged and onclick client script

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
  #2  
Old November 18th, 2005, 12:11 AM
Dave Moore
Guest
 
Posts: n/a
Default Re: DataGrid with Checkbox: Need both OnCheckedChanged and onclick client script

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

homaneag@yahoo.com (Homa) wrote in message news:<a9a6fc0b.0311061518.23a16225@posting.google. com>...[color=blue]
> 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[/color]
  #3  
Old November 18th, 2005, 12:48 AM
James Friesen
Guest
 
Posts: n/a
Default Re: DataGrid with Checkbox: Need both OnCheckedChanged and onclick client script

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.