动态添加多个按钮并在点击时填充PlaceHolder [英] Add Multiple Button's Dynamically and populate PlaceHolder on clicking
问题描述
亲爱的朋友们,
我正在创建多个列,动态地在asp页面的每一列中都有一个按钮。
但是,如何点击生成这些多个按钮的事件。
例如:我用户输入了3列并提交了它,然后将添加三列,其中一个按钮和一个在每一栏中放置持有人。现在,当用户点击该按钮时,我想要一些文本框添加到备受尊重的地方持有人。
请帮帮我朋友,,, ,
我在下面的代码中做了什么,,,
但是当我点击动态创建的按钮时,所有动态添加的按钮都消失了。
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.
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.
}
推荐答案
如何生成这些多按钮的点击事件。
创建按钮时,向其添加事件处理程序,然后在页面中处理相同的事件。
示例:
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:
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:
void btn1_Click(object sender, EventArgs e)
{
// Handle the event. Do your stuff.
}
查看我已更改的代码。
Take a look in the code i have changed.
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.
}
可能是您在上一个按钮的顶部添加了按钮?确保将它们彼此远离或并排放置。
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.
这篇关于动态添加多个按钮并在点击时填充PlaceHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!