Gridview EditTemplate DropDownlist
Error is ddlgvRooms' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
I read a lot of people having issues with this particular problem but non of the fixes have worked for my case.I have tried a few different things such as setting appenddatabounditems="true" also tried to set in the itemcollection a default null value. Most of the forums post on this that I read were from a couple years ago im hoping they fixed this bug already and I am just overlooking something.
I am trying to get my cascading dropdownlist to work in a gridview edittemplate fields. I created these in my detailsview on insert everything works great.
MySetup Basically I have a webmethod that has 2 functions getRooms and getJacks that are supposed to grab the data from the 2 datasets that I have created.The Datasets get their data from a couple of SQLSTOREDPROCEDURES.
My aspx page dropdownlist and AjaxCDDL looks like this
<EditItemTemplate>
<asp:DropDownList ID="ddlgvRooms" runat="server"
SelectedValue='<%# Bind("intRoom") %>'>
</asp:DropDownList> <asp:CascadingDropDownID="ddlgvRooms_CascadingDropDown"
runat="server"
Enabled="True"
TargetControlID="ddlgvRooms"
Category="Jack"
ServiceMethod = "GetRooms"
ServicePath = "CascadingDropDownRooms.asmx"
LoadingText = "[Loading Rooms...]"
PromptText="Please Select Room">
</asp:CascadingDropDown>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgvRoom" runat="server" Text='<%# Eval("intRoom") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JackNumber" SortExpression="intJack">
<EditItemTemplate>
<asp:DropDownList ID="ddlgvJacks" runat="server"
Height="20px" Width="125px">
</asp:DropDownList>
<asp:CascadingDropDown ID="ddlgvJack_CascadingDropDown"
runat="server"
Enabled="True"
Category="Jack"
ServiceMethod="GetJacks"
ServicePath="CascadingDropDownRooms.asmx"
TargetControlID="ddlgvJacks"
ParentControlID="ddlgvRooms"
LoadingText="[Loading Jacks...]"
PromptValue="Please Select A Jack"&g开发者_如何学Got;
</asp:CascadingDropDown>
</EditItemTemplate>
<WebMethod()> _
Public Function GetRooms(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim roomAdapter As New dsRoomsTableAdapters.roomlistTableAdapter()
Dim roomValues As New List(Of CascadingDropDownNameValue)()
For Each row As DataRow In roomAdapter.GetAllRooms()
roomValues.Add(New CascadingDropDownNameValue(row("RoomName").ToString(), row("intRoom").ToString()))
Next
Return roomValues.ToArray()
End Function
<WebMethod()> _
Public Function GetJacks(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
Dim jackid As Integer
If ((Not kv.ContainsKey("Jack")) Or (Not Int32.TryParse(kv("Jack"), jackid))) Then
Return Nothing
End If
Dim jackAdapter As New dsRoomJacksTableAdapters.jacklistTableAdapter()
Dim jackValues As New List(Of CascadingDropDownNameValue)()
For Each row As DataRow In jackAdapter.GetJacksByRoomId(jackid)
jackValues.Add(New CascadingDropDownNameValue(row("JackNumber").ToString(), row("intJack").ToString()))
Next
Return jackValues.ToArray()
End Function
The page is probably trying to set the selected value of your drop down list before it gets populated. Try leveraging the GridView.RowDataBound Event to first populate the options of the drop down list and then to set the selected value to the row's value of intRoom. If ddlgvRooms has a static list of options that can be determined before runtime, define them in the aspx page and then you should be able to do it how you are currently.
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();
There are two possible problems here:
- The DropDownList has not been populated at the time you're setting the SelectedValue;
- You haven't set DataTextField and DataValueField properties
In the markup:
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="TextColumn" DataValueField="ValueColumn" SelectedValue='<%# Eval("SelectedValueColumn") %>' ...>
In the OnRowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList selectList = e.Row.FindControl("DropDownList1") as DropDownList;
if (selectList != null)
{
selectList.DataSource = SomeDataSource; //your datasource
selectList.DataBind();
}
}
This worked for me. When populating the GridView, you should populate each DropDownList in the RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
numberFormatDA formatDA = new numberFormatDA();
DataTable mytable = new DataTable();
DataColumn formatIDcolumn = new DataColumn("fkNumberFormat");
DataColumn formatNameColumn = new DataColumn("numberFormat");
mytable.Columns.Add(formatIDcolumn);
mytable.Columns.Add(formatNameColumn);
DataSet ds = new DataSet();
ds = formatDA.getNumberFormatsDS();
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
TextBox txtSite = (TextBox)e.Row.FindControl("txtIDSite");
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlNumberFormat");
DataRow[] rows = ds.Tables[0].Select();
foreach (DataRow row in rows)
{
DataRow newrow = mytable.NewRow();
newrow["fkNumberFormat"] = row["idnumberFormat"];
newrow["numberFormat"] = row["numberFormat"];
mytable.Rows.Add(newrow);
}
ddl.DataSource = mytable;
ddl.DataTextField = "numberFormat";
ddl.DataValueField = "fkNumberFormat";
int numberFormatID = 0;
Label lblFormatID = (Label)e.Row.FindControl("numberFormatLabel");
numberFormatID = Int32.Parse(lblFormatID.Text);
ddl.SelectedValue = numberFormatID.ToString();
ddl.DataBind();
}
}
Hope this helps!
I have a very simple solution for your problem.
<asp:DropDownList ID="ddlgvRooms" runat="server" SelectedValue='<%# Bind("intRoom") %>'> <asp:ListItem Value="1">FirstRoom</asp:ListItem>
<asp:ListItem Value="2">SecondRoom</asp:ListItem>
</asp:DropDownList>
The above code will give you an error. Instead of SelectedValue = '<%# Bind("intRoom") %>'
change it to SelectedValue = '<%# Eval("intRoom") %>'
This works for sure.
精彩评论