开发者

OnCheckedChanged event not firing when Checkboxes within Repeater are checked

I am a relatively new developer, and have only been doing this for about 6 months full time, so thank you in advance for reading and/or responding to my question

I have a databound repeater. Inside this repeater, I have a gridview, SQLDS, and 2 checkboxes. Both checkboxes have a OnCheckedChanged event and AutoPostback is set to true. The repeater has an OnItemDataBound event as well.

Here is a sample of how my code is laid out:

    <asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:Panel>
                <asp:UpdatePanel>
                    <ContentTemplate>
                        <asp:Checkbox ID="Checkbox1" Autopostback="True" OnCheckedChanged="CheckBox1_CheckedChanged">
                        <asp:Checkbox ID="Checkbox2"Autopostback="True" OnCheckedChanged="CheckBox2_CheckedChanged">
                        <asp:Gridview ID="Gridview1" DataSourceID="SqlDataSource1">
                        <asp:SQLDataSource ID="SQLDataSource1" SelectCommand="SP1" SelectCommandType="StoredProcedure">
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
        </ItemTemplate>
    </asp:Repeater>

And the C#

    protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
    {
        if (Checkbox1.Checked == true)
            {
                if (Checkbox2.Checked == true)
                    SqlDataSource1.SelectCommand = "SP1";
                else
                    SqlDataSource1.SelectCommand = "SP2";
            }
            else
                SqlDataSource1.SelectCommand = "SP3";
    }
    protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
    {
        if (Checkbox2.Checked == true)
            {
                if (Checkbox1.Checked == true)
                    SqlDataSource1.SelectCommand = "SP3";
                else
                    SqlDataSource1.SelectCommand = "SP2";
            }
            else
                SqlDataSource1.SelectCommand = "SP1";开发者_JAVA技巧
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //Uses FindControl to Databind the GV and hides it if GV.Rows.Count==0
    }

I'm doing all of this within an AJAX TabPanel. I have another page where this code works perfectly, but it's not inside a repeater on that other page.

Essentially, I have a page load with a gridview, and the two checkboxes change what SP fills the gridview. The problem I'm having is that when you uncheck the checkbox (they start checked), it 1. Just rechecks itself and 2. Doesn't hit the CheckedChanged event.

Any help would be GREATLY appreciated.


You need to set AutoPostBack=True attribute for the checkboxes and also register/assign event handlers of all controls which are added into the UpdatePanel through Triggers property of UpdatePanel control.


The Autopostback on the checkboxes is causing the SQLDatasource to just revert to the original Stored Procedure, as it hits page load and just ignores the oncheckchanged events.

What I did is I took all of my databinding events in pageload and put them inside of a if (!IsPostBack) clause, so that when the Autopostback hits, it doesn't rebind the SQLDS to the original value.

This way, when the autopostback occurs, there's nothing for it to do, and it hits the OnCheckedChanged events as it's supposed to.

Thank you everyone for reading and responding.


ASP.NET Repeater has an event that causes a round trip from client to occur. The event is called ItemCommand. Check out this link :- http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx

Hope this helps


i found a similar issue and solved

http://www.codeproject.com/Questions/88960/OnCheckedChanged-Event-for-the-checkbox-inside-the/?cmt=3621

Regards


First, you need to bring the UpdatePanel outside of the repeater, and add runat="server". You should also bring the datasource control outside of the repeater too, and reuse it for all of the GridViews inside of the repeater.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <!-- checkboxes -->
                <!-- gridview -->
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ...>


Actually your databinding method probably called outside the if(!ISPOSTBACK) { } condition, so call this method inside the ispostback block, i hope your problem will be solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜