ASP.NET C#,需要按两次按钮才能发生一些事情
发布时间:2020-12-16 04:23:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个Web应用程序,问题是标签中的文本在第一次单击时不会更新,我需要单击按钮两次,我调试代码,然后我发现标签没有重新获取数据直到之后第二次点击, 这是我的代码: System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();S
我有一个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:inetpubwwwrootstianApp_DataDatabase.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; } 解决方法
要非常清楚.按钮单击事件发生在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开发人员来说,最后一条快速建议是彻底了解页面生命周期.了解页面上的事件顺序至关重要.祝好运. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 有人可以帮我理解这个堆栈跟踪吗?
- asp.net-mvc – 如何为ASP.NET 4.5 Web API创建MultipartFo
- asp.net-web-api – Web API ODataResult始终序列化为Atom
- asp.net-mvc – 有没有人使用史蒂夫·桑德森的MvcIntegrati
- asp.net – 异步操作方法和IO完成端口
- asp.net-mvc-3 – 如何从部分视图将JavaScript渲染到Master
- asp.net-mvc-3 – 将部分视图返回到同一视图
- ASP.NET核心项目:如何防止编写原型
- asp.net-mvc – MVCGrid.Net排序问题 – sortdirection
- ASP.NET MVC捆绑缓存. (检测css文件更改)(内部行为)
推荐文章
站长推荐
- 使用TypeScript装饰器会导致错误
- asp.net – 在Cookie中存储多个值
- asp.net-mvc – 其中Simple Injector等效于Struc
- asp.net – 如何删除日历的最后一周
- 从ASP.NET导出的word文件中添加页眉/页脚
- asp.net-mvc-4 – / signalr / hubs未加载到asp.
- asp.net-mvc – lowerCamelCase中的ASP.NET MVC
- asp.net – 网站在解决方案构建后需要很长时间才
- asp.net – Webservice方法返回XmlDocument,Refe
- asp.net-mvc – MVC:存储库和服务
热点阅读