0

I am trying to use dynamics buttons and events. When I clicked static button and I showed dynamic button. But When I clicked dynamic button I didn't work dinamikButon_Click event. What is my wrong? Sorry my language. Thx in advance.

Default.aspx.cs is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebApplication
{
public partial class _Default : Page
{
    int i = 1;
    Button dinamikButon;
    protected void Page_Load( object sender, EventArgs e )
    {

    }

    protected void btnStatik_Click( object sender, EventArgs e )
    {
        dinamikButon = new Button
        {
            Text = "Dinamik" + i,
            ID = "btnDinamik" + i,
            CommandArgument = "commandArgument",
            CommandName = "commandName"

        };
        dinamikButon.Click += dinamikButon_Click;
        panel1.Controls.Add( dinamikButon );
        i++;
    }

    void dinamikButon_Click( object sender, EventArgs e )
    {
        Label1.Text = "Merhaba dinamik butondan geliyorum.";
    }

}
}

2 Answers 2

2

that's because when the page posts back the button doesn't exist. You have to create the buttons on page Load or PreInit. Microsoft suggests PreInit You can dynamically set a master page or a theme for the requested page, and create dynamic controls.

    int i = 1;
    Button dinamikButon;
    private void Page_PreInit(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            CreateButton();
        }
    }

    protected void btnStatik_Click( object sender, EventArgs e )
    {
        CreateButton();
    }

    private void CreateButton()
    {
        dinamikButon = new Button
        {
            Text = "Dinamik" + i,
            ID = "btnDinamik" + i,
            CommandArgument = "commandArgument",
            CommandName = "commandName"

        };
        dinamikButon.Click += dinamikButon_Click;
        panel1.Controls.Add( dinamikButon );
        i++;
    }

Update:

Do do what you've asked now we have to specify that the button has been created using either viewstate, querystring or session.

In this example I'll use a session:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Page.IsPostBack)
        {
            if (Session["Created"] != null)
            {
                CreateButton();
            }
        }
    }

    private void CreateButton()
    {
        dinamikButon = new Button
        {
            Text = "Dinamik" + i,
            ID = "btnDinamik" + i,
            CommandArgument = "commandArgument",
            CommandName = "commandName"

        };

        Panel1.Controls.Add(dinamikButon);
        dinamikButon.Click += dinamikButon_Click;
        i++;
        Session["Created"] = "true";
    }

    private void dinamikButon_Click(object sender, EventArgs e)
    {
        //your action here
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Firstly thx, but it is not working correctly. Because when I run the program, static and dynamic buttons showing on screen. And then When I clicked static button, it creating and showing other dynamic button.
Thanks @prospector. In little time, I will try this.
0

To fill value in "Label1" control using dynamic button

Default.aspx

                <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicCtrl._Default" %>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head runat="server">
                    <title></title>

                    <script type="text/javascript" language="javascript">
                        function dynamicevnt() {
                            document.getElementById("Label1").innerHTML = "Merhaba dinamik butondan geliyorum.";
                            return false;
                        }
                    </script>

                </head>
                <body>
                    <form id="form1" runat="server">
                    <div>
                        <asp:Button ID="btnStatik" runat="server" Text="Click" OnClick="btnStatik_Click" />
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                        <asp:Panel ID="panel1" runat="server">
                        </asp:Panel>

                    </div>
                    </form>
                </body>
                </html>

Default.aspx.cs

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;

            namespace DynamicCtrl
            {
                public partial class _Default : System.Web.UI.Page
                {

                    protected void Page_Load(object sender, EventArgs e)
                    {

                    }
                    protected void btnStatik_Click(object sender, EventArgs e)
                    {
                        CreateButton();
                    }
                    private void CreateButton()
                    {
                        int i = 1;
                        Button dinamikButon = new Button();
                        dinamikButon.Text = "Dinamik" + i;
                        dinamikButon.ID = "btnDinamik" + i;
                        dinamikButon.OnClientClick = "return dynamicevnt();";
                        dinamikButon.Click += new EventHandler(dinamikButon_Click);
                        panel1.Controls.Add(dinamikButon);
                        i++;
                    }
                    protected void dinamikButon_Click(object sender, EventArgs e)
                    {
                        Label1.Text = "Merhaba dinamik butondan geliyorum.";
                    }
                }
            }

1 Comment

I know how can i making use javascript. But I must not use js. Thx.

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.