Pergunta

I have set selected item of the DropDownList in PageLoad.

protected void Page_Load(object sender, EventArgs e)
{

strSelect2 = "SELECT * FROM [Order] WHERE orderId = '" + selOrder + "'";
cmdSelect3 = new SqlCommand(strSelect2, conNWind);
conNWind.Open();
dtrReader = cmdSelect3.ExecuteReader();

if (dtrReader.Read())
{
DropDownList1.Text = dtrReader["status"].ToString();
}
conNWind.Close();

After that, I have another function to retrieve the ID of the selected item in the DropDownList.

String cmd1 = "Select * from [Status] WHERE statusName = @statusName";
SqlCommand cmdSelectCat = new SqlCommand(cmd1, a);
a.Open();

cmdSelectCat.Parameters.AddWithValue("@statusName", DropDownList1.SelectedValue);
dtrReader = cmdSelectCat.ExecuteReader();

if (dtrReader.Read())
{
statusId = dtrReader["statusId"].ToString();
}
a.Close();

When I choose another item in the DropDownList, I try to print on a label using a function on a button.

Label9.Text = DropDownList1.SelectedIndex.toString();

But the statusId I get is the ID where the DropDownList select upon PageLoad. How can I get the value of the selected item but not the selected item in the page load? Beside's using IsPostBack

Foi útil?

Solução

The short answer is that you're trying to combine server-side and client-side code. You've got a global setup and load via ASP.Net, then the 'on-change' of the Selection List is doing some kind of postback to call a server-side function.

You're setting it up to do a request to the server, so it reloads the entire page. And resets the selection in the box, too, probably. Or doesn't do anything. Regardless, you have weird functionality because you're trying to mix functionality between client and server.

I suggest you debug through putting a stop point at the start of your Page_Load() and the start point of your function and seeing what exactly the server is doing after you change the value. Most likely you will be surprised at the order of operations.

To do what I think you're trying to do, I think you'll probably have to use either IsPostBack or Javascript. Sorry.

I suggest you look at this post: ASP.NET DropDownList OnSelectedIndexChanged event not fired I think that there might be some help for you inside.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top