简体   繁体   中英

My function is not called after a postback in asp.net webforms

I have a function called SetActionSection() which i placed in my pageload. I'm expecting it to be called but nothing happens. I get the result I want when I reload the page.

Here's the my pageload

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string fullName = GetUserFullName();
            string id = Request.QueryString["id"];
            TextBoxProjectManager.Text = fullName;

            if (id != null)
            {
                GetCMMDetails(TextBoxProjectManager.Text);

                int valid = ValidateUserAccess(id, fullName);
                if (valid > 0)
                    GetProjectPostEval();
                else
                {
                    Response.Write("You are not allowed to access this data.");
                    ActionSection.Visible = false;
                }
            }
            else
            {
                TextBoxProjectManager.Text = fullName;
                GetCMMDetails(fullName);
            }
            SetActionSection();
        }

    }

Here is SetActionSection() function which shows the a button based on the status in the database.

private void SetActionSection()
    {
        string id = Request.QueryString["id"];
        if (id == null)
        {
            LinkButtonSaveDraft.Visible = true;
            LinkButtonSubmit.Visible = true;
            ActionSection.Visible = true;
            return;
        }
        string status = GetStatus(id);
        string projectManager = GetCMM(id, "ProjectManager");
        string buco = GetCMM(id, "Buco");
        string businessExecutiveOfficer = GetCMM(id, "BusinessExecutiveOfficer");
        string i2lFunctionLead = GetCMM(id, "I2LFunctionLead");
        string user = GetUserFullName();

        if ((status.Equals("Draft", StringComparison.OrdinalIgnoreCase))
           && user.Equals(projectManager, StringComparison.OrdinalIgnoreCase))
        {
            Response.Write(status + " Draft");
            LinkButtonSaveDraft.Visible = true;
            LinkButtonSubmit.Visible = true;
            ActionSection.Visible = true;
        }

        if (status.Equals("Submitted", StringComparison.OrdinalIgnoreCase) &&
             user.Equals(buco))
        {
            Response.Write(status + " Submitted");
            LinkButtonSaveDraft.Visible = false;
            LinkButtonSubmit.Visible = false;
            LinkButtonBUCOApprove.Visible = true;
            ActionSection.Visible = true;
        }

        if (status.Equals("(Approved) - BUCO", StringComparison.OrdinalIgnoreCase) &&
            user.Equals(businessExecutiveOfficer))
        {
            Response.Write(status + " (Approved) - BUCO");
            LinkButtonBUCOApprove.Visible = false;
            LinkButtonBEOApprove.Visible = true;
        }

        if (status.Equals("(Approved) - BEO", StringComparison.OrdinalIgnoreCase) &&
            user.Equals(businessExecutiveOfficer))
        {
            Response.Write(status + " (Approved) - BEO");
            LinkButtonBEOApprove.Visible = false;
            LinkButtonI2LFunctionLeadApprove.Visible = true;
        }
        if (status.Equals("(Approved) - I2L Function Lead", StringComparison.OrdinalIgnoreCase))
        {
            Response.Write(status + " (Approved) - I2L Function Lead");
            LinkButtonI2LFunctionLeadApprove.Visible = false;
        }
    }

I have tested the SetActionSection method and it works. I just want it to be called when the user clicks the submit button. By the way. I'm redirecting to the same form.

Anything inside your if(!IsPostBack) condition will only be executed on initial load and not on submit. You could put the code you want to run on submit (postback) inside an else if you want

    if (!IsPostBack)
 {
 ....
 }
 else
 {
  SetActionSection();
 }

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8

Or put your code inside a button click event

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