I have a method that fires when the dropdown list event OnSelectedIndexChanged is invoked.
When the page redirects it doesn't pass the id value? The url only shows:
http://localhost/Lobbyist/statedetails.aspx?id=
So, the value isn't passed.
I set a break point and the watch window indicates that the id variable is 0?
Here is the code in my aspx page:
<asp:DropDownList ID="state" runat="server" OnSelectedIndexChanged="state_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
Here is the code-behind:
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
state.DataSource = StateDAO.GetStateList();
state.DataValueField = "PK";
state.DataTextField = "name";
state.DataBind();
state.Items.Insert(0, new ListItem("-- Please Select --", ""));
}
protected void state_SelectedIndexChanged(object sender, EventArgs e)
{
string id = state.SelectedValue.ToString();
if (id != "0")
{
Response.Redirect("~/statedetails.aspx?id=" + id);
}
}
}
What am I missing? How can I pass the id value? I don't understand why the value isn't passed.
Any help is appreciated.
Thanks.
Page_Load will fire before the OnSelectedIndexChanged event will fire, so the DropDownList is being re-databound and the selected value is being lost.
Only bind the DropDownList the first time a page is loaded, and not on a post
in Page_Load the code should be
If ( ! Page.IsPostBack)
{
state.DataSource = StateDAO.GetStateList();
state.DataValueField = "PK";
state.DataTextField = "name";
state.DataBind();
state.Items.Insert(0, new ListItem("-- Please Select --", ""));
}
Thank you Steve...that corrected the problem.
Regards.
0 comments:
Post a Comment