简体   繁体   中英

Drop Down Control Selected Index change event

In my webfrom in asp.net I have a grid view a button, a text box and a Dropdownlist. I have a method like this to call and select the data in to my grid view.

public void fillGridByAuthor(string searchKey)
{
    GVDetails.DataSource = new ViewAllBKByAuthorOP().searchAuthorByAUNM(searchKey);
    GVDetails.DataBind();
}

This is my business layer method.

 public DataTable searchAuthorByAUNM(string searchKey)
{
    string query2 = "EXEC SelectBooksDTByAuthor'" + searchKey + "'";
    return new DataAccessLayer().executeTable(query2);
}

I'm calling fillGridByAuthor method in form in the drop downlist selected index change event like this.

 protected void DDAuthor_SelectedIndexChanged(object sender, EventArgs e)
 {
    fillGridByAuthor(DDAuthor.Text);

 }

and in the button click event like this

 protected void btnSearch_Click(object sender, EventArgs e)
 {
     fillGridByAuthor(txtAuName.Text);

 }

It is working fine when the button is clicked. Though I select the same Item in the drop down list, it doesn't give me the same output. What's incorrect here?

From MSDN :

The Text property gets and sets the same value that the SelectedValue property does. The SelectedValue property is commonly used to determine the value of the selected item in the ListControl control. If no item is selected, an empty string ("") is returned.

So the Text property returns the Value not the Text property of the currently selected item. Use SelectedItem.Text instead.

fillGridByAuthor(DDAuthor.SelectedItem.Text);

Try adding autopostback = true to your dropdownlist. It will probably help

And, you should do this:

fillGridByAuthor(DDAuthor.SelectedValue);

EDIT

what Tim Schmelter is probably better because you want the text so:

fillGridByAuthor(DDAuthor.SelectedItem.Text);

只需将下拉列表的AutoPostBack属性设置为true,它就像魅力一样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM