C# 中继器控件中的 DropDownList,无法触发 SelectedIndexChanged

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/175845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:42:47  来源:igfitidea点击:

DropDownList in Repeater control, can't fire SelectedIndexChanged

c#asp.net

提问by jwarzech

I have a repeater control where in the footer I have a DropDownList. In my code-behind I have:

我有一个中继器控件,在页脚中有一个 DropDownList。在我的代码隐藏中,我有:

protected void ddMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item
            || e.Item.ItemType == ListItemType.AlternatingItem)
    {
       // Item binding code
    }

    else if (e.Item.ItemType == ListItemType.Footer)
    {
        DropDownList ddl = e.Item.FindDropDownList("ddMyDropDownList");
        // Fill the list control
        ddl.SelectedIndexChanged += new  
           EventHandler(ddMyDropDownList_SelectedIndexChanged);
        ddl.AutoPostBack = true;
    }
 }

The page appear to PostBack however my EventHandler does not get called. Any ideas?

该页面出现在 PostBack 中,但是我的 EventHandler 没有被调用。有任何想法吗?

采纳答案by KyleLanser

If you just want to fire the OnSelectedIndexChanged, this is how it should look:

如果您只想触发 OnSelectedIndexChanged,它应该是这样的:

Page.aspx - Source

Page.aspx - 源

<FooterTemplate>
    <asp:DropDownList ID="ddlOptions"
             runat="server" 
             AutoPostBack="true" 
             onselectedindexchanged="ddlOptions_SelectedIndexChanged">
        <asp:ListItem>Option1</asp:ListItem>
        <asp:ListItem>Option2</asp:ListItem>
    </asp:DropDownList>
</FooterTemplate>

Page.aspx.cs - Code-behind

Page.aspx.cs - 代码隐藏

protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Event Code here.
    }

And that's it. Nothing more is needed.

就是这样。什么都不需要了。

回答by John Rudy

I think it's because you're probably not databinding on postbacks. I haven't tested this, but try hooking that code up to the ItemCreated event for your repeater instead.

我认为这是因为您可能没有对回发进行数据绑定。我尚未对此进行测试,但请尝试将该代码与中继器的 ItemCreated 事件挂钩。

回答by Dillie-O

Is the AutoPostBack property set to True on the DropDownLists on the ASPX side? I know sometimes this property doesn't get set initially and it will prevent the SelectedIndexChanged event from firing.

ASPX 端的 DropDownLists 上的 AutoPostBack 属性是否设置为 True?我知道有时这个属性最初没有设置,它会阻止 SelectedIndexChanged 事件触发。

回答by Chad Braun-Duin

In this case your parent repeater (ddMyRepeater) must databind itself in page_load on every postback. This is the only way I've found to get nested controls to fire their events.

在这种情况下,您的父中继器 (ddMyRepeater) 必须在每次回发时在 page_load 中对自身进行数据绑定。这是我发现让嵌套控件触发其事件的唯一方法。

This may not be the ideal scenario for you, though. Depending on what your page is doing, you may have to databind this control, twice. Once to get the events to fire and a second time if a fired event causes the repeater's data to change in any way.

不过,这可能不是您的理想方案。根据您的页面正在执行的操作,您可能需要对该控件进行两次数据绑定。一次触发事件,第二次触发事件导致转发器的数据以任何方式改变。

回答by Florjon

I think the problem comes from the fact that the dropdownlist control is not inside the repeter, but on the footer. I don't think that the envent of the reperter fires for the controls that are on the footer. You should try to put the dropdowncontrol out of the repeater control.

我认为问题来自这样一个事实,即下拉列表控件不在重复器内,而是在页脚上。我不认为 reperter 的出现会激发页脚上的控件。您应该尝试将下拉控件置于中继器控件之外。

回答by Florjon

Make sure ViewState is enabled for dropdownlist

确保为下拉列表启用了 ViewState

回答by KevinUK

If the DropDownList is within a Repeater then to make the SelectIndexChanged event fire, you need to disable EnableViewState on the GridView / Repeater.

如果 DropDownList 在中继器内,那么要触发 SelectIndexChanged 事件,您需要在 GridView/中继器上禁用 EnableViewState。

e.g.

例如

EnableViewState="false"

You also need to databind the GridView / Repeater on each postback so databind it in the Page Load method.

您还需要在每次回发时对 GridView/Repeater 进行数据绑定,以便在页面加载方法中对其进行数据绑定。