I have an asp.net page with a listview which is bound to an SQLDataSource. Embedded into the Edit and Insert templates I have a dropDownList which is bound to a second SQLDataSource. In the update parameters I have specified the corresponding parameter to use the Selected Value of the DropDownList (At least I think I have). Unfortunately the selected value is not making it through into the update. If I try to refer the the dropdowlist control explicitly and try to do the update or Insert, the runtime complains that there are too many parameters being passed to the stored procedure used to do the update/insert. Code snippets are below.
Edit Template
<td class="GridContent">
<asp:DropDownList
ID="lstDataType"
runat="server"
DataSourceID="DataType"
DataTextField="Name"
DataValueField="Name"
CssClass="GridContent">
</asp:DropDownList>
Control Parameter (Using the ListView control - Doesn't provide the selected value)
<asp:ControlParameter
ControlID="lstFields"
Name="DataType"
PropertyName="SelectedValue"
Type="String" />
Control Parameter (using the DropDownList control - Update complains of too many parameters)
<asp:ControlParameter
ControlID="lstTypeID"
Name="Name"
PropertyName="SelectedValue"
Type="String" />
So I suppose my question is how do I get the Control Parameter for the update to reference the selected value of the embedded dropdownlist.
UPDATE
In the end it took me a while to find the answer, but it is quite straight forward; although finding the controls at the bottom of the ListView control tree was a complete nightmare.
What I did was add a hidden label control into the update and insert templates and bound this to the SQLDataSource for the listview. Then I grabbed the FindControlRecursive function that's in some sample code on the MSDN site at the bottom of the article here:
http://msdn.microsoft.com/en-us/library/y81z8326.aspx
Then in the Page_Load method for my page I added two calls the the FindControlRecursive function, one to get the label and the other to get the dropdownlist. Then simply assigned the DropDownList.text value to the Lab开发者_如何学Pythonel.text and it all worked beautifully.
I've been doing another listview with an embedded dropdownlist and made an interesting discovery (Well I think so). It seems that it isn't necessary to have the code behind to assign the text for the selected item to the label control. All that is necessary is for the hidden label and the drop down to both be bound to the appropriate field in the DataSource.
精彩评论