Results 1 to 12 of 12

Thread: [RESOLVED] Checkbox event won't fire in ASP.NET datalist

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Resolved [RESOLVED] Checkbox event won't fire in ASP.NET datalist

    Hi all,

    I have a checkbox in my datalist. The autopostback is set to true. I double clicked the checkbox which created this event:
    Code:
    Protected Sub chkWick_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    
                    Stop
        End Sub
    As a test I threw a Stop in there. Now when I run it and click the checkbox, the event is not firing. Am I doing something wrong? I'm using VS 2008 ans ASP.NET 2.0

    Thanks,

    Strick

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Checkbox event won't fire in ASP.NET datalist

    Show your ASPX markup. Ensure that you have AutoPostback="true" and the OnCheckedChanged attribute set.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Checkbox event won't fire in ASP.NET datalist

    Hi, thanks for your response. Yes both are set. Here is my ASPX code of the datalist:

    Code:
    <asp:DataList ID="listShoppingCart" runat="server" 
                BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
                CellPadding="2" ForeColor="Black" Height="16px" Width="434px">
                <FooterStyle BackColor="Tan" />
                <AlternatingItemStyle BackColor="PaleGoldenrod" />
                <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                <HeaderStyle BackColor="Tan" Font-Bold="True" />
                <FooterTemplate>
                    <table class="style27">
                        <tr>
                            <td>
                                <asp:Label ID="Total" runat="server" Text="Total"></asp:Label>
                                <asp:Label ID="lblTotal" runat="server" Font-Bold="True"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </FooterTemplate>
                <ItemTemplate>
                    <table class="style1">
                        <tr>
                            <td class="style4" rowspan="2">
                                <asp:ImageButton ID="ImageButton1" runat="server" 
                                    ImageUrl="~/images/delete_button.gif" CommandName="RemoveItem" />
                            </td>
                            <td class="style18">
                                <asp:Label ID="Label2" runat="server" Text="Fragrance:"></asp:Label>
                            </td>
                            <td class="style22" colspan="5">
                                <asp:Label ID="Label1" runat="server" Font-Bold="False" Font-Underline="True" 
                                    Text='<%# Databinder.Eval(Container.DataItem, "ProductName") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td class="style18">
                                <asp:Label ID="lblQTY" runat="server" Text="Quantity:"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server" 
                                    Text='<%# Databinder.Eval(Container.DataItem, "Quantity") %>' Width="36px" 
                                    ontextchanged="TextBox1_TextChanged"></asp:TextBox>
                            </td>
                            <td class="style24">
                                <asp:Label ID="Label3" runat="server" Text="Price:"></asp:Label>
                            </td>
                            <td class="style25">
                                <asp:Label ID="lblListPrice" runat="server" 
                                    Text='<%# Databinder.Eval(Container.DataItem, "ListPrice","{0:c}") %>'></asp:Label>
                            </td>
                            <td>
                                <asp:CheckBox ID="chkWick" runat="server" AutoPostBack="true" 
                                    EnableViewState="true" Checked='<%# Databinder.Eval(Container.DataItem, "WickInd") %>' 
                                    Text="Wick" oncheckedchanged="chkWick_CheckedChanged" />
                            </td>
                            <td class="style26">
                                <asp:Label ID="Label4" runat="server" Text="Total:"></asp:Label>
                            </td>
                            <td class="style22">
                                <asp:Label ID="Label5" runat="server" 
                                    Text='<%# Databinder.Eval(Container.DataItem, "Total","{0:c}") %>'></asp:Label>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
    The checkbox with ID "chkWick" is the the one that wont fire the Public sub procedure listed in my prev. post

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Checkbox event won't fire in ASP.NET datalist

    Looks fine.

    When you click on the checkbox does it post back? I assume it does.

    Handle the datalist's ItemCommand event. I believe that the checkbox's event may actually be hitting the ItemCommand event which means that you will need to supply the checkboxes with CommandName and CommandArgument properties so that you can identify it in the ItemCommand event.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Checkbox event won't fire in ASP.NET datalist

    Hi,

    The checkbox does indeed postback. It just doesn't fire the event. I originally thought the same of your recommended solution. I later found via searching the net that the checkbox in a datalist does not fire the ItemCommand event (which bites..lol) I read that only the imagebutton, button, and linkbutton (might be a few more objects I'm missing here) fire the ItemCommand event. The checkbox in fact doesn't even have the CommandName and CommandArgument properties. Been stumped on what seems so simple for like 4 or 5 days..

    Strick

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Checkbox event won't fire in ASP.NET datalist

    OK, here's another approach - why do you need the checkbox to postback and raise the event? What is the overall goal here?

    You're right that this should work. If you specified an oncheckedchange event, that event should be raised. But there are certain controls in ASP.NET such as the textbox and checkbox which 'ideally' shouldn't cause postbacks in an application (not that they don't, it's about good practices). Perhaps there's another way to do what you want.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Checkbox event won't fire in ASP.NET datalist

    Aargh, it just occurred to me. You must be rebinding the datalist in the page load event, which is why it is losing viewstate and therefore losing the fact that the event needs to be raised.

    You need to rebind your datalist in the Page Init event so that the viewstate can be reassigned to those checkboxes so that the event can be raised.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Checkbox event won't fire in ASP.NET datalist

    Hi thanks for your response. I think i'm a little closer to understanding what you mean. So in my page load do you mean I need to put my code:

    Code:
    mydatalist.datasource = mydatatable
    mydatalist.databind
    inside a page.ispostback if statement? Is that what you mean by "rebind your datalist in the Page Init event"?

    Thanks,

    Strick

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Checkbox event won't fire in ASP.NET datalist

    Oh my god, that was it! Thanks so much Mendhak. Now i'm pissed cause I been tryin to figure that out since last week..lol. Marking resolved.

    Strick

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: [RESOLVED] Checkbox event won't fire in ASP.NET datalist

    Share your solution here so that others with the same problem can benefit from your experience. It's a somewhat common issue so there are bound to be other searchers.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: [RESOLVED] Checkbox event won't fire in ASP.NET datalist

    Good idea. I wouldn't wish what I went through the past 3-4 days (googling like crazy, searching this site, etc.) to my worse enemy..lol. I'll post it when I get to work

    Again thanks for your help.

    Strick

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: [RESOLVED] Checkbox event won't fire in ASP.NET datalist

    Hi all,

    Just listing how this was resolved in case someone else has this issue. Basically what was happening here was in my load even I was binding the datalist to a class:

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
                listShoppingCart.DataSource = Cart.dtShoppingCart
                listShoppingCart.DataBind()
           
        End Sub
    Then I had a checkbox in my datalist which I double clicked and had a checkchanged event. This event was supposed to fire when the checkbox was clicked but it wasn't. The reason it wasn't was because on postback the load even was firing first. The load event was rebinding the datalist thus getting rid of the fact that the checkchanged even was fired since it is a newly bound datalist. The key here was basically only bind the datalist once on first page load or after the check changed event.

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Page.IsPostBack = False Then
               
                listShoppingCart.DataSource = Cart.dtShoppingCart
                listShoppingCart.DataBind()
            End If
           
        End Sub
    checking for a postback solved this.

    Then after coding the checkchange event make sure to rebind the datalist so you get the latest changes from the datatable/dataset.

    Hope that helps!

    Strick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width