Reputation: 27051
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?
Upvotes: 0
Views: 4666
Reputation: 91677
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" />
Upvotes: 0
Reputation: 67148
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...
Upvotes: 5
Reputation: 5689
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();
}
}
}
Upvotes: 0