【问题标题】:how to set the OnSelectedIndexChanged in C# codebehind instead of ASP.net如何在 C# 代码隐藏而不是 ASP.net 中设置 OnSelectedIndexChanged
【发布时间】:2013-06-25 21:43:33
【问题描述】:

所以在 ASP.NET 中我会简单地这样做:

    <asp:ListBox OnSelectedIndexChanged="UpdateModels" runat="server" > </asp:ListBox>

但我的列表框位于 .ascx 文件中。像这样注册并包含在我的实际网页 massupdate.aspx 中

<%@ Register TagPrefix="mass" TagName="make" Src="~/DynamicData/Make.ascx" %>
<mass:make id="makeControl" runat="server"/>

在我的 make.ascx.cs 我有这个

   public ListBox getlistbox()
    {

        return DropDownList1;

    }

所以我可以访问原始列表框,但我不知道如何在 mass.update.aspx 的代码隐藏中复制 C# 中的第一个代码 sn-p。

【问题讨论】:

    标签: c# asp.net listbox ascx selectedindexchanged


    【解决方案1】:

    首先,在 ascx 中注册的事件应该触发,即使它被注册为页面上的控件。事件应该通过页面上的所有控件来冒泡(或者可能会减少?),这是网络表单“魔力”的一部分。

    其次,如果您确实需要在代码中设置事件处理程序,您可以谷歌“添加事件处理程序 .NET 控件”,或者您可以在下面键入类似的内容,Visual Studio intellisense 应该在您键入后自动为您完成它"+=" 你可以从那里拿走它。

    makeControl.getlistbox().OnSelectedIndexChanged += this.Index_Changed
    
    void Index_Changed(Object sender, EventArgs e) {
        // whatever you need to do on the event
    }
    

    【讨论】:

    • 谢谢。在工作中很难学会 C#。我的背景是 Java,那里的事件处理完全不同。我正在尝试 MakeListBox.SelectedIndexChanged += UpdateModels();和其他东西。我很接近......好吧,无论如何,非常感谢你。
    • 没问题,如果您已经了解 java,我相信您的语法担忧很快就会消失。祝你好运
    猜你喜欢
    • 2016-07-06
    • 2011-03-21
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多