944,121 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 3414
  • ASP.NET RSS
Oct 25th, 2009
0

CheckboxList ListItem Onclick not firing

Expand Post »
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:
ASP.NET Syntax (Toggle Plain Text)
  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:
ASP.NET Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simmy7 is offline Offline
11 posts
since Jun 2006
Oct 26th, 2009
0
Re: CheckboxList ListItem Onclick not firing
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mailme.vibin is offline Offline
6 posts
since Sep 2008
Oct 26th, 2009
0
Re: CheckboxList ListItem Onclick not firing
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?
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
guru_sarkar is offline Offline
68 posts
since Jul 2008
Oct 27th, 2009
0
Re: CheckboxList ListItem Onclick not firing
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simmy7 is offline Offline
11 posts
since Jun 2006
Oct 27th, 2009
0
Re: CheckboxList ListItem Onclick not firing
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.

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Oct 27th, 2009
0
Re: CheckboxList ListItem Onclick not firing
Click to Expand / Collapse  Quote originally posted by Ramesh S ...
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.

ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simmy7 is offline Offline
11 posts
since Jun 2006
Oct 27th, 2009
0
Re: CheckboxList ListItem Onclick not firing
From your first post, it seems that you put the ChecBoxList control inside a Repeater control.

If so, you can try this code.

ASP.NET Syntax (Toggle Plain Text)
  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/
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: does C# Window Service supports Browser_DocumentCompleted event of Web browser?
Next Thread in ASP.NET Forum Timeline: chat room in asp.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC