 |
| 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
|
|
|
|

July 27th, 2011, 02:28 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 27th, 2011, 02:37 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

July 27th, 2011, 02:57 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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?
|
|

July 27th, 2011, 03:49 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 27th, 2011, 03:57 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 27th, 2011, 04:04 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 27th, 2011, 04:18 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 27th, 2011, 05:50 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're welcome, and thanks for the update.
Cheers,
Imar
|
|
 |