Member Avatar for DJ1UK

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:

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:

<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

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.

Member Avatar for DJ1UK

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

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:

protected override void LoadViewState(object savedState)
{
	base.LoadViewState(savedState);
        // your code here
}

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.