How to access ID of a Dyanamic control?
-
hai friends, How to access the value of the Dynamically created control in asp.net (c#)? suppose i create void page_load() { TextBox t1=new TextBox(); form1.Controls.Add(t1); } Now i want to insert the value of the textbox in the database,but ID of the TextBox not known. void insert_click(Object o,Sender e) { String s1=insert into table4 values(here i want to access the ID of the TextBox); //Sql Connections......... } plz,Reply if u have known.Thank u Friends.
-
hai friends, How to access the value of the Dynamically created control in asp.net (c#)? suppose i create void page_load() { TextBox t1=new TextBox(); form1.Controls.Add(t1); } Now i want to insert the value of the textbox in the database,but ID of the TextBox not known. void insert_click(Object o,Sender e) { String s1=insert into table4 values(here i want to access the ID of the TextBox); //Sql Connections......... } plz,Reply if u have known.Thank u Friends.
Hi, When you create the control i.e. the textbox control, assign a value to the 'id' property of the textbox. like... Textbox t1=new TextBox() t1.id="txt...." form1.controls.add(t1) later in your code you can access the textbox using this id. Rate this message :)
Gautham
-
Hi, When you create the control i.e. the textbox control, assign a value to the 'id' property of the textbox. like... Textbox t1=new TextBox() t1.id="txt...." form1.controls.add(t1) later in your code you can access the textbox using this id. Rate this message :)
Gautham
hai Gautham sir, i already tried like that, TextBox tt=new TextBox(); tt.ID="TextBox1"; but it gives compiletime error that "No such TextBox1 Control available" (that error locates the-- insert_click()) void insert_click() { String str="insert into table4 values('"+TextBox1.Text+"')"; SqlConnection.......... } bcz only in the runtime the ID 'TextBox1' assigns to the dynamic control(TextBox) then how we can specify the name during Compilation. try like that surely u got the error,plz help me sir. -- modified at 4:32 Tuesday 6th March, 2007
-
hai Gautham sir, i already tried like that, TextBox tt=new TextBox(); tt.ID="TextBox1"; but it gives compiletime error that "No such TextBox1 Control available" (that error locates the-- insert_click()) void insert_click() { String str="insert into table4 values('"+TextBox1.Text+"')"; SqlConnection.......... } bcz only in the runtime the ID 'TextBox1' assigns to the dynamic control(TextBox) then how we can specify the name during Compilation. try like that surely u got the error,plz help me sir. -- modified at 4:32 Tuesday 6th March, 2007
-
hai Gautham sir, i already tried like that, TextBox tt=new TextBox(); tt.ID="TextBox1"; but it gives compiletime error that "No such TextBox1 Control available" (that error locates the-- insert_click()) void insert_click() { String str="insert into table4 values('"+TextBox1.Text+"')"; SqlConnection.......... } bcz only in the runtime the ID 'TextBox1' assigns to the dynamic control(TextBox) then how we can specify the name during Compilation. try like that surely u got the error,plz help me sir. -- modified at 4:32 Tuesday 6th March, 2007
Ok System.Web.UI.WebControls.TextBox control = (System.Web.UI.WebControls.TextBox)this.form1.FindControl("COntrolID"); This may help you
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
Make sure that the textbox which you created is not private to any event. It should be protected or public so its accessible to all events of the aspx page
Gautham
sir, i made the page_load as public,then the results shown below. public partial class Default2 : System.Web.UI.Page { public void Page_Load(object sender, EventArgs e) { TextBox t1 = new TextBox(); t1.ID = "TextBox1"; form1.Controls.Add(t1); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con); SqlDataReader rr=com.ExecuteReader(); } } ---------------------------- CS0103: The name 'TextBox1' does not exist in the current context Line 24: { Line 25: SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); Line 26: SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con);//this line in Red Color(error portion) Line 27: SqlDataReader rr=com.ExecuteReader(); Line 28: