0
我有以下代碼試圖填充下拉列表中,當我點擊一個網格視圖編輯,它給我下面的錯誤:試圖來填充GridView的編輯下拉列表
" 'ddlgvRoom' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
任何關於爲什麼我需要將代碼添加到行編輯事件中的想法,如果有,您可以提供幫助嗎?我的gridview從一個objectdatasource獲取它的值。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
DropDownList dl = (DropDownList)e.Row.FindControl("ddlgvRoom");
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlgvRoom = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlgvRoom");
string strgvRoom = ddlgvRoom.SelectedItem.Text.ToString();
DropDownList ddlgvJack = (DropDownList)
GridView1.Rows[e.RowIndex].FindControl("ddlgvJack");
string strgvJack = ddlgvJack.SelectedItem.Text.ToString();
DropDownList ddlgvVlan = (DropDownList)
GridView1.Rows[e.RowIndex].FindControl("ddlgvVlan");
string strgvVlan = ddlgvVlan.SelectedItem.Text.ToString();
GridView1.DataBind();
}
}
在哪一行,你有錯誤?試着抓住代碼並捕獲異常,然後更詳細地解釋我們。在GridView1_RowDataBound中你現在正在做的事情沒有意義,因爲一旦你得到它,你就不會對dl對象做任何事情。 –
感謝您的迴應,我試圖填充我的ddlgvRoom沒有錯誤只是說選定的值不存在,但是當我調試它的值顯示就好了。似乎一旦我點擊gridview編輯按鈕,就會丟失值 – Tim