how do I tell a DropDownList, that its value is supposed to be aparameter for a SQLDataSource's InsertParameters? Following is part ofmy aspx code:
<asp:SqlDataSource
ID="srcPerson"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="System.Data.SqlClient"
InsertCommand="InsertPerson"
InsertCommandType="StoredProcedure"
OldValuesParameterFormatString="{0}">
<InsertParameters>
<asp:Parameter Name="PersonLastName" Type="String" />
<asp:Parameter Name="PersonFirstName" Type="String" />
<asp:Parameter Name="RoomID" Type="String" />
<asp:Parameter Name="PersonPhone" Type="String" />
<asp:Parameter Name="PersonPhonePrivate" Type="String" />
<asp:Parameter Name="PersonPhoneMobile" Type="String" />
<asp:Parameter Name="PersonEmail" Type="String" />
</InsertParameters>
</asp:SqlDataSource
<asp:FormView
ID="fvStaff"
cssClass="person"
runat="server"
DataSourceID="srcPerson">
<InsertItemTemplate>
<!--DataSource srcRooms gets all rooms from room table in database. -->
<asp:SqlDataSource
ID="srcRoomsInsert"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SelectRoomsByBuildingID"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlBuildingsInsert"
DefaultValue="1"
Name="ID"
PropertyName="SelectedValue"
Type="Int32"
/>
</SelectParameters>
</asp:SqlDataSource>
<asp:LabelcssClass="lblIdentifier" ID="lblLastName" runat="server"meta:resourcekey="LastName" />
<asp:TextBox ID="PersonLastName" runat="server" Text='<%#Bind("PersonLastName") %>'></asp:TextBox><br />
<asp:LabelcssClass="lblIdentifier" ID="lblFirstName" runat="server"meta:resourcekey="FirstName" />
<asp:TextBox ID="PersonFirstName" runat="server" Text='<%#Bind("PersonFirstName") %>'></asp:TextBox><br />
<asp:LabelCssClass="lblIdentifier" ID="lblBuilding" runat="server"meta:resourcekey="Building"></asp:Label>
<asp:DropDownList
ID="ddlBuildingsInsert"
cssClass="buildings"
runat="server"
DataSourceID="srcBuildings"
DataTextField="BuildingAbbreviation"
DataValueField="BuildingID"
AutoPostBack="True"
AppendDataBoundItems="true"
OnSelectedIndexChanged="ddlBuildingsInsert_SelectedIndexChanged">
<asp:ListItem enabled="true" value="0"meta:resourcekey="NoBuilding"></asp:ListItem>
</asp:DropDownList><br />
<asp:LabelcssClass="lblIdentifier" ID="lblRoom" runat="server"meta:resourcekey="Room" />
<asp:DropDownList
ID="ddlRoomsInsert"
cssClass="rooms"
runat="server"
Enabled="false"
DataSourceID="srcRoomsInsert"
DataTextField="RoomName"
DataValueField="RoomID"
AutoPostBack="true"
SelectedValue='<%# Bind("PersonRoomID") %>'>
</asp:DropDownList><br /
<asp:LabelcssClass="lblIdentifier" ID="lblPhone" runat="server"meta:resourcekey="Phone" />
<asp:TextBox cssClass="staff" ID="Phone" runat="server" Text='<%#Bind("PersonPhone") %>' /><br />
<asp:LabelcssClass="lblIdentifier" ID="lblPhonePrivate" runat="server"meta:resourcekey="Private" />
<asp:TextBox cssClass="staff" ID="PhonePrivate" runat="server"Text='<%# Bind("PersonPhonePrivate") %>' /><br />
<asp:LabelcssClass="lblIdentifier" ID="lblPhoneMobile" runat="server"meta:resourcekey="Mobile" />
<asp:TextBox cssClass="staff" ID="PhoneMobile" runat="server"Text='<%# Bind("PersonPhoneMobile") %>' /><br /
<asp:LabelcssClass="lblIdentifier" ID="lblEmail" runat="server"meta:resourcekey="Email" />
<asp:TextBox cssClass="staff" ID="Email" runat="server" Text='<%#Bind("PersonEmail") %>' /><br />
<asp:LinkButton cssClass="lnkButton" ID="InsertCancelButton"runat="server" CausesValidation="False" CommandName="Cancel"Text="<%$resources:translations,Cancel%>"></asp:LinkButton>
<asp:LinkButton cssClass="lnkButton" ID="InsertButton"runat="server" CausesValidation="True" CommandName="Insert"Text="<%$resources:translations,Insert%>"></asp:LinkButton>
</InsertItemTemplate>
</asp:FormView
This is what I tried in the first place, because the "Text='<%#Bind("PersonEmail") %>' " thing worked fine with the textboxes. Butafter selecting some value in the DropDownList ddlBuildingsInsert I getthe error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
pointing to the DropDownList ddlRoomsInsert. Deleting the"SelectedValue='<%# Bind("PersonRoomID") %>'" line from theDropDownList results in a missing insert parameter "PersonRoomID".Can you tell me the mistake I'm making here?
Thanks a lot, ToobThis one actually stumped me for a while too. The not so nice workaround I did for a similiar situation was to have a hidden value control, then during the dropdown change event I set the hidden value control to the selectedvalue from the dropdown, then link the insert sqldatasource to use the hidden value control instead. Bad, and requires a server postback for the dropdown change event, but for my particular app, I needed to do the postback anyhow.|||
in the data source control :
<SelectParameters>
<asp:ControlParameter Name="State" ControlID="statesDDL" PropertyName="SelectedValue" />
</SelectParameters>
Use a different bind method.
http://eramseur.blogspot.com/2005/11/dropdownlist-with-gridview.html
Hope that helps.
|||Thanks, the ControlParameter made it work. The next problem I wasfacing was the ControlID argument, it would only work if I used theddl's UniqueID. I'd like to put it in there somehow programmaticly(like <%# =statesDDL.UniqueID %>) but didn't find a way. Is therea way to do this?Thanks again,
Toob
No comments:
Post a Comment