Wednesday, March 21, 2012

DropDownList dont work inside ReorderList InsertItemTemplate ?

In want to have a DropDownList inside the InsertItemTemplate of my ReorderList.

Like this:
<asp:DropDownList ID="NewRecipientsList" runat="server" Width="180px"
DataSourceID="NewRecipientsSrc" DataTextField="UserName" DataValueField="UserId"
SelectedValue='<%# Bind("UserId") %>' /
But I get this error message at runtime:
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

Which I don't understand. Using a TextBox instead works perfectly:
<asp:TextBox ID="Test" runat="server" Text='<%# Bind("UserId") %>' /
And using the dropdownlist in other controls, like FormView or DetailsView does work. I can't understand why the combination of ReorderList + DropDownList doesn't work.Are you sure that's causing the problem? I just tried this with the ReorderList sample page and it worked fine - both in the EditTemplate and InsertTemplate.
No, I'm not sure.
As I said, I don't understand why this doesn't work. But I've noticed that replacing the DropDownList with a TextBox works.

May it be that the error message is misleading ? The "UserId" I'm binding to is a SqlServer uniqueidentifier, could that be a problem ? (Although I think I tried to bind to UserName, which is a string, and it didn't work either)
Are you sure it's not the binding of the DropDownList itself that's having problems? Meaning I notice that it's also databound to another data source.
Yes, I'm sure of that.
If I simply remove theSelectedValue='<%# Bind("UserId") %>' attribute, the problem goes away (and the dropdown is correctly filled).

I would like to know what I am doing wrong, too. I am getting the same error. Here is the code for my page:

<h3>

Sort and Add new Categories</h3>

Select Parent Category to Sort:<br/>

<asp:DropDownListID="DropDownList1"runat="server"AppendDataBoundItems="True"AutoPostBack="True"

DataSourceID="CatListDataSource"DataTextField="CategoryName"DataValueField="CategoryID">

<asp:ListItemValue="-1"><Select Parent Category></asp:ListItem>

<asp:ListItemValue="0">Top Level</asp:ListItem>

</asp:DropDownList><br/>

<br/>

<asp:ObjectDataSourceID="CatListDataSource"runat="server"DeleteMethod="Deletecategory"

InsertMethod="AddCategory"OldValuesParameterFormatString="{0}"SelectMethod="GetCategories"

TypeName="Categories"UpdateMethod="UpdateCategory">

<DeleteParameters>

<asp:ParameterName="categoryid"Type="Int32"/>

</DeleteParameters>

<UpdateParameters>

<asp:ParameterName="categoryid"Type="Int32"/>

<asp:ParameterName="categoryname"Type="String"/>

<asp:ParameterName="parentcategoryid"Type="Int32"/>

<asp:ParameterName="sortorder"Type="Int32"/>

</UpdateParameters>

<InsertParameters>

<asp:ParameterName="categoryname"Type="String"/>

<asp:ParameterName="parentcategoryid"Type="Int32"/>

<asp:ParameterName="sortorder"Type="Int32"/>

</InsertParameters>

</asp:ObjectDataSource>

<atlas:ScriptManagerid="MasterScriptManager"EnableScriptGlobalization="false"runat="Server">

</atlas:ScriptManager>

<cc1:reorderlistid="ReorderList1"runat="server"allowreorder="True"callbackcssstyle="callbackStyle"

datakeyfield="categoryid"datasourceid="CatKidsDataSource"sortorderfield="sortorder"ItemInsertLocation="End">

<ItemTemplate>

<divclass="itemArea">

<b>Category:</b><asp:LabelID="Label4"runat="server"Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("CategoryName"))) %>'></asp:Label>

</div>

</ItemTemplate>

<ReorderTemplate>

<asp:PanelID="Panel2"runat="server"CssClass="reorderCue">

</asp:Panel>

</ReorderTemplate>

<InsertItemTemplate>

<divstyle="padding-left:25px; border-bottom:thin solid transparent;border-top:thin solid transparent;">

<asp:PanelID="panel1"runat="server"DefaultButton="Button1">

Insert a new Category here<br/>

Category Name<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ControlToValidate="Category"

ErrorMessage="Please enter a question"ValidationGroup="Insert">*</asp:RequiredFieldValidator>

<asp:TextBoxID="Category"Text='<%# Bind("CategoryName")%>'runat="server"></asp:TextBox><br/>

Parent Category:<br/>

<asp:DropDownListID="ParentName"runat="server"DataSourceID="CatList2DataSource"

DataTextField="CategoryName"DataValueField="CategoryID"ValidationGroup="Insert"AppendDataBoundItems="True"SelectedValue='<%# Eval("ParentCategoryID") %>'>

<asp:ListItemValue="0">Top Level</asp:ListItem>

</asp:DropDownList><asp:ObjectDataSourceID="CatList2DataSource"runat="server"OldValuesParameterFormatString="original_{0}"

SelectMethod="GetCategories"TypeName="Categories"></asp:ObjectDataSource>

<asp:ButtonID="Button1"runat="server"CommandName="Insert"Text="Add"ValidationGroup="Insert"></asp:Button>

</asp:Panel>

</div>

</InsertItemTemplate>

</cc1:reorderlist>

<asp:ObjectDataSourceID="CatKidsDataSource"runat="server"InsertMethod="AddCategory"

OldValuesParameterFormatString="original_{0}"SelectMethod="GetCategoriesByParent"

TypeName="Categories"UpdateMethod="UpdateCategory"DeleteMethod="Deletecategory">

<UpdateParameters>

<asp:ParameterName="categoryid"Type="Int32"/>

<asp:ParameterName="categoryname"Type="String"/>

<asp:ParameterName="parentcategoryid"Type="Int32"/>

<asp:ParameterName="sortorder"Type="Int32"/>

</UpdateParameters>

<SelectParameters>

<asp:ControlParameterControlID="DropDownList1"Name="parentid"PropertyName="SelectedValue"

Type="Int32"/>

</SelectParameters>

<InsertParameters>

<asp:ParameterName="categoryname"Type="String"/>

<asp:ParameterName="parentcategoryid"Type="Int32"/>

<asp:ParameterName="sortorder"Type="Int32"/>

</InsertParameters>

<DeleteParameters>

<asp:ParameterName="categoryid"Type="Int32"/>

</DeleteParameters>

</asp:ObjectDataSource>


I had the same problem. To get around the problem I added some hidden fields that the dropdown updated, and bound the hidden fields and this worked for me:

<InsertItemTemplate>

<!-- bottom border is workaround for IE7 Beta issue where bg doesn't render -->

<divstyle="padding-left:25px; border-bottom:thin solid transparent;">

<asp:PanelID="panel1"runat="server"DefaultButton="Button1">

<asp:DropDownListID="DDLResources"runat="server"

OnPreRender="DDLResources_PreRender"ValidationGroup="Add"OnSelectedIndexChanged="DDLResources_SelectedIndexChanged"/>

<asp:HiddenFieldID="hdnResourceId"runat="server"value='<%# Bind("ResourceId") %>'/>

<asp:HiddenFieldID="hdnResourceCode"runat="server"value='<%# Bind("ResourceCode") %>'/>

<asp:ButtonID="Button1"runat="server"CommandName="Insert"Text="Add"ValidationGroup="Add"/>

</asp:Panel>

</div>

</InsertItemTemplate>

No comments:

Post a Comment