简体   繁体   English

下拉控制选定的索引更改事件

[英]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. 在asp.net的webfrom中,我有一个网格视图,一个按钮,一个文本框和一个下拉列表。 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. 我在下拉列表中选择索引更改事件的形式中调用fillGridByAuthor方法,就像这样。

 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 : 来自MSDN

The Text property gets and sets the same value that the SelectedValue property does. Text属性获取并设置SelectedValue属性所执行的相同值。 The SelectedValue property is commonly used to determine the value of the selected item in the ListControl control. SelectedValue属性通常用于确定ListControl控件中所选项的值。 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. 因此Text属性返回Value而不是当前所选项的Text属性。 Use SelectedItem.Text instead. 请改用SelectedItem.Text

fillGridByAuthor(DDAuthor.SelectedItem.Text);

Try adding autopostback = true to your dropdownlist. 尝试将autopostback = true添加到下拉列表中。 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: Tim Schmelter可能更好,因为你想要这样的文字:

fillGridByAuthor(DDAuthor.SelectedItem.Text);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net中下拉列表的选定索引更改事件 - Selected Index change event of drop down list in asp.net 如何处理MVC4 Razor中下拉列表的选定索引更改事件 - How to handle selected-index change event of a drop down in MVC4 Razor 在下拉框的选定索引更改事件触发后面的代码中实现javascript确认框 - Implementing javascript confirm box in code behind on drop down box's selected index change event firing 使用 on change 事件将下拉列表的选定项目链接到 sql 脚本 - Link the selected item of drop down list to sql script with an on change event 如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性? - How to change drop down list selected index and visibility using another drop down list in asp.net? 在下拉选择的索引更改事件中找到子gridview的控件 - Find control of child gridview in a dropdown selected index change event 获取选定的下拉列表索引 - Getting the selected index of drop down list 动态下拉列表的选定“索引已更改”事件始终返回第一个值 - Dynamic drop down list's selected Index Changed event always return the first value 不会针对在运行时创建的下拉列表触发选定的索引更改事件 - doesn't fire selected index changed event for a drop down list created on run time 更改所选下拉索引时,更改当前行的名称列文本 - change the name column text of the current row when the selected index of drop down is changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM