10 Replies - 2107 Views - Last Post: 09 September 2010 - 02:39 PM Rate Topic: -----

#1 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Server.Transer vs Response.Redirect

Posted 08 September 2010 - 08:34 AM

I have a couple input fields and a button. When my button is clicked it calls a function that checks for input errors and then redirects back to the same page. If I use Server.Transfer I lose any GET variables I append to the end of the URL and if I use Response.Redirect I lose all of my request.form data. I need all of this data.. How can I keep it all?

Thanks for any help.
Is This A Good Question/Topic? 0
  • +

Replies To: Server.Transer vs Response.Redirect

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 08:42 AM

So what is the purpose of redirecting back to the same page? To show a validation error message?

Can you post the HTML and the code that you are using?
Was This Post Helpful? 0
  • +
  • -

#3 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 08:58 AM

Yes, the intent is to make it easier to show an error message.
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim userCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source=....")
            If Request.QueryString("submit") <> "1" Or Request.QueryString("errors") = "1" Then
                ...nothing important in here
            Else
                userCon.Open()
                Response.Write("'" + Request.Form("message") + "'<br />")
                If Request.Form("callBack") = "no" Then
                    Dim sql As String = "INSERT INTO userNotes(adminID, userID, noteDate, message, callBackStatus) VALUES(@adminID, @userID, Now(), @message, '-1');"
                    Dim cmd As New OleDbCommand(sql, userCon)
                    cmd.Parameters.AddWithValue("@adminID", Session("adminID"))
                    cmd.Parameters.AddWithValue("@userID", Request.QueryString("userID"))
                    cmd.Parameters.AddWithValue("@message", Request.Form("message"))
                    cmd.ExecuteNonQuery()
                Else
                    Dim sql As String = "INSERT INTO userNotes(adminID, userID, noteDate, message, callBack, callBackStatus) VALUES(@adminID, @userID, Now(), @message, @callBack, '0');"
                    Dim cmd As New OleDbCommand(sql, userCon)
                    cmd.Parameters.AddWithValue("@adminID", Session("adminID"))
                    cmd.Parameters.AddWithValue("@userID", Request.QueryString("userID"))
                    cmd.Parameters.AddWithValue("@message", Request.Form("message"))
                    cmd.Parameters.AddWithValue("@callBack", Request.Form("callBack"))
                End If
                userCon.Close()
            End If
        End Sub
        Sub submitForm(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim error1 As String = "0"
            If Request.Form("message") = "" And (Request.Form("callBack") = "no" Or Request.Form("aspStart") = "") Then
                error1 = "1"
            Else
                '   Submit page
                If error1 = "0" Then
                    Server.Transfer("addNote.aspx?userID=" + Request.QueryString("userID") + "&submit=1")
                Else
                    Server.Transfer("addNote.aspx?userID=" + Request.QueryString("userID") + "&submit=1&errors=1")
                End If
            End If
        End Sub


Server.Transfer is acting really goofy now. If message is not = "" and callBack = "no" then it will properly insert the userID, adminID and message but its doing it twice for some reason. also, if callBack = "yes" and message <> "" it won't insert anything.. Ugh this language is rough on me.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 09:05 AM

Personally, I think that's the wrong way of doing it. You are already on the server, why not just show your messages if there is a problem. No need to make another postback to the server just to see that there were errors.

what are "message" and "callback"? Are those ASP.Net controls or HTML controls?

Basically, take the code from the Page_Load that is run when there is an error, and put it in the SubmitForm method when an error occurs. There is really no reason to do a redirect or server transfer. The page is going to be refreshed anyway.
Was This Post Helpful? 0
  • +
  • -

#5 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 09:21 AM

At what point does the page refresh currently(other than the transfer)? Is that why it's sending it to the DB twice?

Those are asp.net controls.

What about error messages in the body of the page right below or next to the textbox where the error occurred? How can I show that still?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 10:22 AM

here is a sample login page with controls..

<asp:Label id="label1" runat="server" Text="Name:" />
<asp:Textbox id="txtName" runat="server" />

<asp:Label id="label2" runat="server" Text="Password:" />
<asp:Textbox id="txtPassword" runat="server" />

<asp:Label id="lblInformation" runat="server" Visible="False" />

<asp:Button id="btnLogin" runat="server" Text="Login" onclick="btnSave_Click" />



here is the code that I would use...

protected void btnSave_Click(object sender, EventArgs e)
{
    // this is how you access ASP.Net controls
    string name = txtName.Text;
    string password = txtPassword.Text;

    // do some validation
    if (name != "admin")
    {
         // login failed
         lblInformation.Text = "Login Failed.  Please try again.";
         lblInformation.Visible = true;
    }
    else
    {
        // login successful...do something
    }

}



And that's all my code. The code is run on the server. So when the user clicks the button, it sends the request to the server, and the server starts processing it's code. After it finishes processing code, it sends the response back to the browser, which is then displayed to the user.

You can copy and paste this and watch it work. Maybe that will help you see how ASP.Net works.

It seems that you are coming from an HTML background. I would suggest that you forget about how HTML communicates because that changes. You can still remember the HTML syntax, but the rest you should not try to adapt to ASP.Net. It's different technologies and you will simply be banging your head against the wall if you try to use the same implementations of HTML in ASP.Net.
Was This Post Helpful? 1
  • +
  • -

#7 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Server.Transer vs Response.Redirect

Posted 08 September 2010 - 11:06 AM

Clean, I like it. As far as the HTML background I'm more of a PHP background but I'm thinking my teacher may not have always shown us the best ways to do things.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#8 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Server.Transer vs Response.Redirect

Posted 09 September 2010 - 10:02 AM

Wow there's a lot of things that I'd like to comment about.

When you use Response.Write in your server-side VB.NET or C# code you are going to have problems because the content written into the stream may (most likely) appear out side of the <HTML> content. This makes your HTML page invalid. You should not call the Response.Write method in your server-side code unless you plan on over writing the content of the page (ie you're sending an image instead of HTML).

You can call Response.Write in your ASPX page (using the <% %> syntax) so that you ensure that the content is written into the appropriate place in the HTML content.

You should also be very cautious about using Server.Transfer()

When you use Server.Transfer it could be configured in such a way that all of the variables and controls that were available on the first page are made available to the page that that the server transferred to. (To do this you specify that preserveForm = true...this is the second parameter of the Server.Transfer method)

For example, say you have DefaultForm1.aspx which has TextBox1 on it....now you use Server.Transfer to DefaultForm2.aspx. DefaultForm2.aspx will have access to TextBox1 that is located in DefaultForm1.aspx.

This can be very confusing and can lead to some major problems with debugging...It can also cause problems with memory if you are transferring back and forth between pages. But then again it can be very helpful if you use it properly.


I typically recommend that people new to ASP.NET stay away from Server.Transfer when they are first starting out unless they are willing to take the time to research the topic properly so that they avoid problems with memory etc.

-Frinny
Was This Post Helpful? 0
  • +
  • -

#9 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Server.Transer vs Response.Redirect

Posted 09 September 2010 - 12:31 PM

I would never publish a page with Response.Write in the code behind. I did that to quickly confirm that message indeed had a value.

Also, as soon as I read a little on Server.Transfer I made a little note to myself to remove it from all of my pages.

That's good to know about preserveForm though. That may come in handy down the road. I'm guessing that's not recommended when reloading the same page though since all the stuff in the form would then exist twice.

This is my first big site in ASP.NET and I'm learning almost everything as I go so thank you all for everything and keep the advice coming when you see a mistake!
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Server.Transer vs Response.Redirect

Posted 09 September 2010 - 02:03 PM

Also, don't use Request.Form("callBack") to get the data. Access the controls directly. They are available to you in the code behind by simply typing their "ID".
Was This Post Helpful? 0
  • +
  • -

#11 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Server.Transer vs Response.Redirect

Posted 09 September 2010 - 02:39 PM

Yep, I did catch that in your 3rd post and I've changed my code to use this. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1