Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics 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 February 7th, 2009, 09:05 PM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in FormView

Greetings. I have FormView with Insert as default mode, and inside InsertItemTemplate, I have a DropDownList. Now, problem: I created Page_Load event, and at this event, I want to populate DropDownList with data from DB by using SqlReader (the page is used to add articles, and category where new article belongs is chosen from DropDownList.) But when I try to use DropDownList1.DataSource I get "name DropDownList1 doesn't exist in current context" error. I've spent three hours tying to solve it, and I'm really desperate. Any ideas, please?
 
Old February 8th, 2009, 12:51 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

As your DropDownList is nested, you need to use the FindControl method and cast the found control to a DropDownList.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old February 8th, 2009, 01:21 PM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, that seems to do the trick. Hovewer, I have one more question: How do I bind data to dropdownlist? I've tried

Code:
<asp:DropDownList ID="ddlCategory" runat="server" SelectedValue='<%# Bind("categoryId") %>'></asp:DropDownList>
which doesn't seem to work, as it says that "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.".
The code used to populate dropdownlist:

Code:
DropDownList ddl;

ddl = (DropDownList)FormView1.FindControl("ddlCategory");

ddl.DataSource = dReader;
ddl.DataTextField = "name";
ddl.DataValueField = "numberId";
ddl.DataBind();
Sorry to still bother, but any ideas?

EDIT:
Code:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
conn.Open();
string sSql = "SELECT numberId, name FROM Categories ORDER BY numberId DESC";
SqlCommand cmd = new SqlCommand(sSql, conn);
SqlDataReader dReader;
dReader = cmd.ExecuteReader();
This is dReader. All pasted coded is used at FormView1_DataBound event.

EDIT:

Oh well, I solved the problem by deleting all methods and adding data to dropdownlist by ObjectDataSource. You can lock this thread.

Last edited by NOclipus; February 8th, 2009 at 01:39 PM.. Reason: Solved
 
Old February 8th, 2009, 01:44 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Right. As you have discovered, if you don't using a page-level datasource, your data-binding expressions won't work. This is because the page attempts to evaluate the data-binding expression before the control is actually bound to any data.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch. 4 - Adding FormView control problem donesquire BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 2 August 6th, 2010 03:56 AM
formview mcarol44 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 January 5th, 2008 06:44 AM
FormView/ObjectDataSource/DropDownList Problem davidanderson ASP.NET 2.0 Professional 3 April 14th, 2007 02:50 AM
DropDownList Inside FormView Problem frogosteve ASP.NET 2.0 Basics 0 April 12th, 2007 10:30 AM
FormView Label problem... steve35719 ASP.NET 2.0 Basics 0 September 8th, 2006 12:30 PM





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