简体   繁体   English

Asp.Net下拉列表

[英]Asp.Net Dropdownlist

I Bind the Dropdownlist at runtime and data is populated to the database. 我在运行时绑定下拉列表,并将数据填充到数据库中。 Its ok Fine. 好的。 But if i want to select the particular value and display in a message box. 但是,如果我想选择特定的值并显示在消息框中。 It only shows the default value. 它仅显示默认值。

Here My code is: 这里我的代码是:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("You have selected " + DropDownList1.SelectedItem.Value);
  }

So how can i display the selected value in the message box. 因此,如何在消息框中显示选定的值。 Here i am very new. 在这里我很新。 Please help me. 请帮我。

The problem is in your Page_load event, where you are assigning your Datasource . 问题出在您的Page_load事件中,您在其中分配Datasource When you click the button, Page_load will be called again and it will rebind to your dropdown again. 当您单击按钮时, Page_load将再次被调用,并将再次重新绑定到您的下拉菜单中。

It should be: 它应该是:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Set your dropdown datasource here...
    }
}
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Response.Write("<script>alert('You have selected ' + '" + DropDownList1.SelectedValue + "')</script>")
End Sub

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

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