简体   繁体   中英

ASP.NET: How to trigger click event for the button before page loads

I have a page that has two buttons. Each button when clicked will update a database table. Updated value should be displayed next time the page loads.

Now, when clicking the button, my page_load event fires first, keeping old values taken from the session.

How can I change that so new values will be displayed?

This is some of my code:

Page_Load:

private void Page_Load(object sender, System.EventArgs e)
{

    myDataSet = Session["myDataset"] as DataSet;

    if( myDataSet != null && myDataSet.Tables[0].Rows.Count > 0 )
    {
        myDBType = myDataSet.Tables[0].Rows[0]["dbType"].ToString();
    }
    else
    {
        myDBType = Connection.GetDBType(someid);
    }

    string displayStatus = "Value1: " + myID.Tables[0].Rows[0][0].ToString() +
        " Value2: " + myDataSet.Tables[0].Rows[0][1].ToString() +
        " Value3: " + myDataSet.Tables[0].Rows[0][5].ToString() + " DBType: " + myDBType;

    string resultScript;
    resultScript = "<script>parent.top.setStatusText('" + displayStatus + "');</script>";
    Page.RegisterClientScriptBlock("resultClientBlock",resultScript);

    SwitchDBType(intmid, permTempCheck, myDbType);
}

My button click events:

private void btnSwitchToType1_Click(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs e)
{
    myDBType = "Type1";
}


private void btnSwitchToType2_Click(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs e)
{
    myDBType = "Type2";
}

Thanks

You are missing:

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

This will make sure the Page_Load code you have there is only executed once.

I am not sure how or where you are getting intmid - you will probably need to put this line of code inside of the click events - but I am not sure so I just kept it in the Page_Load.

 SwitchDBType(intmid, permTempCheck, myDbType);

Full code will look like this:

private void Page_Load(object sender, System.EventArgs e)
{


     if(!Page.isPostback)
     {

         myDataSet = Session["myDataset"] as DataSet;

         if( myDataSet != null && myDataSet.Tables[0].Rows.Count > 0 )
         {
            myDBType = myDataSet.Tables[0].Rows[0]["dbType"].ToString();
         }
         else
         {
             myDBType = Connection.GetDBType(someid);
         }

         string displayStatus = "Value1: " + myID.Tables[0].Rows[0][0].ToString() +
             " Value2: " + myDataSet.Tables[0].Rows[0][1].ToString() +
             " Value3: " + myDataSet.Tables[0].Rows[0][5].ToString() + " DBType: " + myDBType;

         string resultScript;
         resultScript = "<script>parent.top.setStatusText('" + displayStatus + "');     </script>";
         Page.RegisterClientScriptBlock("resultClientBlock",resultScript);

         SwitchDBType(intmid, permTempCheck, myDbType);
    }
}

You don't need to trigger the event first. I suggest you extract your "business" logic into a method (that might even receive the DB type as a parameter). Then in the Page_Load you test whether this page load is a postback or not (ie occurred after a button click or not). If it's not a postback then you call the business logic method with a DB type from the session. If it's a postback then you do nothing in Page_Load , instead you change the two click handlers to call the business logic method with the appropriate DB type value.

if i understand you correctly, you cant get the click event to fire before the page load because of the page life cycle which you can read about here: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx

you can "cheat" by calling the event handler function from page init but thats not what you want....

You should place the code that loads the data based on the myDBType in the OnPreRender method. This method is called after the control events (your buttons click) is handled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM