0

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: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:

protected void chkSides_OnItemDataBound(object sender, EventArgs e)
    {
        
        CheckBoxList chkSides = (CheckBoxList)sender;
        foreach (ListItem listitem in chkSides.Items)
        {
            listitem.Attributes.Add("onclick", "alert('hey');");
        }
    }

The checkboxlist is databound and within a databound repeater.

Any ideas?

Thanks!

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Ramesh S
0

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

0

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?

0

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?

0

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/archive/2004/05/04/213.aspx
http://support.microsoft.com/default.aspx?scid=kb;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.

protected void Page_PreRender(object sender, EventArgs e)
    {

        foreach (ListItem listitem in chkSides.Items)
        {
            listitem.Attributes.Add("onclick", "alert('hey');");
        }
    }

Edited by Ramesh S: n/a

0

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/archive/2004/05/04/213.aspx
http://support.microsoft.com/default.aspx?scid=kb;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.

protected void Page_PreRender(object sender, EventArgs e)
    {

        foreach (ListItem listitem in chkSides.Items)
        {
            listitem.Attributes.Add("onclick", "alert('hey');");
        }
    }

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

0

From your first post, it seems that you put the ChecBoxList control inside a Repeater control.

If so, you can try this code.

protected void Page_PreRender(object sender, EventArgs e)
    {
        foreach (RepeaterItem ritem in Repeater1.Items)
        {
            CheckBoxList chkList = ritem.FindControl("CheckBoxList1") as CheckBoxList;
            foreach (ListItem listitem in chkList.Items)
            {
                listitem.Attributes.Add("onclick", "alert('hey');");
            }
        }
    }

Anyway, you found a solution by yourself.

Mark this thread as solved/

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.