Reputation: 221
I have a aspx page like this:
<%@ Page Language="C#" CodeBehind="xxxx.aspx.cs" Inherits="xxxx" %>
<!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 id="Head1" runat="server">
<title>How to use DropDownList OnSelectedIndexChanged event</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">DropDownList: OnSelectedIndexChanged</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
ForeColor="DodgerBlue"
Font-Size="Large"
>
</asp:Label>
<asp:DropDownList
ID="drop1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="GetLocationDetails"
BackColor="Crimson"
ForeColor="FloralWhite"
onclientselectedindexchanged="return false;"
>
</asp:DropDownList>
</div>
</form>
</body>
</html>
The value for the dropdown list "drop1" is populated from the database from the page "xxxx.aspx.cs". When i select the value from the drop down box a function called "GetLocationDetails" is called from the aspx.cs. When i press a value from drop down box, the page is refreshed and the first value is only selected. please help out.
Upvotes: 0
Views: 126
Reputation: 5430
Bind your dropdown list in page load event:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Bind here or call method that binds dropdownlist
}
}
AutoPostBack
is set true
which causes whole page to be posted back to server.
If you don't track IsPostBack
of the page
your dropdownlist is binded again.
Upvotes: 1
Reputation: 7463
your AutoPostBack="true"
in the drop down menu causes your page to submit a request each time you change a value. and since you populate the drop down each page load, it will reset everything.
in your code behind, in page load, wrap your populating code with:
if(!IsPostBack)
{
}
this will cause the drop down to be only populated on first page load and not on postbacks.
Upvotes: 0