2017-09-14 80 views
0

我們已經在我們Repeater控件以下單選按鈕列表:在Repeater控件處理空值的單選按鈕列表

<asp:RadioButtonList ID="rdlmhorsepType" Text='<%#string.IsNullOrEmpty((string)Eval("rdlmhorsepType")) ? "Electric Start" : Eval("rdlmhorsepType") %>' runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;" AutoPostBack="true" OnSelectedIndexChanged="rblPurchaseType_SelectedIndexChanged"> 
    <asp:ListItem Text="Electric Start" /> 
    <asp:ListItem Text="Recoil" /> 
</asp:RadioButtonList><br /> 

我們正常的業務流程需要用戶輸入帳號,檢查與該帳號相關記錄是否存在。

如果存在記錄,則使用這些記錄填充Repeater控件表單。

用戶隨後可以進行任何他們想做的修改。

這部分工作很好。

如果沒有記錄存在,用戶需要輸入他/她的信息並提交給數據庫。

這是我們遇到問題的地方。

這一個問題,因爲如果沒有從rdlmhorsepType值的數據庫已經存在,以下引發錯誤:

「rdlmhorsepType」具有的SelectedValue這是無效的,因爲它沒有在列表中存在項目。參數名:價值

既然有在rdlmhorsepType RadioButtonList控件兩個數值,我用Electric Start作爲默認值,以消除誤差。

問題是,Electric Start總是被插入到數據庫中,並始終顯示爲選定的值。

我試圖使用0爲:

Text='<%#string.IsNullOrEmpty((string)Eval("rdlmhorsepType")) ? "0" : Eval("rdlmhorsepType") %>' 

但它拋出同樣的錯誤。

任何想法如何解決這個錯誤,它允許從數據庫中顯示正確的選定值?

順便說一下該錯誤,指出該數據庫表用於初始化數據表中的值:

 dr = dt.NewRow(); 
     dr["RowNumber"] = 1; 
     dr["rdlmhorsepType"] = string.Empty; 
     dr["rblIssues"] = string.Empty; 
     dr["vesseltypeUse"] = string.Empty; 
     dt.Rows.Add(dr); 
    } 
    else 
    { 
     dt = (DataTable)ViewState["CurrentTable"]; 
    } 
    //Store the DataTable in ViewState for future reference 
    ViewState["CurrentTable"] = dt; 
    if (dt.Rows.Count > 0) 
    { 
     //Bind the Repeater with the DataTable 
     Repeater2.DataSource = dt; 
     Repeater2.DataBind(); 
    } 
} 
+0

嘗試改變列表項值由'0/1'轉換爲'true/false'。 – AsifAli72090

回答

0

添加屬性列表項如下面的代碼

<asp:RadioButtonList ID="rdlmhorsepType" Text='<%#string.IsNullOrEmpty((string)Eval("rdlmhorsepType")) ? "Electric Start" : Eval("rdlmhorsepType") %>' runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;" AutoPostBack="true" OnSelectedIndexChanged="rblPurchaseType_SelectedIndexChanged"> 
     <asp:ListItem Value="0" Text="Electric Start" /> 
     <asp:ListItem Value="1" Text="Recoil" /> 
    </asp:RadioButtonList><br />