Drop Down Selected Text

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 4
Reputation: DJ1UK is an unknown quantity at this point 
Solved Threads: 0
DJ1UK DJ1UK is offline Offline
Newbie Poster

Drop Down Selected Text

 
0
  #1
Jul 5th, 2005
Hi All,

I'm stuck!!

I've got a page with 3 dropdowns on it and a submit button to pass the data to a table in a database.

The code is as follows:

  1. Sub Page_Load(sender as Object, e as eventargs)
  2. dim objConn as new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\blah\blah\blah.mdb)
  3.  
  4. objConn.Open()
  5. Const strSQL as String = "SELECT UserID FROM tblUser"
  6. Dim objCmd as New OleDbCommand (strSQL, objConn)
  7. ddUserID.DataSource = ObjCmd.ExecuteReader()
  8. ddUserID.DataBind()
  9. objConn.Close()
  10.  
  11. objConn.Open()
  12. Const strSQL2 as String = "SELECT ProjectID FROM tblProject"
  13. Dim objCmd2 as New OleDbCommand (strSQL2, objConn)
  14. ddProjectID.DataSource = ObjCmd2.ExecuteReader()
  15. ddProjectID.DataBind()
  16. objConn.Close()
  17.  
  18. End Sub
  19.  
  20. Sub Submit(Sender As Object, e as EventArgs)
  21. if Page.IsValid then
  22. dim objUserProject as New DecSup.UserProject
  23. dim objUserProjectDetails as New DecSup.UserProjectDetails
  24.  
  25. objUserProjectDetails.UserID = ddUserID.Selecteditem.Text
  26. objUserProjectDetails.ProjectID = ddProjectID.Selecteditem.Text
  27. objUserProjectDetails.Type = ddType.Selecteditem.Text
  28.  
  29. objUserProject.AddUserProject(objUserProjectDetails)
  30.  
  31. Response.Redirect("success.aspx")
  32. else
  33. lblMessage.Text = "Please Check - Some information is invalid. User NOT allocated to a project."
  34. end if
  35. End Sub
  36.  

the Drop downs ids are ddUserID, ddProjectID and ddType. ddType has the following code:

  1.  
  2. <tr><td><font face="arial">Type:</font></td><td><font face="arial">
  3. <asp:Dropdownlist id="ddType" runat="server" DataTextField="Type" DataValueField="Type">
  4. <asp:ListItem>Member</asp:ListItem>
  5. <asp:ListItem>Controller</asp:ListItem>
  6. </asp:Dropdownlist>

The third dropdown - ddType (with the hardcoded options) is submitted to the database fine, but the first two default to the value at the top of the list. I want to submit the data that the user chooses (obviously).

any ideas what I'm doing wrong? if you need to vb code let me know and I'll
submit it.

thanks

DJ
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Drop Down Selected Text

 
0
  #2
Jul 5th, 2005
This is tricky. I'm guessing (and it's a guess because ASP.NET is not built to handle dynamic controls, and weird things happen), that you're a victim of the ASP.NET Page Lifecycle.

You're creating the controls on Page_Load, which is good, because dynamic controls must be recreated every time. However, by the time they are created, the LoadPostBack stage has already occured.

In order to make this work, you have to recreate your controls prior to the Load stage.

This article ASP.NET Page Life Cycle and Dynamic Controls discusses the topic in-depth.

Basically, you need to overload the LoadViewState method, and recreate your controls there. Then, when LoadPostBack occurs, your controls will be there to receive their values.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: DJ1UK is an unknown quantity at this point 
Solved Threads: 0
DJ1UK DJ1UK is offline Offline
Newbie Poster

Re: Drop Down Selected Text

 
0
  #3
Jul 5th, 2005
Thanks for the info. I'm a newbie however, so what type of code do I need to implement to get around this problem?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Drop Down Selected Text

 
0
  #4
Jul 5th, 2005
The article I linked to explains all of that, complete with sample code and a detailed explanation of the ASP.NET Page Life Cycle.

In short, though, you have to overload the LoadViewState method.

ASP.NET has a "Load" method, ok? And it automatically allows you to overload it by giving you a Page_Load method to code.

You also can see the Initialization method, but Visual Studio warns you not to edit it.

What you don't see are the other methods that are called automatically, which include LoadViewState and LoadPostBackData.

By default, most new users create their controls in Page_Load, because they think that's what happens FIRST. That's not correct. Initialization happens first. Then LoadViewState, then LoadPostBack, then comes the Load.

So you can create your controls during Initialization (which is what Visual Studio does), or you can overload LoadViewState, like so:

  1. protected override void LoadViewState(object savedState)
  2. {
  3. base.LoadViewState(savedState);
  4. // your code here
  5. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1
Reputation: gaumehta78 is an unknown quantity at this point 
Solved Threads: 0
gaumehta78 gaumehta78 is offline Offline
Newbie Poster

Re: Drop Down Selected Text

 
0
  #5
Jul 7th, 2005
Originally Posted by DJ1UK
Hi All,
Sub Page_Load(sender as Object, e as eventargs)
If Not Page.IsPostBack Then
Dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\blah\blah\blah.mdb)
-----
-----
blah blah
blah blah
End If
Try Adding If Not page.isPostback then
And End If
in your Code & See if that Helps :lol:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC