Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Changing Dropdownlist SelectedValue inside a formview.

Posted on 2009-04-16
8
Medium Priority
?
1,079 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:GPSAddict
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24160323
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
 

Author Comment

by:GPSAddict
ID: 24160385
I forgot to mention that the 2nd dropdownlist is already bound I guess...
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24160514
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 12

Expert Comment

by:wht1986
ID: 24160521
grr i uploaded the wrong pic, sorry
Capture.JPG
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24164689
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
 

Author Comment

by:GPSAddict
ID: 24167197
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
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 total points
ID: 24167516
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
 

Author Comment

by:GPSAddict
ID: 24167745
Awesome, thanks !
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question