1

我在我工作的公司中有一个用 C# 编写的内部 Web 服务 (ASP.NET)。其中只有 2 页,其中一页包含 DropDownList。每次当用户从该 DropDownList 中选择一个项目时,我都需要以某种方式将所选项目值传递给一个静态方法,并在页面上的任何位置显示该方法的结果字符串。

我以前从未使用过 ASP.NET 或任何 Web 编程,对如何做有点困惑,甚至不知道从哪里开始。

4

4 回答 4

1

在你的 aspx 文件中,你应该有这个:

 <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ListBox1_SelectedIndexChanged"></asp:ListBox>

请注意AutoPostBack="True"在用户更改列表框中的选择后立即返回服务器并触发 selectedindexchanged 事件

在您的代码隐藏(.cs 文件)中,您应该有这个:

 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        // Call static method and pass the ListBox1.SelectedIndex
        // MyStaticMethod(ListBox1.SelectedIndex);
    }
于 2012-05-31T13:38:44.233 回答
0

您可能应该查看微软为新的 .NET 开发人员提供的一些重要资源。它们将非常有助于您入门。她是一些非常好的视频的链接,可以帮助你:http ://www.asp.net/web-forms/videos

不确定你来自什么语言,如果有的话......但在大多数情况下,网络表单将像其他基于网络的方法一样工作。

您的 ASP.NET 控件(在您的情况下为 DropDownList)具有客户端和服务器端事件。

您可能希望在 DropDownList 上映射服务器端 OnSelectedIndexChanged 事件。

为了在该控件上进行回发,您需要在 DropDownList 上将 AutoPostBack 属性设置为 true。

于 2012-05-31T13:37:43.997 回答
0

试试这个

在 html 中,

        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

在 aspx.cs 页面中,,

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string selctedValue = DropDownList1.SelectedValue;
        /// Call yours static methid here
     YourMethod(selctedValue);
}
于 2012-05-31T15:11:41.037 回答
0

您可以选择 autoPostBack="true" 并在服务器端处理更改事件,或者使用 jQuery 订阅更改事件并在客户端获取值

于 2012-05-31T13:27:40.220 回答