Changing Dropdownlist SelectedValue inside a formview.

Hi, I have this dropdownlist in my formview (named ddlCategory2) which is databound to a sql which simply returns all rows of a table. When I switch my formview mode to insert, I want this same dropdownlist to have a default selected value based on another dropdownlist which resides outside the formview (named ddlCategory). They are both bound to the same datasource and you can find the code below. What would be the VB.NET code and event to achieve this ? Thanks
<asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="CatDataSource" 
    DataTextField="CATE_DESCRIPTION_E" DataValueField="SPEC_CATEGORY" 
    Width="200px" AutoPostBack="True">
    <asp:ListItem Selected="True" Value="%"></asp:ListItem>
</asp:DropDownList>
 
<asp:DropDownList ID="ddl2Category" runat="server" DataSourceID="CatDataSource" 
                            DataTextField="CATE_DESCRIPTION_E" DataValueField="SPEC_CATEGORY" 
                            SelectedValue='<%# Bind("SPEC_CATEGORY") %>' Width="200px">
                        </asp:DropDownList>

Open in new window

GPSAddictAsked:
Who is Participating?
 
wht1986Commented:
I would handle the inserting event to set the correct value

this is in the insert item template
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserID" SelectedValue="<%# DropDownList1.SelectedValue %>" />

on form view inserting, grab whatever value is in there and stuff it into the values property of the argument, the datasource will then use it for its insert

        protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            DropDownList ddl2 = (DropDownList)this.FormView1.FindControl("DropDownList2");
            e.Values.Add("UserName", ddl2.SelectedItem.Text);
        }

obviously your code will be a little different but the methodology stays the same
0
 
wht1986Commented:
Heres a simple example:
<body>
    <form id="form1" runat="server">
    
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="UserID" DataSourceID="SqlDataSource1">
 
            <ItemTemplate>
                UserName: <asp:Label ID="UserNameLabel" runat="server" Text='<%# Bind("UserName") %>' />
            </ItemTemplate>
 
            <InsertItemTemplate>
                UserName: <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserID" SelectedValue="<%# DropDownList1.SelectedValue %>" />
            </InsertItemTemplate>
 
        </asp:FormView>
        
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserID" />
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ExpertExchangeConnectionString %>" SelectCommand="SELECT * FROM [Users]"></asp:SqlDataSource>
        
        <asp:Button ID="Button1" runat="server" Text="Switch To Insert" onclick="Button1_Click1" />
    
    </form>
</body>
 
 
protected void Button1_Click1(object sender, EventArgs e)
{
    this.FormView1.ChangeMode(FormViewMode.Insert);
}

Open in new window

0
 
GPSAddictAuthor Commented:
I forgot to mention that the 2nd dropdownlist is already bound I guess...
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
wht1986Commented:
All mine are bound.  The dropdownlist inside on the insert template, gets its range of values from the sqldatasource and the default selected value is pulled from the selectedvalue of a dropdown outside the formview.  The picture below kind of describes what i have.

If this is not what you are doing, can you supply the formview definition
Capture1.JPG
0
 
wht1986Commented:
grr i uploaded the wrong pic, sorry
Capture.JPG
0
 
wht1986Commented:
Was i able to answer your question?  If you need more help, post some basic code snippets that i can recreate the issue on a test page here.
0
 
GPSAddictAuthor Commented:
It doesn't work. In order for my insert to work, it seems I still need to set my dropdownlist2 to SelectedValue='<%# Bind("SPEC_CATEGORY") %>...
0
 
GPSAddictAuthor Commented:
Awesome, thanks !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.