Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends,

I am creating multiple columns with one button in each column in asp page dynamically.
But, how click event of these multiple button is generated.

For example: I user entered 3 columns and submit it,Then three columns will be added with one "button" and one "Place Holder" in each column.Now I want when user click on that button ,Some text boxes added to the respected Place Holder.

Please help me friends,,,,


What I did is in the code below,,,

but When I click on dynamically created button ,,All dynamically added button getting disappeared.

C#
protected void common_click(object sender,EventArgs e)
    {
             
        int cellCtr;
        // Current cell counter
        int cellCnt;
        int btnId=1;
        int phId=1;
       
        cellCnt = int.Parse(TextBox1.Text);

        for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
            {
                // Create a new cell and add it to the row.
                HtmlTableCell tCell = new HtmlTableCell();
                RowMain.Cells.Add(tCell);

                PlaceHolder ph = new PlaceHolder();
                Button btn = new Button();
                btn.ID = "btn" + btnId;
                btn.Text = "AddControls";
                btn.Click += new EventHandler(btn_Click);
                ph.ID = "ph" + phId;

                tCell.Controls.Add(btn);
                tCell.Controls.Add(ph);
                TextBox txt = new TextBox();
                ph.Controls.Add(txt);
                btnId = btnId++;
                phId = phId++;
                
            }
        
    }

    void btn_Click(object sender, EventArgs e)
    {
        Button isButton1 = (Button)sender;
        string test = isButton1.ID;
        Response.Write("Was it Button1 = " + test );

        
        //Here I want to add some controls to the particular place holder.
    }
Posted
Updated 23-Jul-18 5:04am
v5

how click event of these multiple button is generated.
When you create a button, add the event handler to it and then handle the same event in your page.

Example:
C#
Button btn1 = new Button();
btn1.ID = "btnONE";
btn1.Text = "Press Me";
btn1.Click += new EventHandler(btn1_Click);
MyPlaceHolder.Controls.Add(btn1);

Somewhere in the same page:
C#
void btn1_Click(object sender, EventArgs e)
{
    // Handle the event. Do your stuff.
}
 
Share this answer
 
Comments
rahulbhadouria 24-May-12 2:41am    
But this is for one button which is dynamically created..
But I want multiple Button.
AhsanS 24-May-12 2:57am    
The solution given by Sandeep is just an example for you to get started. For creating multiple buttons you can follow above example and you have to give different id and that is it. You can create as many as you like. But make sure that id is different.
rahulbhadouria 24-May-12 3:01am    
What I did is in the code below,,,,

but When I click on dynamically created button ,,All dynamically added button getting disappeared.


protected void common_click(object sender,EventArgs e)
{

int cellCtr;
// Current cell counter
int cellCnt;
int btnId=1;
int phId=1;

cellCnt = int.Parse(TextBox1.Text);

for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
HtmlTableCell tCell = new HtmlTableCell();
RowMain.Cells.Add(tCell);

PlaceHolder ph = new PlaceHolder();
Button btn = new Button();
btn.ID = "btn" + btnId;
btn.Text = "AddControls";
btn.Click += new EventHandler(btn_Click);
ph.ID = "ph" + phId;

tCell.Controls.Add(btn);
tCell.Controls.Add(ph);

btnId = btnId++;
phId = phId++;

}

}

void btn_Click(object sender, EventArgs e)
{
Button isButton1 = (Button)sender;
string test = isButton1.ID;
Response.Write("Was it Button1 = " + test );

//Here I want to add some controls to the particular place holder.
}
Take a look in the code i have changed.

C#
protected void common_click(object sender,EventArgs e)
    {
             
        int cellCtr;
        // Current cell counter
        int cellCnt;
        int btnId=1;
        int phId=1;
       
        cellCnt = int.Parse(TextBox1.Text);

        for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
            {
                // Create a new cell and add it to the row.
                HtmlTableCell tCell = new HtmlTableCell();
                

                PlaceHolder ph = new PlaceHolder();
                Button btn = new Button();
                btn.ID = "btn" + btnId;
                btn.Text = "AddControls";
                btn.Click += new EventHandler(btn_Click);
                ph.ID = "ph" + phId;

                ph.Controls.Add(btn);
                tCell.Controls.Add(ph);
                RowMain.Cells.Add(tCell);
                btnId = btnId++;
                phId = phId++;
                
            }
        
    }

    void btn_Click(object sender, EventArgs e)
    {
        Button isButton1 = (Button)sender;
        string test = isButton1.ID;
        Response.Write("Was it Button1 = " + test );

        //Here I want to add some controls to the particular place holder.
    }
 
Share this answer
 
Comments
rahulbhadouria 24-May-12 3:39am    
Still the same problem ,,,The problem is that when i click on dynamically created button its event is not getting fired.....

And all controls are getting disappeared
AhsanS 24-May-12 3:45am    
I assume that RowMain is a HtmlTableRow.
Are you adding this to the main table?
rahulbhadouria 24-May-12 3:47am    
Yes you r right...
It could be that you are adding buttons on top of the previous buttons? Make sure to position them away from each other or side by side.
 
Share this answer
 
Comments
Richard Deeming 23-Jul-18 14:02pm    
Asked, answered, and solved SIX YEARS AGO.

Stick to answering recent questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900