在Repeater里面的UpdatePanel里面的RadioButtonList,我可以吗?

Lei*_*igh 3 asp.net updatepanel repeater radiobuttonlist nested

我在ItemTemplate中有一个带有RadioButtonList的转发器,但是当RadioButtonList.OnSelectedIndexChanged事件触发时,它会生成一个完整的回发.我在下面的代码中做错了什么?如何获取OnSelectedIndexChanged以生成异步回发?

<asp:UpdatePanel runat="server" ID="UpdatePanel2">
    <ContentTemplate>
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="sqlOptions">
            <ItemTemplate>
                <asp:UpdatePanel runat="server" ID="pnlA">
                    <ContentTemplate>
                        <strong>
                            <%# Eval("Name") %></strong><br />
                        <asp:RadioButtonList ID="RadioButtonList1" 
                             DataSourceID="sqlOptionValues" runat="server"
                             DataTextField="id" DataValueField="Id" AutoPostBack="true" 
                             OnSelectedIndexChanged="LoadPrice"
                             ValidationGroup="options" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                             ForeColor="Red" runat="server" 
                             ControlToValidate="RadioButtonList1" 
                             ErrorMessage="Required Field" 
                             ValidationGroup="options" />
                        <asp:SqlDataSource ID="sqlOptionValues" runat="server" 
                             ConnectionString="<%$ ConnectionStrings:
                                 ConnectionString6 %>"
                             SelectCommand='<%# "SELECT DISTINCT OptionValue.Name,
                                 OptionValue.Id FROM CombinationDetail 
                                 INNER JOIN OptionValue 
                                 ON CombinationDetail.OptionValueId = OptionValue.Id 
                                 WHERE (OptionValue.OptionId =" + 
                                 Eval("Id") + ")" %>'>
                        </asp:SqlDataSource>
                        <br />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助:)

Mik*_*Vee 11

这是一个真实的用例.我有一个带有Repeater的页面,其他Accordions中的Ajax Accordions,其他Update面板中的Update Panels,你可以命名.页面效果很好,除非我想用我的RadioButtonList(RBL)更新其中一个Accordion面板.即使在更新面板中使用RBL,它也会导致整个页面的回发.我尝试了一切.当我注意到我的按钮工作得很好时,我终于意识到这不是我.我认为它必须是框架或Ajax Control Toolkit中的错误.我确实发现人们在整个网站上引用了这个链接(http://blog.smarx.com/posts/the-case-of-the-radiobuttonlist-half-trigger.aspx),但是2007年的这个链接已经死了,可能不再适用,所以没有帮助.

我最终做的是做什么工作 - 提交按钮.我所做的只是向RBL添加一个onclick属性来调用隐藏按钮.现在您不希望将按钮设置为Visible = false,因为该按钮不会出现在生成的标记中.而是将按钮的样式设置为display:none; 所以没有人会看到这个黑客,因为是的,这就是这个解决方法 - 它是一个黑客,但简单,就像你期望的那样有效.不要忘记从RBL中删除Autopostback ="True".

CAVEAT:因为我正在使用黑客按钮进行onclick事件,所以用户可以单击RBL区域,但实际上不能选择项目.当发生这种情况时,我们的onclick会触发AsyncPostBack,并且会处理代码隐藏逻辑,所以请记住这一点.为了让您了解我的意思:将调用所有Page_Load()事件,但如果他们碰巧在RBL区域中单击而没有实际选择项目,则不会调用rbl_Questions_SelectedIndexChanged().出于我的目的,这不会导致我的逻辑出现问题,也不会对用户产生任何影响.

这是代码:

某处.Aspx页面:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">  
    <ContentTemplate>

        <asp:RadioButtonList ID="rbl_Questions" runat="server"
            OnSelectedIndexChanged="rbl_Questions_SelectedIndexChanged">
        </asp:RadioButtonList>

        <asp:Button ID="btn_rbl_Questions" runat="server" style="display:none;"/>

        <asp:Label ID="lbl_Result" runat="server" Text="" Visible="false">
        </asp:Label>
    </ContentTemplate>  
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

在Page_Load()事件中:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        //Instead of using the AutoPostback of the RBL, use this instead.
        rbl_Questions.Attributes.Add("onclick",
            "document.getElementById('"
            + btn_rbl_Questions.ClientID
            + "').click();");
        //Bind your RBL to a DataSource, add items programmatically,
        //  or add them in the aspx markup.
    }
}
Run Code Online (Sandbox Code Playgroud)

在rbl_Questions_SelectedIndexChanged()事件中:

protected void rbl_Questions_SelectedIndexChanged(object sender, EventArgs e)
{
    //Your code here.
    //My code unhid the lbl_Result control and set its text value.
}
Run Code Online (Sandbox Code Playgroud)


更新05/24/2011

上面的" 黑客 "已经不再需要了(我将其留在上面,因为这是作者的答案).我找到了最好的方法来做到这一点,多亏了这个答案:
Updatepanel提供完整的回发而不是asyncpostback

代码现在简单得多,只需删除我放在Page_Load()方法中的内容并删除我在Aspx页面中使用的Button,并将ClientIDMode ="AutoID"和AutoPostBack ="True"添加到您希望UpdatePanel捕获的控件中.

某处.Aspx页面:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">  
    <ContentTemplate>

        <asp:RadioButtonList ID="rbl_Questions" runat="server"
            ClientIDMode="AutoID" AutoPostBack="true"
            OnSelectedIndexChanged="rbl_Questions_SelectedIndexChanged">
        </asp:RadioButtonList>

        <asp:Label ID="lbl_Result" runat="server" Text="" Visible="false">
        </asp:Label>
    </ContentTemplate>  
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

MS将.net 4.0中生成ClientID的方式从"AutoID"更改为"Predictable",我猜测ScriptManager或UpdatePanel未正确更新以使用它.我无法找到有关为什么会出现这种情况的文档,或者它是否被设计保留.

  • 希望我能+2这个.黑客+1给我笑,+1为理智的方式! (3认同)
  • 我建议您将正确的解决方案放在答案的顶部,这样人们就不必阅读不必要的hacky部分,而该部分可能只是出于历史原因。 (2认同)