2013-11-02 12 views
0

我在點擊asp.net gridview上的編輯按鈕時出現此錯誤。ASP.NET DropDown列表選定值錯誤'ddldept'有一個無效的選項,因爲它不存在於項目列表中

'ddldept'有一個SelectedValue,它是無效的,因爲它不存在於項目列表中。

這是我的下拉的SelectedIndexChanged事件

protected void dgbus_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = dgbus.SelectedRow; 

    //here am getting the error 
    ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim(); 
    txtappcode1.Text = row.Cells[3].Text.ToString(); 
    txtappcode2.Text = row.Cells[4].Text.ToString(); 

    hdn.Value = row.Cells[1].Text.ToString(); 

    lblMsg.Text = ""; 
} 

eveything工作正常,但是當我嘗試編輯具有row.Cells[2].Text爲「財經&帳戶」行.. 這是「&」問題?

我的GridView標記

<asp:GridView ID="dgbus" runat="server" class="table-format" 
       AutoGenerateColumns="False" Width="100%" 
       OnSelectedIndexChanged="dgbus_SelectedIndexChanged" 
       OnRowCreated = "dgbus_RowCreated" 
       OnPageIndexChanging = "dgbus_PageIndexChanging" AllowPaging="True" 
       PageSize="50"> 
    <Columns> 
      <asp:CommandField ButtonType="Image" HeaderText="Edit Details" 
          SelectImageUrl="~/images/modify.gif" 
          SelectText="Modify Approver" 
          ShowSelectButton="True" /> 
      <asp:BoundField HeaderText="Approver ID" DataField="appid"/> 
      <asp:BoundField HeaderText="Deptt" DataField="deptt" /> 
      <asp:BoundField HeaderText="Appcode1" DataField="appcode1" /> 
      <asp:BoundField HeaderText="Appcode1" DataField="appcode2" /> 
    </Columns> 
    <RowStyle CssClass="misctext" Height="20px" /> 
    <HeaderStyle BackColor="ControlLight" CssClass="contentbold" 
        Height="20px" /> 
    <PagerSettings PageButtonCount="5" /> 
    <PagerStyle BackColor="Gainsboro" CssClass="link" HorizontalAlign="Right" 
       VerticalAlign="Middle" /> 
</asp:GridView> 
+0

對不起,這裏是我的gridview的selectedindexchanged ddldept.SelectedValue = row.Cells [2]。 Text.ToString()。Trim(); //這裏我得到錯誤.. txtappcode1.Text = row.Cells [3] .Text.ToString(); txtappcode2.Text = row.Cells [4] .Text.ToString(); hdn.Value = row.Cells [1] .Text.ToString(); (例外c) // { // lblMsg.Text = c.Message; //} lblMsg.Text =「」; } –

回答

0

嘗試嘗試將SelectedValue,這樣才找到在下拉列表中選擇值:

if (ddldept.Items.FindByValue(row.Cells[2].Text.ToString().Trim()) != null) 
{ 
    ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim(); 
} 

所以你完整的代碼應該是這樣的:

protected void dgbus_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = dgbus.SelectedRow; 

    if (ddldept.Items.FindByValue(row.Cells[2].Text.ToString().Trim()) != null) 
    { 
     ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim(); 
    }   

    txtappcode1.Text = row.Cells[3].Text.ToString(); 
    txtappcode2.Text = row.Cells[4].Text.ToString(); 

    hdn.Value = row.Cells[1].Text.ToString(); 

    lblMsg.Text = ""; 
} 
相關問題