簡體   English   中英

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

[英]Drop Down Control Selected Index change event

在asp.net的webfrom中,我有一個網格視圖,一個按鈕,一個文本框和一個下拉列表。 我有一個這樣的方法來調用並選擇我的網格視圖中的數據。

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

Text屬性獲取並設置SelectedValue屬性所執行的相同值。 SelectedValue屬性通常用於確定ListControl控件中所選項的值。 如果未選擇任何項目,則返回空字符串(“”)。

因此Text屬性返回Value而不是當前所選項的Text屬性。 請改用SelectedItem.Text

fillGridByAuthor(DDAuthor.SelectedItem.Text);

嘗試將autopostback = true添加到下拉列表中。 它可能會有所幫助

而且,你應該這樣做:

fillGridByAuthor(DDAuthor.SelectedValue);

編輯

Tim Schmelter可能更好,因為你想要這樣的文字:

fillGridByAuthor(DDAuthor.SelectedItem.Text);

只需將下拉列表的AutoPostBack屬性設置為true,它就像魅力一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM