Jump to content

ASP.NET newbie request?

Whether you're a seasoned veteran or a struggling beginner, Web Radiance is the web development and web design forum for you. You'll find answers to all your HTML, CSS, SEO, and Programming needs. Pull up a chair and stay awhile.

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

ASP.NET newbie request? Rate Topic: -----

#1 User is offline   PieMasterPete 

  • W.R. Private
  • Group: Members
  • Posts: 16
  • Joined: 01-August 06

Posted 16 April 2009 - 06:31 PM

I know I'm probably going to get told off for this but I'm tired, it's past midnight and I've been working on a project all day so I'm way, way too tired to bother doing any research on the subject tonight, but I figure I'll give it a shot tomorrow unless somebody wants to be helpful and tell me where to look, lol.

Basically I've been developing a system in VB.NET that uses a database back end and I've been messing around with the database, connecting dynamically using my own random code, pulling out the info I need and displaying it in DataGridView objects and textboxes, and then allowing the users to write back to the database and update it whenever they want to change some content. It's taken me a few weeks to get the whole system working, and now it is and I'm most happy, and I figured it might be quite neat to develop a smaller version of the system using ASP.NET instead. Obviously a lot of the code is similar, but there's a few things I just have no clue how to do and I know if I can get the basics working the actual hard parts won't be that hard, since they're all so similar to VB.NET, right?

So here's the problem:

In VB.NET I can use a basic form to enter a username and a password, validate the username and password match the database, and then hide the form and launch a new form, passing it the username, and displaying all the personal details of that user. That's basically the bit I want to recreate using ASP.NET, only I have no clue how to launch a new page as it's completely different (obviously) to VB.NET.

The VB.NET equivalent of what I'm looking for is this:

Form: login.vb

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
		Dim username, password, tempUser, tempPass As String
		Dim loopNo, maxLoop As Integer
		Dim ds As New DataSet

		username = txtUser.Text
		password = txtPass.Text

		ds = DatabaseConnections.getDataSet("SELECT userID, userPassword FROM tblUsers", "tblUsers")
		maxLoop = ds.Tables("tblUsers").Rows.Count - 1
		For loopNo = 0 To maxLoop
			tempUser = ds.Tables("tblUsers").Rows(loopNo).Item("userID")
			tempPass = ds.Tables("tblUsers").Rows(loopNo).Item("userPassword").ToString

			If tempUser = username And tempPass = password Then
				Dim myDetails As New DetailsForm

				myDetails.Show()
				myDetails.loadDetails(username)
				Me.Hide()
			End If
		Next
End Sub


Then the DetailsForm form has a basic class in that's something like "Public Sub loadDetails(ByVal userID As String)" and I go from there. The problem is I have absolutely no clue how to do something similar to ASP.NET. I can obviously write the methods just fine, since they're exactly the same as VB.NET, but for actually creating a new instance of the form, and launching it in the same window, and firing up the method I want to use (which, the plan was, would just be loadDetails(ByVal userID As String) again) I have no clue what to do.

So yeah, ASP.NET newbie, if anybody wants to save me a whole bunch of time and tell me how to do the same basic plan in ASP as in VB I'd love them forever, and buy them a cookie or something. :lol:
0

#2 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 17 April 2009 - 01:09 AM

What a coincidence, I like cookies.

Anyway, there's a few ways to do what you'd like. ASP.NET has a whole membership login system with controls, so instead of writing your own login code like you've done you can simply drop some controls on to a page and be good to go. Also the database code probably isn't the normal way you'd do it in ASP.NET, so you'd need to adjust that.

So if you'd like to stick with your own login code, then you'll probably want to store the login credentials in the Session object, once that's done you'd redirect the user to your grid page.

I'm a C# coder, so there will be minor differences in this code and what you'll use. If you have any trouble i can look up the VB.NET equivalents.

Replace
myDetails.Show()
myDetails.loadDetails(username)
Me.Hide()


with

Session["username"] = username;
Session["password"] = password;
Session["loginok"] = true;

Response.Redirect("grid.aspx");


Then on your grid.aspx page you'll put your GridView and bind it to a DataSource control. In the DataSource you can specify a Session Parameter that'll grab the stored value from the Session and allow you to use it in your SQL. Something like this, though you'd want to add your Update,Insert, etc SQL as well; you can do all that via the WYSIWYG wizard.

<asp:GridView ID="gvMyGrid" runat="server" DataSourceID="dsGrid">
	</asp:GridView>
	
	<asp:SqlDataSource ID="dsGrid" runat="server" ConnectionString="<%$ ConnectionStrings:con %>"
		SelectCommand="SELECT * FROM yourtable WHERE username=@username">
		<SelectParameters>
			<asp:SessionParameter Name="username" Type="String" SessionField="username" />
		</SelectParameters>
	</asp:SqlDataSource>


Finally in the Page Load event on grid.aspx you'd want to check to make sure the user is logged in with something like this

if (Session["loginok"] == null || (bool)Session["loginok"] == false) Response.Redirect("unauthorized.aspx");

0

#3 User is offline   PieMasterPete 

  • W.R. Private
  • Group: Members
  • Posts: 16
  • Joined: 01-August 06

Posted 17 April 2009 - 11:28 AM

You are so completely and entirely the man. I owe you major cookies for that help, thanks you to I've gotten 90% of what I wanted to do working. :notworthy:

There is just one more thing I'm a little stuck on. In VB.NET obviously you can control the location of objects, since you're drawing them on a form, but in ASP.NET you can't. This isn't too big of a deal for the majority of what I'm trying to do, only in my VB.NET interface I've made a form called "Schedule" that pulls information out of the database and creates new labels on the page to display said information. The labels don't need to do anything, since literally all they are there for is to display information.

To do this in VB.NET I did this...

	Private Sub labelMake(ByVal x As Integer, ByVal y As Integer, ByVal mySize As Size, ByVal content As String, ByVal myColour As Color)
		Dim myPoint As New Point
		Dim myLabel As New Label
		myPoint.X = x
		myPoint.Y = y

		myLabel.Text = content
		myLabel.Location = myPoint
		myLabel.Size = mySize
		myLabel.BackColor = myColour
		Me.Controls.Add(myLabel)
	End Sub

(yes, I know I could have gone myLabel.Location = New Point(x, y) but yeah, I may change that later)

Anyway, I modded the code to get the information out of the database no problem, so I can now pass the label creation method the size, content and colour I want the labels to be, since I obviously don't need to pass it x or y since you can't set the location. The problem I have comes in the fact that a.) if the text is larger than the label it splits it in to multiple lines, which isn't a problem, except that it shifts the labels so that any on one line only are in line with the BOTTOM line of a multi-line label, which is just weird, and b.) I can't set the location of the label, and in VB.NET when I reached the end of a day I'd just set the y co-ordinate to + 100 and move down a line, but in ASP I'll need something else (even a simple <br /> at the end will do) but I don't know how to do that.

So basically what I'm asking is, how do you write the HTML code in ASP.NET that will allow me to solve both these problems, so instead of making labels I could just make DIV's instead or something, so I could create the same effect by doing:

"<div style=\"width: " & mySize.width & "; height: " & mySize.height & ";\">content</div>"

instead of using a label, and that'll solve all my problems then because I can have as much or as little content as I want. Obviously I'll need to figure out how to make it ignore the " character so that I can include said character in my <div style="whatever"> script without it throwing a hissy fit, but that can't be too hard to find since in Javascript I believe it's something like \" to ignore it (it's been a while since I wrote any javascript).

Also, one more time: thank you for the help so far! The Session["username"] = username; bit of code you provided is the single greatest thing I have ever learned, it's made passing between forms so freaking easy, and thanks to my day of mass experimentation in ASP.NET I'm actually really beginning to like this language now.
0

#4 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 17 April 2009 - 02:26 PM

Glad to be of help, wish there were more asp.net questions on here.

The Label or Literal control could both do what you want, in different ways. You can put HTML into the .Text of the Literal control and it will write it out to the page, so you could create your DIV in a string and assign it to the Literal.

The Label control you can set a width and height, color, etc. You can't use HTML in it, but you probably wouldn't need to since you can set the properties on the Label. If you need a line break in the Label, since you can't use <br>, use Environment.NewLine

The other thing you could do, since you're going to be assigning values to the labels from a database , is to use a FormView control bound to a DataSource control. So in the item template you'd just add the labels that you want, and in the label bind the text to the field in the database.

Like: <asp:Label runat="server" ID="lblEventName" Text='<#% Eval("eventname") %>' />
0

#5 User is offline   PieMasterPete 

  • W.R. Private
  • Group: Members
  • Posts: 16
  • Joined: 01-August 06

Posted 18 April 2009 - 10:36 AM

Using the Literal was perfect, I just created div's with absolute positioning in the same way I'd create labels in VB.NET, setting their width as mySize.width, their height as mySize.height, their left position as x and their top position as y, and hey-presto it drew them out exactly as I wanted. I had to write a little script for the generation of the content, since it wanted to write it out all on one line so I had to split it up so it made a series of lines that were no more than 13 characters and then added a <br /> on the end of each one, but hey, it worked.

In conclusion, you sir are my new hero.

:notworthy:
0

#6 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 18 April 2009 - 11:30 AM

Glad to be of help, don't hesitate to ask if anything else comes up.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users