| | |
Drop Down Selected Text
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
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:
the Drop downs ids are ddUserID, ddProjectID and ddType. ddType has the following code:
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
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:
ASP.NET Syntax (Toggle Plain Text)
Sub Page_Load(sender as Object, e as eventargs) dim objConn as new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\blah\blah\blah.mdb) objConn.Open() Const strSQL as String = "SELECT UserID FROM tblUser" Dim objCmd as New OleDbCommand (strSQL, objConn) ddUserID.DataSource = ObjCmd.ExecuteReader() ddUserID.DataBind() objConn.Close() objConn.Open() Const strSQL2 as String = "SELECT ProjectID FROM tblProject" Dim objCmd2 as New OleDbCommand (strSQL2, objConn) ddProjectID.DataSource = ObjCmd2.ExecuteReader() ddProjectID.DataBind() objConn.Close() End Sub Sub Submit(Sender As Object, e as EventArgs) if Page.IsValid then dim objUserProject as New DecSup.UserProject dim objUserProjectDetails as New DecSup.UserProjectDetails objUserProjectDetails.UserID = ddUserID.Selecteditem.Text objUserProjectDetails.ProjectID = ddProjectID.Selecteditem.Text objUserProjectDetails.Type = ddType.Selecteditem.Text objUserProject.AddUserProject(objUserProjectDetails) Response.Redirect("success.aspx") else lblMessage.Text = "Please Check - Some information is invalid. User NOT allocated to a project." end if End Sub
the Drop downs ids are ddUserID, ddProjectID and ddType. ddType has the following code:
ASP.NET Syntax (Toggle Plain Text)
<tr><td><font face="arial">Type:</font></td><td><font face="arial"> <asp:Dropdownlist id="ddType" runat="server" DataTextField="Type" DataValueField="Type"> <asp:ListItem>Member</asp:ListItem> <asp:ListItem>Controller</asp:ListItem> </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
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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.
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.
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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:
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:
ASP.NET Syntax (Toggle Plain Text)
protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); // your code here }
•
•
Join Date: Jul 2005
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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
And End If
in your Code & See if that Helps :lol:
![]() |
Similar Threads
- for validation - text box and drop down menu (JavaScript / DHTML / AJAX)
- change the font/color of the selected text on a web page through javascript (JavaScript / DHTML / AJAX)
- Remove Selected Value from the Text Area (JavaScript / DHTML / AJAX)
- Invoking a shell script on selected text (Shell Scripting)
- I've tried everything...setting font of selected text (Java)
- setting font of selected text (Java)
- text to numbers, numbers to texrt (Visual Basic 4 / 5 / 6)
Other Threads in the ASP.NET Forum
- Previous Thread: Problem with real-time app reading from serial port
- Next Thread: how to invoke postback
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax alltypeofvideos appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions control countryselector dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit editing embeddingactivexcontrol feedback fileuploader fill findcontrol flash folder form gridview gudi iis image javascript list listbox login maps microsoft mobile mouse mssql nameisnotdeclared news novell numerical opera panelmasterpagebuttoncontrols parent problem project radio redirect registration relationaldatabases reportemail richtextbox rows schoolproject search security select sessionvariables silverlight smoobjects software sql sql-server ssl tracking treeview validatedate validation vb.net videos vista visualstudio vs2008 web webapplications webdevelopment webprogramming webservice wizard xsl






