【问题标题】:Data in a grid view wont show until I refresh the page c#在我刷新页面 c# 之前,网格视图中的数据不会显示
【发布时间】: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


【解决方案1】:

我在这里看到的两个问题...

首先,当您执行重定向时:

Response.Redirect("ViewLeague.aspx");

目标页面 (ViewLeague.aspx) 上唯一会被调用的事件是 init/load/etc。事件。单击按钮可能导致重定向,但没有单击目标页面上的按钮,因此不会使用其他此类处理程序。

因此,为了在页面加载时在网格中显示任何内容,需要在 Page_Load 处理程序中发生:

protected void Page_Load(object sender, EventArgs e)
{
    //...
    GetData();
}

第二,您在分配任何内容之前使用该值:

string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = '" + Name2 + " '";

// Later...
Name2 = (String)this.Session["showName"];

如果Name2在你使用的时候是空的,那么那个SQL子句就是:

WHERE LeagueName = ''

因此,如果没有LeagueName 值为空的记录,则将没有数据可显示。获取值后需要在查询中设置值。 (并且您应该使用参数来避免 SQL 注入。)

string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = @Name";

// Later...
Name2 = (String)this.Session["showName"];

// Later...
var cmd = new SqlCommand(SQLName);
cmd.Parameters.Add("@Name", SqlDataType.VarChar, 25).Value = Name2;
// etc.

(我不得不猜测列的类型和大小,并进行相应的调整。)

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 2015-10-31
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    相关资源
    最近更新 更多