简体   繁体   English

在Repeater控件中处理RadioButtonList中的null

[英]Handling nulls in RadioButtonList in Repeater control

We have the following RadioButtonList in our Repeater control: 我们的Repeater控件中具有以下RadioButtonList:

<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 />

Our normal business process requires a user to enter an account number to check the existence of records associated with that account number. 我们的正常业务流程要求用户输入一个帐号,以检查与该帐号相关联的记录是否存在。

If records exist, Repeater control form is populated with those records. 如果存在记录,则用这些记录填充“ Repeater”控件表单。

Users can then make whatever modifications they wish to make. 然后,用户可以进行他们希望进行的任何修改。

This part works great. 这部分效果很好。

If no record exists, user is then required to enter his/her information and submit to the database. 如果没有记录,则要求用户输入他/她的信息并提交到数据库。

This is where we are having problem. 这就是我们遇到的问题。

This an issue because if no values from the rdlmhorsepType already exists on the database, the following error is raised: 这是一个问题,因为如果数据库中不存在来自rdlmhorsepType值,则会引发以下错误:

'rdlmhorsepType ' has a SelectedValue which is invalid because it does not exist in the list of items. “ rdlmhorsepType”的SelectedValue无效,因为它不存在于项目列表中。 Parameter name: value 参数名称:值

Since there are two values in the rdlmhorsepType RadioButtonList control, I used Electric Start as the default value to eliminate the error. 由于rdlmhorsepType RadioButtonList控件中有两个值,因此我将Electric Start作为默认值来消除该错误。

Problem with this is that Electric Start always gets inserted into the database and is always displayed as the selected value. 问题在于, Electric Start总是插入到数据库中,并始终显示为所选值。

I attempted to use 0 as in: 我试图在其中使用0

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

but it throws same error. 但它会引发相同的错误。

Any ideas how to fix this error which allows the display of the correct selected value from the database? 有什么想法可以解决这个错误,该错误允许从数据库中显示正确的所选值吗?

That error by the way, points to this database table is used for initializing DataTable values: 顺便指出,该错误指向该数据库表用于初始化DataTable值:

        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();
    }
}

Add Value attribute to list item like below code Value属性添加到列表项,如下面的代码

 <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 />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM