ASP.NET C #, you need to press a button twice for something to happen

I have a web application, the problem is that the text in the shortcut will not be updated on the first click, I need to double-click the button, I am debugging the code, and I found out that the label does not return data until the second click,

Here is my code:

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; } 
+6
source share
9 answers

Be extremely clear. The button click event occurs after the Page_Load event, which means that filtering does not apply to the first postback. It has been updated in the second postback, and you see the filtering. The simplest change to make your code work is to move all the code to your Page_Load event in OnPreRender so that a reboot occurs after the button click event.

However, a cleaner solution would probably move it to the LoadData function and call it on the Load page when it is not a postback, and also call it on the button click event after updating the filters. This will prevent any postback cycles that do not require data reloading to be called into the database:

 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(); } 

The final piece of quick advice for the aspiring ASP.Net developer is to thoroughly examine the page life cycle. Knowing the sequence of events on a page is important. Good luck.

+11
source share

Microsoft Overview The page life cycle can help in understanding the flow (and solution to your problem).

+7
source share

Button click event handlers will be executed AFTER Page_Load. Instead, try using Page_LoadComplete.

So, in your code, as soon as the button is pressed, the page_load event fires and sets the data, then the btnClick event fires and changes the data. But the data was already linked in the old form. This requires 2 clicks to work.

If you put the same page_load code in the page_loadcomplete event, this will happen after the btnClick event. This should lead to the desired result.

+6
source share

I do not see typical

 if (!Page.IsPostBack) { ... } 

in your Page_Load method, which means that your binding will happen every time the page loads, which is likely to cause your problem. I would suggest adding this to the code and see if it solves the problem.

+2
source share

JackCom, the solution worked! Thanks. And I will study the life cycle of the page. I have to add that I only have experience in software development, I just started with web development this fall.

+1
source share

I had the same problem on my page. Each time I had to double-click to make it work. This was caused by some text box and drop-down list with autorun set to true. As soon as I deleted auto-repeat, it even went smoothly, and single clicks start correctly.

+1
source share

I was stuck on this for about a week. Finally, I put the code for the Button_Click event TextChanged event, and it worked. Pressing the button causes the focus to leave the TextBox so that the event fires when the Button_Click event Button_Click not work. Very stupid. I do not like this.

I stumbled upon an interesting article that didn't really work for me, but I'm glad I read it anyway: Enter and the Click Event button

This may work in other situations.

0
source share

ASP.Net can sometimes do something weird. Today I had the same problem. I found that I put AutoPostBack = "true in the TextBox, and although it didn’t work to do what I wanted, I forgot to remove AutoPostBack from the markup. When I clicked a button on the same line of the table, the first click caused two callbacks but didn’t fire the button event. After the button is pressed a second time, when the button event is clicked. When I found an extraneous AutoPostBack from the markup, the button event started firing from the first click. that the text field has nothing to do with the button, except that the event button click refers to the contents of the text Vågå field.

0
source share

I don't know much about this, but this trick works for me:

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

All Articles