0

I have the following code inside the CS file of the page, i am trying to add an html button inside a div in the server side to close the alert modal dialog. When I add the event to the button and try to fire it. The event doesn't fires. So, where is the problem ?

public Alert(HtmlGenericControl alert ,string alertMessage)
        {
            vAlert = alert;

            alert.Attributes.Add("class", "uk-modal");
            alert.Attributes.Add("aria-hidden", "true");
            alert.Attributes.Add("style", "display: none; overflow-y: scroll;");

            HtmlGenericControl innerDiv = new HtmlGenericControl();
            innerDiv.TagName = "div";
            innerDiv.Attributes.Add("class", "uk-modal-dialog");
            innerDiv.Attributes.Add("style", "top: 35.5px;text-align:center; padding:30px;");

            HtmlInputButton btnclose = new HtmlInputButton();
            btnclose.Attributes.Add("type", "button");
            btnclose.Attributes.Add("id", "alert_close");
            btnclose.Attributes.Add("runat", "server");
            btnclose.Attributes.Add("class", "uk-modal-close uk-close");
            btnclose.Attributes.Add("style", "padding:15px;");
            
            btnclose.ServerClick += new EventHandler(btnclose_ServerClick);
            innerDiv.Controls.Add(btnclose);
            

            HtmlGenericControl p = new HtmlGenericControl();
            p.TagName = "p";
            p.InnerText = alertMessage;
            innerDiv.Controls.Add(p);

            alert.Controls.Add(innerDiv);

            ShowAlert(alert);
        }

        private void btnclose_ServerClick(object sender, EventArgs e)
        {
            HideAlert(vAlert);
        }

Can you help me ?

5
  • add the part with Button's part from shtml/aspx file Commented Sep 19, 2018 at 11:32
  • how can you give me an example ? Commented Sep 19, 2018 at 11:41
  • ¿In wich event are you adding the controls? Page life cycle I mean. Commented Sep 19, 2018 at 12:02
  • Thank you for your recommendation. But i am trying to show/hid a dialog in the button click event in the server side without using either a javascript or a jquery Commented Sep 19, 2018 at 12:03
  • @YusufShayah At what event are you creating controls. You should suppose to do it Page_Init Commented Sep 19, 2018 at 15:34

1 Answer 1

2

The button probably doesn't exist at the time when the server callback is executing.

You are using a dynamically created button. In order for it to be able to trigger a server side method, the button needs to be added and have it's event handler bound in an early stage of the page life cycle, such as On_Init.

We don't know when you call your "Alert" method, but it might very well too late. It also needs to be called on every postback - or the link between button and handler won't be there when the button causes a postback.

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.