The Stored Procedure is something like:
SELECT name contact, parentID FROM sometable WHERE value = @ID
and the datasource that uses this is:
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
This is the DataSource that I am using to bind the SelectedValue of the
DropDownList mentioned earlier.
When I'm populating my FormView:
<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1"
OnModeChanged="FormView1_ModeChanged" >
<EditItemTemplate>
Name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%#
Bind("name") %>'></asp:TextBox><br />
Contact Person:
<asp:TextBox ID="contactpersonTextBox" runat="server"
Text='<%# Bind("contactperson") %>'></asp:TextBox><br />
Parent:
<asp:DropDownList ID="parentIDDropDownList"
runat="server" DataSourceID="SqlDataSource3" DataTextField="name"
DataValueField="ID">
<asp:ListItem Text="Select a Parent" Value="0" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [client]
ORDER BY [name]"></asp:SqlDataSource>
<br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton> <asp:LinkButton
ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
Name:
<asp:Label ID="nameLabel" runat="server" Text='<%#
Bind("clientname") %>'></asp:Label><br />
Contact Person:
<asp:Label ID="contactpersonLabel" runat="server"
Text='<%# Bind("contactperson") %>'></asp:Label><br />
Parent:
<asp:Label ID="parentIDLabel" runat="server"
</asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit" Visible="false"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
As you can see in the EditItemTemplate, for the parentIDDropDownList, I
need to add a new ListItem with value "0", before binding the
SelectedValue to the value returned from SqlDataSource3. But once I add
the ListItem "Select a Parent", there is no way I can assign the
SelectedValue other than in the FormView1_ModeChanged() EventHandler in
the code-behind file.(Or is there???)
protected void FormView1_ModeChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
DropDownList myDdl =
(DropDownList)(FormView1.FindControl("parentIDDrop DownList"));
if (myDdl != null)
{
myDdl.Items.Insert((myDdl.Items.Count), new
ListItem("Select a parent", "0"));
// myDdl.SelectedValue=SqlDataSource1.????
}
}
}