ASP.NET C#,需要按两次按钮才能发生一些事情

时间:2008-11-17 22:36:34

标签: c# asp.net

我有一个Web应用程序,问题是标签中的文本在第一次单击时不会更新,我需要单击按钮两次,我调试代码,然后我发现标签没有重新显示数据直到第二次点击后,

这是我的代码:

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlConnection connection;
string CommandText;
string game;
string modtype;
bool filter;
protected void Page_Load(object sender, EventArgs e)
{

    labDownloadList.Text = null;

    //Session variables:
    if (Session["Game"] != null)
    {
        game = Convert.ToString(Session["Game"]);
    }
    if (Session["ModType"] != null)
    {
        modtype = Convert.ToString(Session["ModType"]);
    }
    if (Session["FilterBool"] != null)
    {
        filter = Convert.ToBoolean(Session["FilterBool"]);
    }
    string ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\stian\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";
    connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
    System.Data.SqlClient.SqlDataReader reader;
    command = connection.CreateCommand();
    connection.Open();
    CommandText = "SELECT * FROM Command";
    if (filter)
    {
        CommandText = "SELECT * FROM Command WHERE Game='" + game + "' AND Type='" + modtype + "'";
    }
    command.CommandText = CommandText;
    reader = command.ExecuteReader();
    labDownloadList.Text = "";
    while (reader.Read())
    {
        string game = reader.GetString(1);
        string author = reader.GetString(2);
        string downloadlink = reader.GetString(3);
        string size = reader.GetString(4);
        string description = reader.GetString(5);
        string version = reader.GetString(6);
        string screenshotlink = reader.GetString(7);
        Int64 AmountDownloaded = reader.GetInt64(8);

        labDownloadList.Text += "Game: " + game + "<br>";
        labDownloadList.Text += "Author: " + author + "<br>";
        labDownloadList.Text += "Size: " + size + "<br>";
        labDownloadList.Text += "Description: " + description + "<br>";
        labDownloadList.Text += "Version: " + version + "<br>";
        labDownloadList.Text += "<img src='" + screenshotlink + " /><br>";
        labDownloadList.Text += "Downloaded: " + AmountDownloaded + " times<br><hr>";
        labDownloadList.Text += "<a href='" + downloadlink + "'>Download</a><br>";
    }
}

protected void Page_UnLoad(object sender, EventArgs e)
{
    Session["Game"] = game;
    Session["ModType"] = modtype;
    Session["FilterBool"] = filter;
    connection.Close();
}

protected void btnFilter_Click(object sender, EventArgs e)
{
    game = lstGames.SelectedValue;
    modtype = lstTypeMod.SelectedValue;
    filter = true;
}

10 个答案:

答案 0 :(得分:11)

要非常清楚。按钮单击事件发生在Page_Load事件之后,意味着未在第一个回发上应用过滤。它已在第二次回发时更新,您会看到过滤。让代码工作的最简单的变化是将Page_Load事件中的所有代码移动到OnPreRender中,以便在按钮单击事件之后重新加载。

然而,一个更干净的解决方案可能是将它移动到LoadData函数中,并在不是回发时在PageLoad上调用它,并在更新过滤器后在按钮单击事件上调用它。这将阻止在不需要重新加载数据的任何回发页面循环上调用数据库:

 
protected void Page_Load(object sender, EventArgs e)
{    
    if (!Page.IsPostBack)
        {   
             LoadData()
        }
}

private void LoadData()
{
    labDownloadList.Text = null;
    //Session variables:    
    if (Session["Game"] != null)
    ...
}

protected void btnFilter_Click(object sender, EventArgs e)
{    
    game = lstGames.SelectedValue;
    modtype = lstTypeMod.SelectedValue;
    filter = true;
    LoadData();
}
 

对于初露头角的ASP.Net开发人员来说,最后一条快速建议是彻底了解页面生命周期。了解页面上的事件顺序至关重要。祝你好运。

答案 1 :(得分:7)

Microsoft对Page Life Cycle的概述可能有助于理解流程(并解决您的问题)。

答案 2 :(得分:6)

按钮单击事件处理程序在Page_Load之后发生。请尝试使用Page_LoadComplete。

因此,在您的代码中,单击按钮后,page_load事件将触发并设置数据,然后btnClick事件将触发并更改数据。但是,这些数据已经被旧的形式所束缚。这就是为什么它需要2次点击才能工作。

如果您将相同的page_load代码放入page_loadcomplete事件中,它将在btnClick事件之后发生。这应该会产生预期的结果。

答案 3 :(得分:2)

我没有看到典型的

if (!Page.IsPostBack)
{
   ...
}
在Page_Load方法中

,这意味着每次加载页面时都会发生数据绑定,这很可能会导致您的问题。我建议将其添加到代码中,看看它是否能解决问题。

答案 4 :(得分:1)

JackCom,解决方案有效!谢谢。我将研究页面生命周期。 我必须补充一点,我只有软件开发方面的经验,今年秋天我刚刚开始进行Web开发。

答案 5 :(得分:1)

我的页面遇到了同样的问题。 每次我不得不点击两次才能让它工作。 这是由一些文本框和下拉列表导致autopostback设置为true引起的。 一旦我移除了自动回复,即使顺利进行,单击也会正确触发。

答案 6 :(得分:0)

我被困在这一天大约一个星期。最后,我在Button_Click事件中为TextChanged事件添加了代码并且它有效。按下按钮使焦点离开TextBox,以便在Button_click事件没有时触发事件。非常kludgy。我不喜欢它。

我看到一篇有趣的文章并没有真正对我有用,但我很高兴我还是看了它:Enter and the Button Click Event

这可能适用于其他情况。

答案 7 :(得分:0)

ASP.Net有时可以做很奇怪的事情。我今天遇到同样的问题。我发现我在TextBox上放了一个AutoPostBack =&#34;如果它没有按照我想要的那样工作,我忘了将AutoPostBack从标记中删除。当我单击同一表格行中的按钮时,第一次单击导致两次回发,但没有触发按钮事件。第二次单击按钮时,按钮单击事件被触发。当我从标记中找到无关的AutoPostBack时,按钮事件在第一次单击时开始触发。有人会认为文本框没有连接到按钮,只是按钮单击事件引用了文本框的内容。

答案 8 :(得分:0)

我对此并不了解,但这个技巧对我有用:

function pageLoad(sender, args) {
    $(document).ready(function () {
        //your stuff
    });
    $(":button").each(function() {
        $(this).click();
        //this is a trick;  click one when page load,
    });
}

答案 9 :(得分:0)

我遇到了这个问题,因为我在页面之间使用“ Server.Transfer”。当我更改为“ Response.Redirect”时,问题已解决。