最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - VB.net and QueryString Parameters - Stack Overflow

programmeradmin0浏览0评论

I have legacy web app with two asp text boxes on a website that I would like to autofill with info from URL parameters and also allow a user to type their info if the get to the site without URL parameters. The code below works to get the URL parameters. But does accept user typed information, it recognizes it as blank when no URL parameters are present.

The URL is

My VB code behind is as follows:

Me.txtPIN.Text = Request.QueryString("memberid")

Edit. Additional Code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.txtPassword.Text = Me.txtPassword.Text & " "
    Me.txtPassword.Text = Trim(Me.txtPassword.Text)
    Me.txtPassword.Text = Request.QueryString("pwd")

    Me.txtPIN.Text = Request.QueryString("memberid")
End Sub



Private Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
    Dim pin As String = Me.txtPIN.Text
    Dim password As String = Me.txtPassword.Text

    Dim bl As New BL
    Dim ol As New oLogin

I have legacy web app with two asp.net text boxes on a website that I would like to autofill with info from URL parameters and also allow a user to type their info if the get to the site without URL parameters. The code below works to get the URL parameters. But does accept user typed information, it recognizes it as blank when no URL parameters are present.

The URL is https://www.example.com/example?memberid=12345

My VB code behind is as follows:

Me.txtPIN.Text = Request.QueryString("memberid")

Edit. Additional Code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.txtPassword.Text = Me.txtPassword.Text & " "
    Me.txtPassword.Text = Trim(Me.txtPassword.Text)
    Me.txtPassword.Text = Request.QueryString("pwd")

    Me.txtPIN.Text = Request.QueryString("memberid")
End Sub



Private Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
    Dim pin As String = Me.txtPIN.Text
    Dim password As String = Me.txtPassword.Text

    Dim bl As New BL
    Dim ol As New oLogin
Share Improve this question edited Feb 6 at 18:21 Andrew King asked Feb 6 at 18:00 Andrew KingAndrew King 112 bronze badges 3
  • What is "user typed input" in this case? The only code shown is reading from the request URL query string. If you're also trying to read data from somewhere else, such as from a text input on the page, where do you do that? Please clarify. – David Commented Feb 6 at 18:14
  • edited original with the code that was written before I got this. I added the two query string request lines. – Andrew King Commented Feb 6 at 18:22
  • See my post below as to how you deal with such issues - this issue will exist for near every web page you write - even ones that don't use QueryString. – Albert D. Kallal Commented Feb 6 at 18:31
Add a comment  | 

2 Answers 2

Reset to default 0

Page_Load is invoked on every request. So you're over-writing user-entered values when posting back to the server.

You can indicate to WebForms that certain logic should only happen in an initial page load and not on post-backs by wrapping that logic in a condition that checks IsPostBack:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.txtPassword.Text = Me.txtPassword.Text & " "
        Me.txtPassword.Text = Trim(Me.txtPassword.Text)
        Me.txtPassword.Text = Request.QueryString("pwd")

        Me.txtPIN.Text = Request.QueryString("memberid")
    End If
End Sub

We probably need more code.

However, the issue of course is that if you place code in the page load event to grab the parameters and fill out the 2 text boxes? Then that code should only EVER run on first page load.

Keep in mind that for any button click, or event on the web page? Then the page load event fires EACH and every time.

So, if the user enters some values into the text boxes, and hits some button?

Then the page load event will run first (and probably runs your code to get the query parameters, and if none, then the textboxes are set to blank).

After the page load event runs, then your button code stub runs. So, in EVERY case, and in ALL cases, the page load event runs for each post-back (any button click or event that posts back).

So, the simple solution?

You want to run code ONLY on the first REAL page load. In fact, we often have all kinds of code to load up a drop down list (DDL), load up a GridView, or even your case to setup the text boxes. However, if we say re-load a drop down list with some items?

Then user will select a drop down item, hit some button.

But, now your page load event runs first (again), and loads up the DDL, and then your code to process the user's DDL choice runs.

But, since your page load runs first before such events, and runs each and every time? Well, if we re-load the DDL, then we lose the user's choice!

So, we need to setup code in our page load event to only run ONE time, and on the REAL first page load. You thus adopt use of the IsPostBack flag.

Hence, your page load event will look like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        ' this code ONLY runs on the first REAL page load
        ' Code here thus only runs one time, and only on first page load

        If Request.QueryString("memberid") <> "" Then
            txtPIN.Text = Request.QueryString("memberid")
        End If

        ' code here to load up DDL's, load up grid view
        ' load up and set some default values for controls etc.

    End If

End Sub

So, we only check for the query on the first real page load. We have to do this, since if the user were to edit the text box, and we each and every time pull some value from the QueryString (which will work even if the paramter was not provided, then the value is a emptry string ("").

So, we can't have that code run each and every time, since then each and every time the page loads, it will overwrite any value(s) entered into the text boxes by the user.

So, keep in mind that the page load event run each and every time on any post-back actions by the user. So, page load runs first, and then your button click code stub runs.

As a result?

You really can't build a working page without adding that If Not IsPostBack code stub. In fact, I can easy state that the last 200 .aspx web pages all have this if/then block for IsPostBack. In fact, you can't build a correctly working asp.net page without use of this if/then block in the page load event.

发布评论

评论列表(0)

  1. 暂无评论