问题描述
我有一个绑定到LINQ数据源的DropDownList.此下拉列表显示除何时状态在数据库中设置为false之外的所有Bowzer号.
假设我有一个早些时候创建的记录,现在我想编辑现在设置为false的Bowzer.我有这个例外抛出,我不知道如何处理它.
'bowzerdl'具有一个无效的选定value,因为它在项目列表中不存在. 参数名称:值
这是我的linqdatasource
的代码<asp:LinqDataSource ID="LinqBowzerDS" runat="server" ContextTypeName="ShippingWeb.ShippingDbDataContext" EntityTypeName="" Select="new (bowzer_id, bowzer_no)" TableName="Bowzers" OrderBy="bowzer_no" Where="expiry_date >= DateTime.Now && status==True">
一旦我点击详细信息的编辑按钮,我的下拉列表就是我之前提到的例外情况.有没有办法我可以抓住这个例外吗?在编辑后,仅显示那些状态为真实的记录?
以下是我的详细信息的代码
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" DataSourceID="LinqDataSource1" Height="50px" Width="363px" DataKeyNames="challan_id" ForeColor="#333333" GridLines="None" ondatabound="DetailsView1_DataBound" onmodechanged="DetailsView1_ModeChanged" >
<asp:TemplateField HeaderText="Bowzer No"> <EditItemTemplate> <asp:DropDownList ID ="bowzerddl" runat="server" DataSourceID="LinqBowzerDS" DataTextField="bowzer_no" DataValueField="bowzer_id" selectedValue='<%# bind("bowzer_id") %>' AppendDataBoundItems="true" > <asp:ListItem Selected="True" Value="">(None)</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblbowzer" runat="server" Text='<%# Eval("Bowzer.Bowzer_no") %>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Fields> <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> </asp:DetailsView>
推荐答案
我从这个链接提起答案no 16
这适用于我们:
protected void PreventErrorOnbinding(object sender, EventArgs e) { dropdownlist theDropDownList = (DropDownList)sender; theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); theDropDownList.AppendDataBoundItems = true; ListItem li = new ListItem("Make a selection >>",""); theDropDownList.Items.Insert(0, li); try { theDropDownList.DataBind(); } catch (ArgumentOutOfRangeException) { theDropDownList.SelectedValue = ""; } }
在控制上,您可以添加ondatabinding ="walleerroronbinding"
由于所选值设置为""它始终选择射击 dropdownlist中的项目.
问题描述
I have a dropdownlist that is bound to a linq data source. This drop down list shows all the bowzer numbers except those whos status is set to false in the database.
Suppose I have a record which was created earlier and now i want to edit the bowzer that is now set to false. I am thrown with this exception and i don't know how to deal with it.
'bowzerddl' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Here is the code for my LinqDataSource
<asp:LinqDataSource ID="LinqBowzerDS" runat="server" ContextTypeName="ShippingWeb.ShippingDbDataContext" EntityTypeName="" Select="new (bowzer_id, bowzer_no)" TableName="Bowzers" OrderBy="bowzer_no" Where="expiry_date >= DateTime.Now && status==True">
As soon as I click on EDIT button of the detailsView, where my dropdownlist is, I am given an exception that i've mentioned earlier. Is there any way i can catch this exception? and upon edit, show only those records who's status is True?
Here is the code for my detailsView
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" DataSourceID="LinqDataSource1" Height="50px" Width="363px" DataKeyNames="challan_id" ForeColor="#333333" GridLines="None" ondatabound="DetailsView1_DataBound" onmodechanged="DetailsView1_ModeChanged" >
<asp:TemplateField HeaderText="Bowzer No"> <EditItemTemplate> <asp:DropDownList ID ="bowzerddl" runat="server" DataSourceID="LinqBowzerDS" DataTextField="bowzer_no" DataValueField="bowzer_id" selectedValue='<%# bind("bowzer_id") %>' AppendDataBoundItems="true" > <asp:ListItem Selected="True" Value="">(None)</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblbowzer" runat="server" Text='<%# Eval("Bowzer.Bowzer_no") %>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Fields> <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> </asp:DetailsView>
推荐答案
I solved it from this link Answer No 16
This worked for us:
protected void PreventErrorOnbinding(object sender, EventArgs e) { dropdownlist theDropDownList = (DropDownList)sender; theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); theDropDownList.AppendDataBoundItems = true; ListItem li = new ListItem("Make a selection >>",""); theDropDownList.Items.Insert(0, li); try { theDropDownList.DataBind(); } catch (ArgumentOutOfRangeException) { theDropDownList.SelectedValue = ""; } }
On the control(s) you add ondatabinding="PreventErrorOnbinding"
Since the selected value is set to "" it always selects the firts item in the dropdownlist.