0

It reloads the page empty when I click the button. How do I fire click event on button click? I think Page.IsPostBack is the reason it reloads the page empty instead of showing the label.

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                account account = new account();
                accountManager accountManager = new accountManager();
                group group = new group();
                groupManager groupManager = new groupManager();
                string emailAddress;

                emailAddress = HttpContext.Current.User.Identity.Name;
                account = accountManager.getAccInfoByEmailAddress(emailAddress);
                group = groupManager.getGroupLeader(account.groupNo);

                if (account.groupNo == 0)
                {
                    divMessage.InnerHtml = "You are not in any group.";
                }
                else
                {
                    try
                    {
                        Button btn = new Button();
                        btn.Text = "Click";
                        btn.Click += new EventHandler(button_Click);
                        form1.Controls.Add(btn);
                    }
                    catch (Exception)
                    {
                        divMessage.InnerHtml = "Unable to retrieve data. Please contact administrator if the problem persists.";
                    }
                }
            }
        }

.

private void button_Click(object sender, EventArgs e)
        {
            Label Label1 = new Label();
            Label1.Text = "rthfg";
            form1.Controls.Add(Label1);
        }
2
  • after you click the button the page is going to be a postback so just create your button outside of the if(!IsPostBack) Commented Jul 15, 2015 at 7:27
  • possible duplicate of ASP.NET button not firing on click event Commented Jul 15, 2015 at 7:35

1 Answer 1

1

When you click the button, or somehow else generate a postback, ASP.NET creates the page (as it always does) and tries to find the source of the request, that is the button you clicked. In your case this button is no longer on the page, so ASP.NET cannot find anything, end does not fire the event.

Resolution seems easy enough in your case - just always create the button and put it on the page, regardless of the postback:

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

Button btn = new Button();
btn.Text = "Click";
btn.Click += new EventHandler(button_Click);
form1.Controls.Add(btn);

Btw, why make the button dynamic? Dynamic controls are always harder to manage.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.