[英]Asp.Net Dropdownlist
我在運行時綁定下拉列表,並將數據填充到數據庫中。 好的。 但是,如果我想選擇特定的值並顯示在消息框中。 它僅顯示默認值。
這里我的代碼是:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You have selected " + DropDownList1.SelectedItem.Value);
}
因此,如何在消息框中顯示選定的值。 在這里我很新。 請幫我。
問題出在您的Page_load
事件中,您在其中分配Datasource
。 當您單擊按鈕時, Page_load
將再次被調用,並將再次重新綁定到您的下拉菜單中。
它應該是:
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.