Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to access ID of a Dyanamic control?

How to access ID of a Dyanamic control?

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaseasp-nettutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    senthil_rajesh_kavin
    wrote on last edited by
    #1

    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.

    G 1 Reply Last reply
    0
    • S senthil_rajesh_kavin

      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.

      G Offline
      G Offline
      gauthee
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • G gauthee

        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

        S Offline
        S Offline
        senthil_rajesh_kavin
        wrote on last edited by
        #3

        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

        G S 2 Replies Last reply
        0
        • S senthil_rajesh_kavin

          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

          G Offline
          G Offline
          gauthee
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • S senthil_rajesh_kavin

            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

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • G gauthee

              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

              S Offline
              S Offline
              senthil_rajesh_kavin
              wrote on last edited by
              #6

              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:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups