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>
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
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));
}
thanks alot for each answer ,
as for accessing the control TextBox tmp = (TextBox)PlaceHolder1.FindControl("input");
where is “input” is textbox id I set
You are adding the event in the wrong way.
It should be like, since you are adding a server side event:
Instead
btn.OnClientClick = "button_click";this is the way we normally add a JavaScript function.