Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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?
  #2 (permalink)  
Old February 8th, 2009, 12:51 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
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(); 
}
  #3 (permalink)  
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
  #4 (permalink)  
Old February 8th, 2009, 01:44 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
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(); 
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:11 AM.


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