CheckboxList ListItem Onclick not firing

Thread Solved

Join Date: Jun 2006
Posts: 11
Reputation: simmy7 is an unknown quantity at this point 
Solved Threads: 0
simmy7 simmy7 is offline Offline
Newbie Poster

CheckboxList ListItem Onclick not firing

 
0
  #1
Oct 25th, 2009
I am using .Net 2 in Vs 2008 and I am trying to get the onclick event for a checkox to fire when clicked. Unfortunately it is not firing. I can see the code is added to the checkboxs html.

This is the checkboxlist:
  1. <asp:CheckBoxList runat="server" ID="chkSides" RepeatColumns="3" RepeatDirection="Horizontal" DataValueField="ItemNo" DataTextField="ShortName" BackColor="White" OnDataBound="chkSides_OnItemDataBound" ></asp:CheckBoxList>

this is the code that set the onclick attribute:
  1. protected void chkSides_OnItemDataBound(object sender, EventArgs e)
  2. {
  3.  
  4. CheckBoxList chkSides = (CheckBoxList)sender;
  5. foreach (ListItem listitem in chkSides.Items)
  6. {
  7. listitem.Attributes.Add("onclick", "alert('hey');");
  8. }
  9. }

The checkboxlist is databound and within a databound repeater.

Any ideas?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: mailme.vibin is an unknown quantity at this point 
Solved Threads: 1
mailme.vibin mailme.vibin is offline Offline
Newbie Poster
 
0
  #2
Oct 26th, 2009
Did u set the AutoPostBack property of the checkbox. set it as true. May be this will help....MAKE THIS A SOLUTION IF U FIND THE ANSWER
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 44
Reputation: guru_sarkar is an unknown quantity at this point 
Solved Threads: 6
guru_sarkar guru_sarkar is offline Offline
Light Poster
 
0
  #3
Oct 26th, 2009
I just copy Pasted your code, added my own DataSource and it works.
It shows me alert 'hey' message when i click the checkbox to check/unchek an item.

How are you dataBinding your CBL?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 11
Reputation: simmy7 is an unknown quantity at this point 
Solved Threads: 0
simmy7 simmy7 is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
Originally Posted by guru_sarkar View Post
I just copy Pasted your code, added my own DataSource and it works.
It shows me alert 'hey' message when i click the checkbox to check/unchek an item.

How are you dataBinding your CBL?
I forgot to mention that the repeater and checkboxlist are within an ajax updatepanel. So the problem seems to be that on the partial post back the OnItemDataBound isn't being fired. So the question now becomes how can I set the attribute in such a way that it is persisted across post backs?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 452
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
0
  #5
Oct 27th, 2009
Putting the CheckBoxList control inside a UpdatePanel control does not make this problem.

The CheckBoxList is implemented a collection of ListItems.

Attributes added to a ListItem control don't get rendered during postback.

Actually it is a bug in ASP.NET.

Refer the following links to know more about on this bug.

http://unboxedsolutions.com/sean/arc...05/04/213.aspx
http://support.microsoft.com/default...;en-us;Q309338
http://aspnet.4guysfromrolla.com/articles/091405-1.aspx

To work around this issue, you need to add attributes to ListItem collection in the Page PreRender event instead of DataBound event.

Try this code.

  1. protected void Page_PreRender(object sender, EventArgs e)
  2. {
  3.  
  4. foreach (ListItem listitem in chkSides.Items)
  5. {
  6. listitem.Attributes.Add("onclick", "alert('hey');");
  7. }
  8. }
Last edited by Ramesh S; Oct 27th, 2009 at 6:30 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 11
Reputation: simmy7 is an unknown quantity at this point 
Solved Threads: 0
simmy7 simmy7 is offline Offline
Newbie Poster
 
0
  #6
Oct 27th, 2009
Originally Posted by Ramesh S View Post
Putting the CheckBoxList control inside a UpdatePanel control does not make this problem.

The CheckBoxList is implemented a collection of ListItems.

Attributes added to a ListItem control don't get rendered during postback.

Actually it is a bug in ASP.NET.

Refer the following links to know more about on this bug.

http://unboxedsolutions.com/sean/arc...05/04/213.aspx
http://support.microsoft.com/default...;en-us;Q309338
http://aspnet.4guysfromrolla.com/articles/091405-1.aspx

To work around this issue, you need to add attributes to ListItem collection in the Page PreRender event instead of DataBound event.

Try this code.

  1. protected void Page_PreRender(object sender, EventArgs e)
  2. {
  3.  
  4. foreach (ListItem listitem in chkSides.Items)
  5. {
  6. listitem.Attributes.Add("onclick", "alert('hey');");
  7. }
  8. }
At page prerender the checkboxlist isn't being found so this won't work either.

In any case I found a way to stop the partial post back so I don't have this problem now.

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 452
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
0
  #7
Oct 27th, 2009
From your first post, it seems that you put the ChecBoxList control inside a Repeater control.

If so, you can try this code.

  1. protected void Page_PreRender(object sender, EventArgs e)
  2. {
  3. foreach (RepeaterItem ritem in Repeater1.Items)
  4. {
  5. CheckBoxList chkList = ritem.FindControl("CheckBoxList1") as CheckBoxList;
  6. foreach (ListItem listitem in chkList.Items)
  7. {
  8. listitem.Attributes.Add("onclick", "alert('hey');");
  9. }
  10. }
  11. }

Anyway, you found a solution by yourself.

Mark this thread as solved/
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 572 | Replies: 6
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC