DropDownList only selects the first value

倖福魔咒の 提交于 2019-12-12 01:44:14

问题


I have a problem with my DropDownList. after reading alot of post here i still cant make it work so ill ask.

this is my C# code:

protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Newsletter;Integrated Security=True"))
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("Select Email From Newsletter", connection);
        EmailList.DataSource = cmd.ExecuteReader();
        EmailList.DataTextField = "Email";
        EmailList.DataBind();
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Vælg_Click(object sender, EventArgs e)
{
    EmailListe  .Text = EmailList.Text;
}

Here is a my Asp code:

<asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
    <asp:Button runat="server" ID="Vælg" Text="Vælg Email" OnClick="Vælg_Click" />
    <asp:TextBox runat="server" ID="EmailListe" TextMode="MultiLine" />
    <asp:TextBox ID="Besked" runat="server" />

As you can see I get the DropDownList value from my SqlDatabase. When I select an Email in the dropdownlist and click the button then it ALWAYS add the first Value to the textbox, even if I select another Email.

what am i doing wrong?


回答1:


Just remember how ASP.NET works, Page_Load is called before your event handler for each server roundtrip then list is refreshed to default value. Just check it's not a postback:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        return;

    // Your code here
}

Edit: a small suggestion; you're not disposing SqlCommand and SqlReader, you should extract values and dispose them as soon as possible to free resources. This way they'll be collected by GC and this may be a big problem, especially if your site has heavy traffic...




回答2:


Use the EmailList.SelectedValue; property, which gives you the selected value of the dropdown list.

Also, in your Page_Load check for postback, so you are not adding duplicate entries to your dropdown every time the page loads.

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
    using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Newsletter;Integrated Security=True"))
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("Select Email From Newsletter", connection);
        EmailList.DataSource = cmd.ExecuteReader();
        EmailList.DataTextField = "Email";
        EmailList.DataBind();
    }
   }
}



回答3:


Seems like you don't need a round trip to the server to do what you are trying to do. Plain JavaScript:

<script>
    function setEmail() {
        var list = document.getElementById("<%= EmailList.ClientID %>");
        var textBox = document.getElementById("<%= EmailListe.ClientID %>");
        textBox.value = list.options[list.selectedIndex].text;
    }
</script>

<asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<button onclick="setEmail(); return false;">Vælg Email</button>
<asp:TextBox runat="server" ID="EmailListe" TextMode="MultiLine" />


来源:https://stackoverflow.com/questions/21171623/dropdownlist-only-selects-the-first-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!