Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2011, 02:28 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default From dropdown list to Textbox value

Based upon a dropdown list selected value by the user (which is a field in a table) I want to populate a TextBox with another field value that is in the same record.

I have:
Code:
 <asp:DropDownList ID="DropDownList2" runat="server" 
                  DataSourceID="CourseDataSource2" DataTextField="Course_Name" 
                  DataValueField="Course_Name" SelectedValue='<%# Bind("Course_Name") %>' 
                  AutoPostBack="True" OnSelectedIndexChanged="CourseSelectionChanged">
               </asp:DropDownList>
In my method

Code:
 protected void CourseSelectionChanged(object sender, EventArgs e)
   {
      string value;
      
       GolfDatabaseDataContext db = new GolfDatabaseDataContext();

       var dropquery = from Couse in db.Courses
                       where Course.Course_Name == DropDownList2.SelectedItem.Value
                       select Course.Course_Rating;

       foreach (var result in dropquery)
       {
          value = result.ToString();
          TextBox CourseRating = ((TextBox)DetailsView1.FindControl("TextBox2"));
          CourseRating.Text = value;
       }//end foreach
      
      
   }//end method
The error I am getting is that Course.Course_Name and DropDownList2 do not exist in the current context and makes me believe that the problem is an incorrect paramenter list in the method header?

Any help would be greatly appreciated
TIA

Cliff
 
Old July 27th, 2011, 02:37 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Cliff,

Is DropDownList2 in the DetailsView as well? if so, shouldn't you be using FindControl as well to find that control as you do with the TextBox?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old July 27th, 2011, 02:57 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default DropDownList2

Code:
<asp:TemplateField HeaderText="Course_Name" SortExpression="Course_Name">
            <EditItemTemplate>
               <asp:DropDownList ID="DropDownList2" runat="server" 
                  DataSourceID="CourseDataSource2" DataTextField="Course_Name" 
                  DataValueField="Course_Name" SelectedValue='<%# Bind("Course_Name") %>' 
                  AutoPostBack="True" OnSelectedIndexChanged="CourseSelectionChanged">
               </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:DropDownList ID="DropDownList2" runat="server" 
                  DataSourceID="CourseDataSource2" DataTextField="Course_Name" 
                  DataValueField="Course_Name" SelectedValue='<%# Bind("Course_Name") %>' 
                  AutoPostBack="True">
               </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
               <asp:Label ID="Label6" runat="server" Text='<%# Bind("Course_Name") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
above is that portion of DetailsView showing DropDownList2 as a member, although I only added the OnSelectedIndexChanged to the <EditItemTemplate> portion as DropDownList2is a Template Field.

I'm not sure I follow the TextBox idea with DropDownList2 as my error message comes prior to my TextBox declaration. The error is shown in the WHERE clause of my var dropquery SQL code?
 
Old July 27th, 2011, 03:49 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

TextBox2 is not directly available (as it's in the DetailsView) so you're using FindControl to get access to it. The same is true for the DropDownList2 which you access directly in your query at the moment. There is no DropDownList2 control at the class level (only inside the DetailsView), and thus the code crashes. Using FindControl as you do with the textbox might work:

Code:
 
DropDownList myDDL = ((DropDownList)DetailsView1.FindControl("DropDownList2"));
where Course.Course_Name == myDDL.SelectedItem.Value
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 27th, 2011)
 
Old July 27th, 2011, 03:57 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default New version of method

New version of my "Changed" method. No errors, but now when I make a selection from the drop down list the page "flickers" but the Text value I'm looking for does not appear. Another PostBack issue?

Code:
 protected void CourseSelectionChanged(object sender, EventArgs e)
   {
      string value;
      
       GolfDatabaseDataContext db = new GolfDatabaseDataContext();
      
      DropDownList CoursePlace = ((DropDownList)DetailsView1.FindControl("DropDownList2"));

       var dropquery = from Course in db.Courses
                       where Course.Course_Name == CoursePlace.SelectedItem.Value
                       select Course.Course_Rating;

       foreach (var result in dropquery)
       {
          value = result.ToString();
          TextBox CourseRating = ((TextBox)DetailsView1.FindControl("TextBox2"));
          CourseRating.Text = value;
       }//end foreach
      
      
   }//end method
 
Old July 27th, 2011, 04:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What happens when you debug? Maybe your code gets overwritten by the details view? Hard to say without seeing the full code.

This is exactly the reason why I don't like the DetailsView for these kind of scenarios. To hard to customize. I would reconsider AJAX / web services or a simple,straight forwarded hand coded pages using LINQ / EF in the Code Behind.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 27th, 2011)
 
Old July 27th, 2011, 04:18 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Solution

You have been so helpful, I thought I would share the solution.

In the DetailsView I had the OnSelectedIndexChanged code in the <EditItemTemplate> section only. My thought was to copy that "call" into the <InsertItemTemplate> section of the DropDownList and when I did, the code worked.

This is huge for my project.

TNX

Cliff
 
Old July 27th, 2011, 05:50 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome, and thanks for the update.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdown list maha .NET Framework 2.0 7 April 28th, 2007 12:32 AM
Updating a textbox from DropDown selection kjord ASP.NET 1.0 and 1.1 Professional 4 November 7th, 2006 04:04 AM
Data from Dropdown List to TextBox caterpillar General .NET 1 July 10th, 2006 06:05 AM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM
Remove value of dropdown list when textbox changed sn00pyg4rfi3ld Javascript How-To 1 January 20th, 2005 07:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.