Search code examples
c#asp.netajaxradio-buttonupdatepanel

CheckedChanged event doesn't work


I have make a structure like this:

  • Topic 1
    • Topic A
      • Topic α
      • Topic β
    • Topic B
      • Topic γ
      • Topic δ
  • Topic 2
    • Topic C
      • Topic ε
      • Topic ζ
    • Topic D
      • Topic η
      • Topic θ

First comes topics 1 and 2 on screen. By checking one of them Topic A and B or C and D comes on screen. Finally coms α until θ on screen dependent on the chosen letters. I do this with the following code:

ASP.NET

<asp:ScriptManager EnablePartialRendering="true" runat="server" />

<div>
    <asp:UpdatePanel ID="updPnlTopics" UpdateMode="Always" runat="server">
        <ContentTemplate>

            <asp:Panel ID="pnlNumbers" CssClass="topics" runat="server" />
            <asp:Panel ID="pnlLetters" CssClass="topics" runat="server" />
            <asp:Panel ID="pnlRoman" CssClass="topics" runat="server" />

        </ContentTemplate>
    </asp:UpdatePanel>

</div>

C#

private List<TopicNumbers> _allTopics;
private TopicNumber _topicNumber;

protected async void Page_Load(object sender, EventArgs e)
{
    await LoadTopics();
}

private async Task LoadTopics()
{
    _allTopics = await GetAllTopics(); //this gets all topics from database

    foreach (TopicNumber number in _allTopics)
    {
        MakeRadioButton<TopicNumber>(number, pnlNumbers, new EventHandler(rbtNumber_CheckedChanged));
    }
}

private void rbtNumber_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rbt = (RadioButton)sender; // 1 ● --> stop always

    pnlLetters.Controls.Clear();
    pnlRomain.Controls.Clear();

    if (rbt.Checked)
    {
        _topicNumber = (from number in _allTopics
                        where number.Naam == rbt.Text
                        select number).First<TopicNumber>();

        foreach (TopicLetter letter in _topicNumber.Letters) //Letters contains a list with all the letters for that number
        {
            MakeRadioButton<TopicLetter>(letter, pnlLetters, new EventHandler(rbtLetter_CheckedChanged));
        }
    }
}

public void rbtLetter_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rbt = (RadioButton)sender; // 2 ● --> don't stop

    pnlRoman.Controls.Clear();

    if (rbt.Checked)
    {
       TopicLetters letter = (from letter in _topicNumber.Letters
                              where letter.Naam == rbt.Text
                              select letter).First<TopicLetters>();

       foreach (TopicRomain romain in letter.Romans) //contains a list with all roman letters for that western letter
       {
            RadioButton rbtRoman = new RadioButton() {
                Text = roman.ToString(),
                GroupName = "roman",
                AutoPostBack = true
            };

            pnlRoman.Controls.Add(rbtRoman);

            pnlRoman.Controls.Add(new Literal() {
                Text = "<br/>"
            });
        }
    }
}

private void MakeRadioButton<T>(T type, Panel pnl, EventHandler evt)
{
    RadioButton rbt = new RadioButton() {
        Text = type.ToString(),
        GroupName = pnl.ID,
        ID = type.ToString(),
        AutoPostBack = true,
        CssClass = "topic"
    };

    rbt.CheckedChanged += evt;

    //\\
    updPnlCategorieen.Triggers.Add(new AsyncPostBackTrigger() {
        ControlID = rbt.ID,
        EventName = "CheckedChanged"
    });

    pnl.Controls.Add(rbt);

    pnl.Controls.Add(new Literal() {
        Text = "<br/>"
    });
}

Now my problem is when I will show the roman letters. My code looks to be good (think I), but rbtLetter_CheckedChanged never runs. I've place two breakpoints (see ● in code), but the compiler always stops on the first one and never by the second one.

Some images for explain it better:

  1. First state = beginning state: (only numbers are show) ok

    enter image description here

    I check one of the numbers.

  2. Second state: (only numbers and letters are show) ok

    enter image description here

    I check one of the letters.

  3. Third state: (numbers, letters and roman must be show) error

    enter image description here


Edit: august 15, 2015

I've also try to add a trigger on the UpdatePanel everytime a Checkbox is created.

This is my code that I used for it (stands //\\ on code above).

updTopics.Triggers.Add(new AsyncPostBackTrigger()
{
    ControlID = rbtNumber.ID, // or rbtLetter
    EventName = "CheckedChanged"
});

But it is always the same problem that the roman and the letters are deleted.


Edit: august 27, 2015

Anyone (on another community) said me that there is a page life cycle. Now I know why this code is not working. But I don't know how to solve this.

I know this question is too broad, but can anyone help me or give me a hint for my problem? I will test it if your idea works. I'm really new with ASP.net.
Thanks for all help and sorry for my poor english.


Solution

  • On the way you have develop it, goes it never work. UpdatePanels can not be used for that. An alternative you can use are user controls. and into that control can UpdatePanels work.