【发布时间】:2016-05-04 18:38:43
【问题描述】:
我有一个按钮,它发送会话变量并重定向到另一个页面,该页面应该读取变量并根据会话变量的字符串显示数据,它可以工作,但除非我刷新页面,否则它不会显示。这是按钮点击:
protected void btnJoin_Click(object sender, EventArgs e)
{
Button lb = (Button)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
getText1 = row.Cells[1].Text;
Session["showName"] = getText1;
Response.Redirect("ViewLeague.aspx");
}
这是 ViewLeage.aspx 的页面加载:
string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = '" + Name2 + " '";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.Session["showName"] != null)
{
Name2 = (String)this.Session["showName"];
}
}
编辑:
数据被添加到gridview的地方
private void GetData()
{
DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection("Data Source= tyler-pc\\sqlexpress; Integrated Security=true; Database=CODFANTASY2"))
{
// write the sql statement to execute
string sql = SQLName;
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}
}
// specify the data source for the GridView
ViewLeagueTable.DataSource = table;
// bind the data now
ViewLeagueTable.DataBind();
}
感谢任何帮助!
【问题讨论】:
-
首先,在实际分配任何内容之前,您在 SQL 查询中使用了
Name2值。 (注意:您使用它的方式也存在 SQL 注入漏洞。)因此该子句将始终为WHERE LeagueName = ''。其次,ViewLeague.aspx中的Page_Load事件实际上并不向用户显示任何数据。如果其他事件正在这样做,那么在触发该事件之前它不会显示任何内容。 -
不添加数据被添加的部分
-
解决了非常感谢,我需要移动 sql 字符串
标签: c# asp.net session redirect