简体   繁体   中英

How get a value by URL in another aspx page

I've a form, built on a masterpage, in that intend to search for the existence of an user (in a sql database). If after submission of the form, the sql find some user, then it should direct to a new page and to show the details of that user.

My subject is: How can i save the ID of user for, in the following page to simply do only a "SELECT", of that user, and show his details.

May i use javascript, with masterpages, or it is not recommended?

My C#/ASP.NET Code is:

    protected void ClientSearch(object sender, EventArgs e)
    {
        int i;
        string query;
        con.Open();
        sqlcomm.Connection = con;
        query = "Select * FROM Cliente WHERE Cliente.nome='" + textboxclientname.Text + "'AND Cliente.apelido='" + textboxapelido.Text + "'";

        sqlcomm.CommandText = query;
        SqlDataReader dr;
        dr = sqlcomm.ExecuteReader();
        dr.Read();
        if (dr.HasRows)
        {
            //new page
            //At that page, user data may be displayed in textboxes, to be updated


        }
        else
        {
            //show error page
        }

    }

将您的信息存储在会话中

If your are navigating to a separate page, you have quite a few options for persisting a particular value across :

  • Session
  • Cookies
  • QueryString Parameters

Any of these can be used to easily store values and then access them within the next page or request.

Example Using Session Storage

So in your case, you would want to store the value returned from your database within one of these elements and then retrieve in on your next page :

protected void ClientSearch(object sender, EventArgs e)
{
    using(var con = new SqlConnection("{your-connection-string}"))
    {
        var query = "SELECT * FROM Cliente WHERE nome = @name AND apelido = @apelido";
        using(var comm = new SqlCommand(query,con))
        {
             con.Open();
             // User parameters to avoid SQL Injection
             comm.Parameters.AddWithValue("@name",textboxclientname.Text);
             comm.Parameters.AddWithValue("@apelido",textboxapelido.Text);
             // Get your result and store it in the Session
             var id = -1;
             Int32.TryParse(Convert.ToString(comm.ExecuteScalar()),out id);
             if(id != -1)
             {
                  // Store your ID in the Session
                  Session["UserID"] = id;
                  // Go to your other page
                  Response.Redirect("OtherPage.aspx");
             }
             else
             {
                  // No user was found, do something
             }
        }
    } 
}

And then within your OtherPage, simply read it out of the Session as expected :

if(Session["UserID"] != null)
{ 
     // Read the value from the Session
     var userId = Convert.ToInt32(Session["UserID"]);
     // Do something here
}

Likewise, you could easily create a cookie to persist your UserID values across multiple pages, or simply append it to the QueryString when navigating to your other page :

Response.Redirect(String.Format("OtherPage.aspx?userId={0}",userId));

and then read it via :

var userId = Convert.ToInt32(Request["userId"]);

Once you've retrieved the ID, you could pass that on the querystring to the page which displays the user details, eg http://example.com/userdetails.aspx?id=1

Then the userdetails page can retrieve the ID from the querystring, and display the appropriate details for that user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM