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 dynamically created controls

How to access dynamically created controls

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nettutorial
3 Posts 2 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.
  • N Offline
    N Offline
    naresh_pandey13
    wrote on last edited by
    #1

    Hi all, in my ASP.Net page i have dynamically created a table and i added dynamically created controls such as text box, list box etc. inside tables tags using literal controls My Prob is this... (1)I have call the proc to create dynamic page in Page Load, but it is not working after a post back occurs....i have to call it again outside Page Load. (2) How to acces values in those textboxes & combos (3) How to set a new value based on some calculation into the TextBoxes during/after Post back. I will be very thanful fo any valuaable suggestions. Thannxx :-D naresh This is naresh

    A 2 Replies Last reply
    0
    • N naresh_pandey13

      Hi all, in my ASP.Net page i have dynamically created a table and i added dynamically created controls such as text box, list box etc. inside tables tags using literal controls My Prob is this... (1)I have call the proc to create dynamic page in Page Load, but it is not working after a post back occurs....i have to call it again outside Page Load. (2) How to acces values in those textboxes & combos (3) How to set a new value based on some calculation into the TextBoxes during/after Post back. I will be very thanful fo any valuaable suggestions. Thannxx :-D naresh This is naresh

      A Offline
      A Offline
      Aryadip
      wrote on last edited by
      #2

      hi naresh, your question is not clear... Are you creating the dynamic page in old asp style or r u using the ASP.NET class library (classes like HtmlTable , HtmlTableCell,HtmlTableRow , TextBox ,DropDownList ,etc) to generate the page in page_load ?? anyway... herez a rough answer... it may be wrong or right... hope itz helpful... While creating the controls store the newly created control object in an arraylist. One arraylist should be present for a specific type of control you are adding... Later on during the postback, you can get the object of the control from the arraylist and use the object property to retrieve the value. class form1 : System.Web.UI { private ArrayList comboArray = new ArrayList private void Page_Load(...) { for(int i=0 ; i<10 ; i++) { DropDownList ddl = new DropDownList(); ddl.ID = "ddl_"+i.ToString(); comboArray.Add(ddl); } } private void ddl_SelectedIndexChanged(object sender,...) { string id = ((DropDownList)sender).ID; string strCount = id.SubString(id.IndexOf("_")+1); int count = Convert.ToInt32(strCount); string selVal = ((DropDownList)comboArray[count]).SelectedItem.Value; } } Hope this code gives you a fair idea of what I'm talking about... If this is a wrong reply then I'm sorry... regards, Aryadip. Cheers !! and have a Funky day !!

      1 Reply Last reply
      0
      • N naresh_pandey13

        Hi all, in my ASP.Net page i have dynamically created a table and i added dynamically created controls such as text box, list box etc. inside tables tags using literal controls My Prob is this... (1)I have call the proc to create dynamic page in Page Load, but it is not working after a post back occurs....i have to call it again outside Page Load. (2) How to acces values in those textboxes & combos (3) How to set a new value based on some calculation into the TextBoxes during/after Post back. I will be very thanful fo any valuaable suggestions. Thannxx :-D naresh This is naresh

        A Offline
        A Offline
        Aryadip
        wrote on last edited by
        #3

        Hey Naresh, Sorry for the late reply... had a chance to see ur query in my mail box today... Well... your code won't work in the postback situation simply because during postback you are restricting the creation of the page controls by the "if(this.PostBack)" condition. In simple language if I try to write your code... then it means...: If a postback happens then just initialize the variables and if the postback has not occured then call "SetPage" method. Now this statement simply demonstrates that SetPage won't be called when a postback is happening...right??? So to access the controls of the page after the postback you need to call the SetPage Method like below (I'm copy-pasting a snippet from your code... check out the comments for my modifications...itz the Page_Load delegate function) private void Page_Load(object sender, System.EventArgs e) { if(this.IsPostBack == true) { tot1 = (int.Parse(Request["dpersonsid"])); tot0 = (int.Parse(Request["dpid"])); if(tot1 <= 1) { tot2=int.Parse(Request["rent1"].ToString()) ; } else {//tot2 = int.Parse (areader["CoupleRent"].ToString()) ; tot2=int.Parse(Request["rent2"].ToString()) ; } int tot3 = (tot0)*(tot2); this.total.Text ="" ;// tot3.ToString(); TextBox1.Text = tot3.ToString(); setPage(); //<------ I have changed here ...aryadip } else { setPage(); } } Remember a basic funda... whenever you are handling a server side code... the page is re-processed and resent to the client browser for display... and thatz the reason why you need to readd the controls on the page during postback... in otherwords during postback Page_Load is first called andd then your event delegate. Hope this solves your problem... and at the same time I'm clear on the point... and hope I'm not too late... regards, Aryadip. Cheers !! and have a Funky day !!

        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