How to get value from dynamic control on Post-back

Hi All,

This is my first post. The problem is about dynamic control.

1. In Page Load, I auto generated a datagrid.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim lmws As New LIMSWSV10.LIMSService
        Dim ds As New DataSet
        If Not IsPostBack Then
            ds = lmws.GetResultValueForShippingJob(Request.QueryString("ShippingJobID"))
            dgResults.DataSource = ds
        end if
 End Sub
2. Then Item Data Bound check box column before each data column.
Private Sub dgResults_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgResults.ItemDataBound
        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim i As Integer
            For i = 1 To (e.Item.Cells.Count - 1) Step 2
                e.Item.Cells(i).Controls.Add(New CheckBox)
        End If
    End Sub
3. How can I get my control to detect the checkbox checked event after post-back? * means that line meet problem.
Private Sub UpdateCofA()
        Dim lmws As New LIMSWSV10.LIMSService
        Dim resultcode As Integer
        Dim oncertificate As Boolean
        Dim dgitem As DataGridItem
        For Each dgitem In dgResults.Items
            Dim ckbox1, ckbox2, ckbox3 As CheckBox
            ‘can not pass from here.
            *ckbox1 = dgitem.Cells(1).Controls(0)
            *ckbox2 = dgitem.Cells(3).Controls(0)
            *ckbox3 = dgitem.Cells(5).Controls(0)
            If ckbox1.Checked = True Then
                oncertificate = True
                oncertificate = False
            End If
            resultcode = dgitem.Cells(8).Text
            lmws.UpdateResultOnCertificate(resultcode, oncertificate)
    End Sub
Actually, I cannot get my dynamic control any more. I'm looking forward to seeing your answer.
Thanks in advance!
8/23/2007 8:32:35 PM 72751 articles. 2 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 37

Any controls you create dynamically will retain their user selected/input values between postback, but they have to be re-created on postback. In your case, it looks like you are not adding the controls back when it's a postback.


Please remember to click "Mark as Answer" on the posts that helped solve your issue.
8/23/2007 8:56:21 PM

Please give more details.

8/23/2007 9:08:21 PM


Are you trying to detect when the user clicks a checkbox or are you trying to retain what was checked/unchecked between postbacks?



Please remember to click "Mark as Answer" on the posts that helped solve your issue.
8/23/2007 10:15:36 PM


Thank you for replying.  Actually, I'm trying to detect whick checkbox was checked when the user clicks a button. UpdateCofA is inside a button click method.   


8/24/2007 2:27:02 PM

Hi thewind,

Hopefully I haven't screwed you from getting an answer more quickly by members who are more experienced with the datagrid. I've done a lot with dynamic controls, though ;)

When you create a dynamic control during a Page_Load, any data changed by the user is retained between postbacks; asp handles that for you. However, you have to re-create the dynamic control when the page is posting back so that asp has a place to put the retained data. Usually, people place creation of the dynamic controls in the Page_Init event to ensure the control is present for data from the viewstate and postback to be loaded into the control. If I didn't explain that too well, check out msdn's asp page lifecycle article at

Simply put, you cannot exclude the code that creates your dynamic controls when a postback occurs. So, 

If Not IsPostBack Then
   'create my dynamic controls here
End If

will fail to retain your postback data because asp has nowhere to put it. You must be sure your dynamic controls are created everytime the page initializes/loads, regardless of a postback.

That being said, I don't know how you would handle that in the ItemDataBound event for the datagrid. If the only place you're binding your grid is when it's not a postback (which is how I read your source code), then I don't know how your dynamic checkboxes will retain their postback data.


Please remember to click "Mark as Answer" on the posts that helped solve your issue.
8/24/2007 4:38:25 PM

I did see a reference in this article to the ItemCreated event getting fired even during postback. Try moving your control creation there and see if it helps. (Number 4 in this article:



Please remember to click "Mark as Answer" on the posts that helped solve your issue.
8/24/2007 4:54:48 PM

Similar Artilces:

Value of Control created dynamically is not getting populated on post back
Hi I am creating a table at run time. If I put some value to textbox, its not getting populated after post back. If I do it on Page onInit event everything works fine.. but I cant do this as I have to make rows on button click. The code is like this Sub MakeTable() Dim ht As New Hashtable ht("1") = "1" ht("2") = "2" For i As Integer = 0 To 2 Dim tr As New TableRow For j As Integer = 0 To 1 Dim td As New TableCell Select Case (j) ...

How get value from a dynamically created control in a user control
HI, I am working with a version of IBuySpy. On one page is a form that contains a user control that lists the items the user purchased, the subtotal, shipping, etc. I wanted to add a DropDownList from which the user could select the shipper. The dropdown list works fine as long as it has .AutoPostBack = false. However, I cannot figure out how to get the value that the user selected when they finally press the submit button on the form. All attempts to locate the control and it's value have failed. I tried drillingdown from the form to the child controls, and sub child controls. N...

Help getting control name and value of dynamic controls
I have an application which creates a dynamic form consisting of an html table with web controls in the page. I need a user to be able to fill in this form and then grab the values of each control and save both the control name and the control value to a database. This is the Code that I have Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If not ispostback then Dim infotable As New Table Dim objparticipant As New Participants.ParticipantInfo Dim objCustomFields As New CustomFieldController Dim TypeId As Intege...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Dynamic control post back value
*** edited by: master4eva *** Please enclose your code in < code ></ code> tags (without the spaces). This will make your code easier to read online; therefore, encouraging a response to be faster. It is to your own benefit for your question to be answered in future. I have already done the editing to include the < code ></ code> tags for this post. ********* I'm builidng a dynamic page with server controls inside a table based on data from sql table. While myReader.Read() Dim lbl2 As Label = New Label lbl2.ID = "l...

How to maintain form controls value after user control post back?
 hello experts, I have created filter grid in & C#.My form design has contains 3 dropdown controls and Filter grid(User control).Its working fine(filter), but one problem arise.the form controls are clear when i filter a grid(post back).I tried to reterive values using view state, hidden controls, i got only null values.Anyone please help, its urgent to me. Its my user control code.FilterGrid.aspx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FilterGrid.ascx.cs" Inherits="FilterGrid" %><style type = "text/css&...

Getting back the values of dynamically created controls...
Hello, If you have a set of input controls that have been generated dynamically, say from values in a database, and after building a form page with those inputs, you want to collect the values again and update the db. However, you don't know how many inputs there are going to be. How do you efficiently gather all the information from your controls given you dont know the ids or the types of the controls??? (Is this a case where you would want to store a couple delimited strings in the viewstate to keep track of the Ids and input types?) Thanks... Actually, Just figured it out... ...

Dynamically Loading User Control and Maintaining its viewstate. And Getting Values from the control.
 I have a drop down which is populated on the page looad.  When i select an item from drop downlist i get to see two listboxes with add/removing the objects from one left to right listbox and below that i have a user control which should be populated dynamically based on the selectedvalue from drop down list.  So there are three sections that are seen once a selected value is consiedered from drop down list.  Now I am getting the user control using the below code givencase "42": viewRequired = "vw_AncCostSavingsByCarrier_adhoc";    ...

Getting control value inside the grid at the post back
Hi,       I used the DropdownList control inside the gridview and i want to retrive the value of dropdown in gridview at the page postback. Is it possible to get the value using "Request.Form"? or is any other method to reteive the value?. Looking for help   Thanks  Dropdownlist ddl = (Dropdownlist)gridview1.rows[gridviewrowno].FindControl("dropdownlistid");string selvalue  = ddl.SelectedValue; in above you have to change  gridviewrowno to the rowno you wants to retrieve.dropdownlistid to the id of the dropdownlist in...

How to get table control values on post back event?
Hi, I am using table control in, there are 3 columns in the table, values of first two columns are added manually at design time and third column has text box where user needs to add values. On button click event i need to save table (cell) values of column2,column3, but some how on post back event values are disappearing in the table. There is no limit on rows, it can be one or 20 depends on user selection. Is there any way to save table values on post back event or can i disable post back event for button so that table values will not be disappeared? Thanks ...

How to read values from dynamicly created controls when posted back?????
It seemed a good idea, making an “inquiry-tool”; users can put their questions + the way to answer (eg radiobuttons or textfields) into a database. Answers are written back into the same database. Using a placeholder I managed to get dynamicly user-written questions + ways-to-answer to the client-side. But here is the problem: How to read values from dynamicly created controls when posted back????? I really tryed very hard but I can’t. ........ Can anyone help me please?? Here’s my code: <%@ Page Language="VB" Debug="true" %> <%@ imp...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

getting focus back to the same control after post back
Hi friends!  How the control can be set focused on the same conrol after post back from where it was generated?thanks At the event of the control which causes Postback , add {        ControlID.Focus() } in this wayprotected void Page_Load(object sender, EventArgs e) {if (IsPostBack) {Control control = null; string ctrlname = Page.Request.Params["__EVENTTARGET"];if (ctrlname != null && ctrlname != String.Empty) { control = Page.FindControl(ctrlname); control.Focus(); } }   }   Note this will work for all...

Menu control : Cross page posting and posted values in nested controls?
I've created a dyrnamic template for a menu control which renders either a textbox or a linkbutton. The textbox acts as an search input field. The value of this field then acts as input to another page(on another frame actually). What happens is that clicking on the linkbutton will do a postback to Main.aspx(which resides in a frame called 'MainWindow'). When the value arrives at Main.aspx, I see the value from 'searchMovies' textbox posted into the Request.form collection as : 'NavigationMenu$ctl05$searchMovies' with the value that was keyed in. From main.aspx, does anyone know...

Web resources about - How to get value from dynamic control on Post-back -

INFOGRAPHIC: Back To School Posts Fail On Facebook
... in fear: “ Back to school .” Apparently, it has the same effect on Facebook. According to social software company Expion , back to school posts ...

Facebook brings post insights back to Pages Manager app after temporary removal
... information, they were instead directed to a screen where they could buy Promoted Posts. Now, the additional metrics have been brought back. ...

Canada Post back-to-work bill clears House
A Conservative bill ordering 48,000 Canada Post employees back to work cleared the House of Commons on Saturday after a marathon debate and several ...

Manhattan Powers Back Post-Sandy as Subway Service Expands
Parts of New York City sprang back to life as power returned to blacked-out Manhattan neighborhoods and service resumed on the busiest subway ...

Washington Post backs EPA coal regulations
'Congress hasn't faced up to the global-warming threat'

How To Get Denver Post Back Issues
Denver Post back copies that are 30 days old or less, if in stock, are available at the customer service desk in the lobby of our building in ...

Murdoch’s NY Post Backs Michael Moore’s Bush-Saudi 9/11 Claims
... media machine is conceding the argument. Shock: today’s Murdoch owned highly conservative New York Post features an opinion piece backing Michael ...

Amazon posts back-to-back profitable quarters, nets $79M in Q3
Amazon’s stock is back on the climb after the company posted a surprise profit for the second quarter in a row. For Q3 2015, the company reported ...

‘Reliable Sources with Brian Stelter’ and ‘Fareed Zakaria GPS’ Post Back-to-Back #1 Wins Among Adults ...
Reliable Sources with Brian Stelter had the most growth in cable news at 11am on Sunday mornings

Resources last updated: 1/19/2016 5:14:13 PM