Friday, 23 August 2013

DrowDownList not keeping the selected item after postback

DrowDownList not keeping the selected item after postback

I have a dropdownlist control and a button in asp.net page. The
dropdownlist is populated from an object datasource. If I select any item
other than the first item, after clicking the button, I lose the selected
item in the DDL and it selects the first item and also I am getting the
value of the first item only in the button click event. How can I fix the
problem?
enter code here
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ODCustomers" runat="server"
SelectMethod="GetCustomers"
TypeName="CC"></asp:ObjectDataSource>
Show orders by customer
<asp:DropDownList ID="userDropDown" runat="server"
DataSourceID="ODCustomers" DataTextField="CustomerName"
DataValueField="CustomerId">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />
</div>
</form>
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(userDropDown.SelectedValue.ToString());
}

No comments:

Post a Comment