简体   繁体   English

CheckedChanged事件不起作用

[英]CheckedChanged event doesn't work

I have make a structure like this: 我有一个这样的结构:

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

First comes topics 1 and 2 on screen. 首先是屏幕上的主题1和2。 By checking one of them Topic A and B or C and D comes on screen. 通过检查其中之一,主题A和B或C和D出现在屏幕上。 Finally coms α until θ on screen dependent on the chosen letters. 最后根据选择的字母在屏幕上显示α直到θ。 I do this with the following code: 我使用以下代码执行此操作:

ASP.NET 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# 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. 我的代码看起来不错(想想我),但是rbtLetter_CheckedChanged永远不会运行。 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 第一状态=起始状态:( 仅显示数字)

    在此处输入图片说明

    I check one of the numbers. 我检查了其中一个数字。

  2. Second state: (only numbers and letters are show) ok 第二种状态:( 仅显示数字和字母) 确定

    在此处输入图片说明

    I check one of the letters. 我检查了一封信。

  3. Third state: (numbers, letters and roman must be show) error 第三状态:( 必须显示数字,字母和罗马) 错误

    在此处输入图片说明


Edit: august 15, 2015 编辑:2015年8月15日

I've also try to add a trigger on the UpdatePanel everytime a Checkbox is created. 每当尝试创建Checkbox我也尝试在UpdatePanel上添加触发器。

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 编辑:2015年8月27日

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. 我对ASP.net真的很陌生。
Thanks for all help and sorry for my poor english. 感谢所有帮助,对不起我的英语不好。

On the way you have develop it, goes it never work. 在开发过程中,它永远都行不通。 UpdatePanel s can not be used for that. UpdatePanel不能用于此目的。 An alternative you can use are user controls. 您可以使用的替代方法是用户控件。 and into that control can UpdatePanel s work. 并在该控件中可以运行UpdatePanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM