c# - 下拉控件选中的索引更改事件

标签 c# asp.net dropdownbox

在我的 asp.net webfrom 中,我有一个 GridView 、一个按钮、一个文本框和一个下拉列表。 我有一个类似这样的方法来调用数据并将其选择到我的 GridView 中。

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

这是我的业务层方法。

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

我在下拉列表选择的索引更改事件中调用表单中的 fillGridByAuthor 方法,就像这样。

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

 }

在这样的按钮点击事件中

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

 }

单击按钮时它工作正常。虽然我在下拉列表中选择了相同的项目,但它并没有给我相同的输出。 这里有什么不正确的?

最佳答案

来自 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.

因此 Text 属性返回的是 Value 而不是当前所选项目的 Text 属性。请改用 SelectedItem.Text

fillGridByAuthor(DDAuthor.SelectedItem.Text);

关于c# - 下拉控件选中的索引更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480488/

相关文章:

c# - 输入 : Resolving an object by type _and_ registration name/identifier

c# - 为稍后提到的函数内的整数赋值时出现问题 - 范围问题 - 我认为

c# - Gridview Itemtemplate DropDownList 已启用

c# - 如何使用Autofac在构造函数中注入(inject)具体实现

c# - 页面加载的 PrincipalPermission

asp.net - BundleTransformer LibSass 找不到错误

c# - 添加 ListItems 导致错误 "Cannot have multiple items selected in a DropDownList"

excel - 数据验证下拉框箭头消失

javascript - 如何在 TIVO 设备上启动 asp.net 网站?

html - 如何从 ckeditor 中删除 HTML 标签到 textarea