Question

ASP.NET Formview control dropdownlist SelectedIndexChanged problem

Asked by: DeMyu

I have problems getting the Formview control with two dependent dropdownlists to work in Edit Mode. I have say dropdownlist A and dropdownlist B where Dropdownlist B is dependent on dropdownlist A.

My goal is such that when a user clicks the edit button in the Formview the selectedvalues of dropdownlist A and dropdownlist B should be the values obtained from the database.

Here is the error message that I am receiving from the web server when I click edit to put the formview into edit mode.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

The error disappears when I remove SelectedValue='<%# Bind("appID") %>'  from

<asp:DropDownList ID="dropApplication" runat="server" DataTextField="appName" DataValueField="appID" SelectedValue='<%# Bind("appID") %>' />

<asp:FormView ID="FormView1" runat="server" DataKeyNames="romID">
							<EditItemTemplate>
								<table class="tblborder">
 <tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropComponent">Component:</LABEL><span class="form-required">*</span></td>
										<td align="left">	
									<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" AutoPostBack="True" SelectedValue='<%# Bind("orgID") %>' OnSelectedIndexChanged="dropComponent_SelectedIndexChanged">
                                    <asp:ListItem Value="">
                                            -- Choose a component --
                                        </asp:ListItem>
		                           </asp:DropDownList>
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropComponent" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
									<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropApplication">Application:</LABEL><span class="form-required">*</span></td>
										<td align="left">	
									<asp:DropDownList ID="dropApplication" runat="server" DataTextField="appName" DataValueField="appID" SelectedValue='<%# Bind("appID") %>' />
                                    
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropApplication" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
</table>
							</EditItemTemplate>	
 
 
----- codebehind
 
Sub dropComponent_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
        FormView1.ChangeMode(FormViewMode.Edit)
 
        Dim dropApplication As DropDownList = CType(FormView1.FindControl("dropApplication"), DropDownList)
        Dim dropComponent As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
 
        dropApplication.Items.Clear()
 
        Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("APPdbConnectString").ConnectionString)
 
        Dim cmdSQL As New SqlCommand("dbo.P_applications_show_active", objConn)
 
        cmdSQL.CommandType = CommandType.StoredProcedure
        cmdSQL.Parameters.Add(New SqlParameter("@orgID", SqlDbType.INT, 4)).Value = dropComponent.SelectedItem.Value
 
        Dim sqlDR As SqlDataReader
 
        Try
            objConn.Open()
 
            sqlDR = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)
 
            dropApplication.DataSource = sqlDR
            dropApplication.DataBind()
        Catch myException As Exception
            Message.InnerHtml = "<tr><th height=""25"" class=""thmsg"" colspan=""3"" nowrap=""nowrap"">" & myException.ToString() & "</th></tr>"
 
        Finally
            If Not sqlDR Is Nothing Then
                sqlDR.Close()
            End If
            dropApplication.Items.Insert(0, New ListItem("-- Choose an application --", ""))
            dropApplication.SelectedIndex = 0
        End Try
 
    End Sub

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-11 at 10:19:20ID24725257
Tags

ASP.NET

,

VB

,

Formview

Topic

Programming for ASP.NET

Participating Experts
1
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. dropdownlist
    Hi experts. I am having some problems populating my dropdown list which is embedded in the datagrid. this is the aspx ============================= <ASP:TemplateColumn HeaderText="Delete_Status"> <ItemTemplate> <asp:DropDownList ID="ItemDropDow...
  2. DropDownList
    HI, I have a dropdownlist populated with more than 1000 items. Is it possible to make the dropdownlist to locate the item by typing in the dropdownlist or any alternative solution to this? I am looking for dropdownlist to be able to type in it. can anyone pls. help me samp...
  3. databind in formview
    In ASP.NET 2.0 C# I have a form with a Formview, Dropdownlist and a TextBox. I change a value in the DropdownList and I nees the Textbox to refreshed. In the DropdownList_SelectedIndexChange I do my checkings . If there I doe Formview1.DataBind() I get the refresh I want in t...
  4. Dropdownlist in Listview
    I am using VS express web developer .net3.5. I have a datalist on the page and have changed 2 textboxes in to dropdown lists. I can populate the data in the dropdowns without an issue but I cannot bind it back to the database. With the code below I get the following error ...
  5. DropDownList in <EditItemTemplate>
    I have the following <EditItemTemplate> <tr style=""> <td><asp:DropDownList ID="stateDropDown" DataTextField="stateName" DataValueField="stateID" DataSource='<%# GetStates(Eval(&q...
  6. DropDownList in ListView fed from a separate datasource?
    Our ListView control is bound to the Content table In the InsertItem template (as well as the EditItem template eventually, etc...) we would like to populate a DropDownList control with the contents of the SiteMap table rather than using the standard textbox bound to the Sit...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: carlnorrbomPosted on 2009-09-11 at 23:23:55ID: 25315416

Hi,

Are you sure any items are returned for AppID? This error most likely occurs because of two reasons:

1) There are no values bound to the control
2) A value collection has been bound to the control but the value for AppID is null and null is not one of the values that has been bound to the control.

/Carl.

 

by: DeMyuPosted on 2009-09-12 at 15:47:10ID: 25318233

Carl,

Yes there is a value returned for AppID.

You are correct in your observation, the problem occurs because I am passing a selecteditem value to the control dropApplication when there are no values bound to the control - the Subroutine dropComponent_SelectedIndexChanged has not been activated. How can I prepopulate dropApplication with only the records that are related to the dropComponent.SelectedItem.Value (OrgID)? Remember the problem is in the edit mode of the formview.

Thank you.

DeMuy

 

by: DeMyuPosted on 2009-09-13 at 07:42:43ID: 25320364

I have modified my code as suggested by the author of the link http://www.webswapp.com/categories/ASPNET2/CascadingLists/vbFormView.aspx

however, I now have the error message: Could not find control 'dropApplication' in ControlParameter 'orgID'.    and

the data in the dropdownlist dropApplication is not filtered based on the orgID. Any suggestion? My current code snippet is below.


<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropComponent">Component:</LABEL><span class="form-required">*</span></td>
										<td align="left">	
									<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" autopostback="true" SelectedValue='<%# Bind("orgID") %>' >
                                    <asp:ListItem Value="">
                                            -- Choose a component --
                                        </asp:ListItem>
		                           </asp:DropDownList>
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropComponent" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
									<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropApplication">Application:</LABEL><span class="form-required">*</span></td>
										<td align="left">										
											
									<asp:DropDownList ID="dropApplication" runat="server"  DataSourceID="AppSqlDS"   DataTextField="appName" DataValueField="appID" AppendDataBoundItems="true" OnDataBound="dropApplication_DataBound" >
									<asp:ListItem Value="">
                                            -- Choose an Application --
                                        </asp:ListItem>
		                           </asp:DropDownList>		                           		                           
		                           <asp:SqlDataSource ID="AppSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:APPdbConnectString %>"
                                        SelectCommand="SELECT [appID], [appName] FROM [dbo].[applications]  WHERE ([orgID] = @orgID)">
                                       <SelectParameters>                               
                                       <asp:ControlParameter ControlID="dropApplication" Name="orgID" PropertyName="SelectedValue" Type="Int32" />
                                                                      
                                       </SelectParameters>
                                   </asp:SqlDataSource>
 
------- code behind
 
 Protected Sub dropApplication_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'add an empty item on top of the list 
        'AddEmptyItem(ddl)
        Dim frmV As FormView = DirectCast(ddl.NamingContainer, FormView)
        If frmV.DataItem IsNot Nothing Then
            'Pull the application's value from the databound item. 
            Dim strAppID As String = DirectCast(frmV.DataItem, DataRowView)("appID").ToString()
 
            ddl.ClearSelection()
             
            Dim li As ListItem = ddl.Items.FindByValue(strAppID)
            If li IsNot Nothing Then
                li.Selected = True
            End If
        End If
       
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:

Select allOpen in new window

 

by: carlnorrbomPosted on 2009-09-13 at 09:48:57ID: 25320897

Hi,

It's probably because the dropdown is nested inside your formview, try changing the control parameter declaration to:

<asp:ControlParameter ControlID="FormView1$dropApplication" Name="orgID" PropertyName="SelectedValue" Type="Int32" />

If that doesn't fly, declare a public property in code behind and set it's value to dropApplication.ClientID.ToString() for instance in the FormView's databound event and use that in the declaration of the control parameter, i.e. ControlID="<%= ControlId %>"

/Carl.

 

by: DeMyuPosted on 2009-09-13 at 15:04:42ID: 25321967


Option 1 resulted in the following error messages:

Could not find control 'Formview1$dropApplication' in ControlParameter 'orgID'.

I am not sure of how to implement Option 2.

Thank you.

DeMuy

 

by: carlnorrbomPosted on 2009-09-14 at 00:20:36ID: 25323352

Hi,

Hmm, strange.. Option 1 usually works with nested controls. Also, I just noticed you have it wrong, the control parameter should point to dropComponent, not dropApplication -> dropApplication is the dropdown you want to populate based on the selected value of dropComponent, right?

<asp:ControlParameter ControlID="FormView1$dropComponent" Name="orgID" PropertyName="SelectedValue" Type="Int32" />

I'll get back to you with an implementation of the public property.

/Carl.

 

by: carlnorrbomPosted on 2009-09-14 at 01:42:11ID: 25323640

Hi,

I reviewed an old project of mine and found an even simpler solution:

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        If CType(sender, FormView).CurrentMode = FormViewMode.Insert Then
            Dim dropComponent As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
            Dim AppSqlDS As SqlDataSource = CType(FormView1.FindControl("AppSqlDS"), SqlDataSource)
            AppSqlDS.SelectParameters.RemoveAt(0)
            AppSqlDS.SelectParameters.Add("orgID", dropComponent .SelectedValue.ToString())
        End If
    End Sub

It simply checks at runtime whether the formview is in insert mode, if it is, it removes the first parameter in the collection (in this case we only have one) and replaces it with the correct dropdown. Should do the trick.

/Carl.

 

by: DeMyuPosted on 2009-09-14 at 08:24:29ID: 25326411

I have made the update that you've recommended above in the application. It filters the data in dropApplication based on the selectedvalue of dropComponent, however, the application is still handicapped by the following problems:

1. Error message "Could not find control 'Formview1$dropComponent' in ControlParameter 'orgID'." when I click either the cancel or update button.

2. When another value is selected from the dropComponent it fails to filter the data in the dropApplication and displays all the records in dropApplication.

</tr>
								    <tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropComponent">Component:</LABEL><span class="form-required">*</span></td>
										<td align="left">	
									<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" autopostback="true" SelectedValue='<%# Bind("orgID") %>' >
                                    <asp:ListItem Value="">
                                            -- Choose a component --
                                        </asp:ListItem>
		                           </asp:DropDownList>
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropComponent" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
									<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropApplication">Application:</LABEL><span class="form-required">*</span></td>
										<td align="left">										
											
									<asp:DropDownList ID="dropApplication" runat="server"  DataSourceID="AppSqlDS"   DataTextField="appName" DataValueField="appID" AppendDataBoundItems="true" OnDataBound="dropApplication_DataBound" >
									<asp:ListItem Value="">
                                            -- Choose an Application --
                                        </asp:ListItem>
		                           </asp:DropDownList>		                           		                           
		                           <asp:SqlDataSource ID="AppSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:APPdbConnectString %>"
                                        SelectCommand="SELECT [appID], [appName] FROM [dbo].[applications] WHERE ([orgID] = @orgID)">
                                       <SelectParameters>                               
                                       <asp:ControlParameter ControlID="Formview1$dropComponent" Name="orgID" PropertyName="SelectedValue" Type="Int32" />                                                                      
                                       </SelectParameters>
                                   </asp:SqlDataSource>	                          
                                    
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropApplication" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
 
 
---- Code Behind
 
Protected Sub dropApplication_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'add an empty item on top of the list 
        'AddEmptyItem(ddl)
        Dim frmV As FormView = DirectCast(ddl.NamingContainer, FormView)
        If frmV.DataItem IsNot Nothing Then
            'Pull the application's value from the databound item. 
            Dim strAppID As String = DirectCast(frmV.DataItem, DataRowView)("appID").ToString()
 
            ddl.ClearSelection()
             
            Dim li As ListItem = ddl.Items.FindByValue(strAppID)
            If li IsNot Nothing Then
                li.Selected = True
            End If
        End If
       
    End Sub
 
    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        If CType(sender, FormView).CurrentMode = FormViewMode.insert Then
            Dim dropComponent As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
            Dim AppSqlDS As SqlDataSource = CType(FormView1.FindControl("AppSqlDS"), SqlDataSource)
            AppSqlDS.SelectParameters.RemoveAt(0)
            AppSqlDS.SelectParameters.Add("orgID", dropComponent.SelectedValue.ToString())
        End If
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: carlnorrbomPosted on 2009-09-14 at 10:19:05ID: 25327454

Hi,

First of all, you don't need to declare a control parameter if you use the code behind. Secondly, the code behind is checking what current state the formview is in and assigns the parameter if it's in insert mode. If you want this function to work regardless of which state the formview is in, simply remove:

        If CType(sender, FormView).CurrentMode = FormViewMode.insert Then
            ...
        End If

Or alternatively, change the first line to:

If CType(sender, FormView).CurrentMode = FormViewMode.insert Or CType(sender, FormView).CurrentMode = FormViewMode.Edit Then

You need to make sure to only check for the dropdown in the modes where it is present, otherwise the code will break! Also change the select parameter to a regular parameter or remove it all together.

/Carl.

 

by: DeMyuPosted on 2009-09-14 at 11:04:36ID: 25327809

I have again made changes to the code as suggested and seen some improvements in the application, however, one feature that is still pending is the ability to change the records in the dropdownlist dropApplication based on the selected value of dropComponent. That is the reason behind the SelectCommand="SELECT [appID], [appName] FROM [dbo].[applications] WHERE ([orgID] = @orgID)" in the SQLdatasource. Am I missing something here?

Thanks

 <tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropComponent">Component:</LABEL><span class="form-required">*</span></td>
										<td align="left">	
									<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" autopostback="true" SelectedValue='<%# Bind("orgID") %>' >
                                    <asp:ListItem Value="">
                                            -- Choose a component --
                                        </asp:ListItem>
		                           </asp:DropDownList>
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropComponent" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
									<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropApplication">Application:</LABEL><span class="form-required">*</span></td>
										<td align="left">										
											
									<asp:DropDownList ID="dropApplication" runat="server"  DataSourceID="AppSqlDS"   DataTextField="appName" DataValueField="appID" AppendDataBoundItems="true" OnDataBound="dropApplication_DataBound" >
									<asp:ListItem Value="">
                                            -- Choose an Application --
                                        </asp:ListItem>
		                           </asp:DropDownList>		                           		                           
		                           <asp:SqlDataSource ID="AppSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:APPdbConnectString %>"
                                        SelectCommand="SELECT [appID], [appName] FROM [dbo].[applications] WHERE ([orgID] = @orgID)">
                                      <SelectParameters> 
                                      <asp:Parameter Name="orgID" Type="int32" />                           
                                                                                                             
                                       </SelectParameters>
                                   </asp:SqlDataSource>	
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropApplication" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
 
 
--- code behind
 
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
 
        If CType(sender, FormView).CurrentMode = FormViewMode.insert Or CType(sender, FormView).CurrentMode = FormViewMode.Edit Then
 
            Dim dropComponent As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
            Dim AppSqlDS As SqlDataSource = CType(FormView1.FindControl("AppSqlDS"), SqlDataSource)
            AppSqlDS.SelectParameters.RemoveAt(0)
            AppSqlDS.SelectParameters.Add("orgID", dropComponent.SelectedValue.ToString())
        End If
    End Sub
 
Protected Sub dropApplication_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'add an empty item on top of the list 
        'AddEmptyItem(ddl)
        Dim frmV As FormView = DirectCast(ddl.NamingContainer, FormView)
        If frmV.DataItem IsNot Nothing Then
            'Pull the application's value from the databound item. 
            Dim strAppID As String = DirectCast(frmV.DataItem, DataRowView)("appID").ToString()
 
            ddl.ClearSelection()
             
            Dim li As ListItem = ddl.Items.FindByValue(strAppID)
            If li IsNot Nothing Then
                li.Selected = True
            End If
        End If
       
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:

Select allOpen in new window

 

by: carlnorrbomPosted on 2009-09-14 at 12:27:02ID: 25328473

Hi,

Try the following:

1. Remove all the code in the FormView1_DataBound() method
2. Add the following method:

    Protected Sub dropComponent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dropApplication As DropDownList = CType(FormView1.FindControl("dropApplication"), DropDownList)
        Dim AppSqlDS As SqlDataSource = CType(FormView1.FindControl("AppSqlDS"), SqlDataSource)
        dropApplication.Items.Clear()
        dropMake.Items.Add(New ListItem("-- Choose an application --", ""))
        AppSqlDS.SelectParameters.Clear()
        AppSqlDS.SelectParameters.Add("orglID", CType(sender, DropDownList).SelectedValue)
    End Sub

3. Change your markup for the dropComponent to:

<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" AutoPostback="true" OnSelectedIndexChanged="dropComponent_SelectedIndexChanged" SelectedValue='<%# Bind("orgID") %>' >
    <asp:ListItem Value="" Text="-- Choose a component --" />
</asp:DropDownList>

/Carl.

 

by: DeMyuPosted on 2009-09-14 at 15:46:01ID: 25330002

carlnorrbom:

I finally got the application to work as intended. I thank you for your assistance and perseverance. Your most recent code above and information that I obtained from http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1629&count=no were very helpful. I have posted my working code below. I am sure it will come in handy for someone else.

DeMuy


<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropComponent">Component:</LABEL><span class="form-required">*</span></td>
										<td align="left">
										<asp:DropDownList ID="dropComponent" runat="server" DataSourceID="SqlDataSource01" DataTextField="orgName" DataValueField="orgID" AppendDataBoundItems="true" AutoPostback="true" OnSelectedIndexChanged="dropComponent_SelectedIndexChanged" SelectedValue='<%# Bind("orgID") %>' >
                                            <asp:ListItem Value="" Text="-- Choose a component --" />
                                        </asp:DropDownList>	
									
		                           </td>
		                           <td><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="dropComponent" ErrorMessage="<img src='images/common/required_trans.gif' align='absmiddle'>" />
									</td>
									</tr>
									<tr>
										<td class="formlabel"><LABEL class="reqlabel" for="dropApplication">Application:</LABEL><span class="form-required">*</span></td>
										<td align="left">										
											
									<asp:DropDownList ID="dropApplication" runat="server"  DataSourceID="AppSqlDS"   DataTextField="appName" DataValueField="appID" AppendDataBoundItems="true">
									<asp:ListItem Value="">
                                            -- Choose an Application --
                                        </asp:ListItem>
		                           </asp:DropDownList>		                           		                           
		                           <asp:SqlDataSource ID="AppSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:APPdbConnectString %>"
                                        SelectCommand="SELECT [appID], [appName] FROM [dbo].[applications] WHERE ([orgID] = @orgID)">
                                      <SelectParameters> 
                                      <asp:Parameter Name="orgID" Type="int32" />                           
                                                                                                             
                                       </SelectParameters>
                                   </asp:SqlDataSource>	
 
---- code behind
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
 
        If FormView1.CurrentMode = FormViewMode.Edit Then
            Dim dv As System.Data.DataRowView = FormView1.DataItem
            Dim ddl2 As DropDownList = FormView1.FindControl("dropApplication")
            Dim dsc As SqlDataSource = FormView1.FindControl("AppSqlDS")
            dsc.SelectParameters("orgID").DefaultValue = dv("orgID")
            ddl2.DataBind()
            If Not IsDBNull(dv("appID")) Then
                ddl2.SelectedValue = dv("appID")
            End If
        End If
    End Sub
 
 
    Protected Sub dropComponent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
 
        Dim dsc As SqlDataSource = CType(FormView1.FindControl("AppSqlDS"), SqlDataSource)
        Dim ddl1 As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
        Dim ddl2 As DropDownList = CType(FormView1.FindControl("dropApplication"), DropDownList)
 
        ddl2.Items.Clear()
        ddl2.Items.Add(New ListItem("-- Choose an application --", ""))
        dsc.SelectParameters(0).DefaultValue = ddl1.SelectedValue
        ddl2.DataBind()
    End Sub
 
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
                Dim dropComponent As DropDownList = CType(FormView1.FindControl("dropComponent"), DropDownList)
        Dim dropApplication As DropDownList = CType(FormView1.FindControl("dropApplication"), DropDownList)
 
                e.NewValues("appID") = dropApplication.SelectedValue
 
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: DeMyuPosted on 2009-09-14 at 15:47:04ID: 31627665

Thank you for the great work and assistance.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...