如何在另一个 aspx 页面中通过 URL 获取值
How get a value by URL in another aspx page
我有一个建立在母版页上的表单,用于搜索用户的存在(在 sql 数据库中)。如果在提交表单后,sql 找到了某个用户,那么它应该指向一个新页面并显示该用户的详细信息。
我的主题是:如何保存用户的 ID,在下一页中只对那个用户做一个 "SELECT",并显示他的详细信息。
我可以将 javascript 与母版页一起使用吗?还是不推荐?
我的 C#/ASP.NET 代码是:
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
}
}
将您的信息存储在 Session
如果您要导航到一个单独的页面,您可以通过多种方式来保持特定值:
- 会话
- Cookies
- 查询字符串参数
这些中的任何一个都可以用来轻松地存储值,然后在下一个页面或请求中访问它们。
使用会话存储的示例
因此,在您的情况下,您可能希望将从数据库返回的值存储在这些元素之一中,然后在下一页中检索:
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
}
}
}
}
然后在您的 OtherPage 中,只需按预期从 Session 中读取它即可:
if(Session["UserID"] != null)
{
// Read the value from the Session
var userId = Convert.ToInt32(Session["UserID"]);
// Do something here
}
同样,您可以轻松地创建一个 cookie 以在多个页面中保留您的 UserID 值,或者在导航到您的其他页面时将其简单地附加到 QueryString 中:
Response.Redirect(String.Format("OtherPage.aspx?userId={0}",userId));
然后通过 :
阅读
var userId = Convert.ToInt32(Request["userId"]);
检索到 ID 后,您可以将其通过查询字符串传递到显示用户详细信息的页面,例如http://example.com/userdetails.aspx?id=1
然后用户详细信息页面可以从查询字符串中检索 ID,并显示该用户的相应详细信息。
我有一个建立在母版页上的表单,用于搜索用户的存在(在 sql 数据库中)。如果在提交表单后,sql 找到了某个用户,那么它应该指向一个新页面并显示该用户的详细信息。
我的主题是:如何保存用户的 ID,在下一页中只对那个用户做一个 "SELECT",并显示他的详细信息。
我可以将 javascript 与母版页一起使用吗?还是不推荐?
我的 C#/ASP.NET 代码是:
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
}
}
将您的信息存储在 Session
如果您要导航到一个单独的页面,您可以通过多种方式来保持特定值:
- 会话
- Cookies
- 查询字符串参数
这些中的任何一个都可以用来轻松地存储值,然后在下一个页面或请求中访问它们。
使用会话存储的示例
因此,在您的情况下,您可能希望将从数据库返回的值存储在这些元素之一中,然后在下一页中检索:
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
}
}
}
}
然后在您的 OtherPage 中,只需按预期从 Session 中读取它即可:
if(Session["UserID"] != null)
{
// Read the value from the Session
var userId = Convert.ToInt32(Session["UserID"]);
// Do something here
}
同样,您可以轻松地创建一个 cookie 以在多个页面中保留您的 UserID 值,或者在导航到您的其他页面时将其简单地附加到 QueryString 中:
Response.Redirect(String.Format("OtherPage.aspx?userId={0}",userId));
然后通过 :
阅读var userId = Convert.ToInt32(Request["userId"]);
检索到 ID 后,您可以将其通过查询字符串传递到显示用户详细信息的页面,例如http://example.com/userdetails.aspx?id=1
然后用户详细信息页面可以从查询字符串中检索 ID,并显示该用户的相应详细信息。