Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make like dynamic control generation and I managed to put my controls on placeholders .the problem I get when I tried to get info back from a control , since it won't "see" the controls in the handler .

i.e.
in test.aspx
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:PlaceHolder ID="PlaceHolder2" runat="server"> 

in code behind the page
protected void Page_Load(object sender, EventArgs e){
TextBox txt = new TextBox();
Button btn = new Button();
btn.OnClientClick = "button_click";
//btn.Attributes.Add("onClick","button_click"); I tried this way , yet didn't work 
btn.Attributes.Add("runat","server");
PlaceHolder1.Controls.Add(txt);
PlaceHolder1.Controls.Add(btn);}

and the handler is :
protected void button_click(Object sender , EventArgs args){
string str = txt.Text; // I don't know if this would work , cuz this method didn't run onclick
 PlaceHolder2.Controls.Add(new LiteralControl("intered :"+str));
}
Posted
Comments
Timberbird 4-Jul-11 9:12am    
Have you tried btn.Click += new EventHandler(button_click); ?

the wrong thing is type of event I'm using ^^'
it's for javascript actions
to do my work I used
btn.click += EventHandler(button_click);

and it works

as for the text control ,
TextBox tmp = (TextBox) PlaceHolder1.findControl("input");

where "input" is the text box id
 
Share this answer
 
v2
Comments
Parwej Ahamad 4-Jul-11 9:24am    
Because you didn't provide TextBox id so you will not able to find Textbox control.
Second approach is, if you are adding more than one control in Place Holder so it's better approach is, iterate all child control under PlaceHolder using PlaceHolder1.Controls property.
And make sure provide unique id for all child controls.
First of all "OnClientClick" mean you are trying to call client side jscript method.
So you need to registered click event with event handler as "Timberbird" commentes.
Go with this article just change linkbutton with Button control:
http://aquesthosting.headtreez.com/doc/3309d107-bd26-4d78-ad38-7a82933d5c50[^]
 
Share this answer
 

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