Retrieve values from place holder

javiercuellar

Member
Joined
Apr 6, 2009
Messages
6
Programming Experience
5-10
Hi again everybody.

Hope someone can help me.

I have a place holder. Dynamiclly add controls (textbox, dropdownlist and labels) after the user clikcs into a listbox (so the dynamic loading is not done in the page_init). Until here everything fine.

I want to update a table with the values the user has selected in those controls.

In the update button, for testing I put
msgbox(Me.FindControl("lblIdArea1"))

but there is nothing there.

I'm using Vb.net

This should be easy, but after a lot of search, I can not find any clear and simple example.


Thanks for any help
 
It seems to be working just msgbox is not displaying anything because MsgBox resides on the server side. to show the msgbox you should use javascript.

VB.NET:
[SIZE=2]ClientScript.RegisterStartupScript([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetType(), [/SIZE][SIZE=2][COLOR=#a31515]"MessageBox"[/COLOR][/SIZE][SIZE=2], JavaScriptHere)[/SIZE]

Even simpler you can use Response.Write statements to examine the code.

VB.NET:
Response.Write(Me.FindControl("lblIdArea1"))

You should get the following:

ButtonSystem.Web.UI.WebControls.Label

If you want to get the lblIdArea1 text property use this code:

VB.NET:
Response.Write(Ctype(Me.FindControl("lblIdArea1"), Label).Text)

Hope this helps!
 
Sorry, to make it simple I made it confuse.

I'll put an example of what I have (only the basic and important parts):


'this is part of the code generated when user clicks in a listbox and generates the controls dynamiclly. It works fine.

VB.NET:
Protected Sub lstAlumnos_SelectedIndexChanged(.....
.....
.....
 ' This loads dynamiclly textbox and dropdownlist into placeholder 
   i=0
   While RsAlumArea.Read()
       lblIdArea = New Label()
       cmbArea = New DropDownList()
       i  += 1
       lblIdArea.Text = RsAlumArea("id_area").ToString
       lblIdArea.ID = "lblIdArea" & i.ToString
       cmbArea.DataSource = ArrayNotasArea
       cmbArea.ID = "cmbarea" & i.ToString
       tc.Controls.Add(lblIdArea)
       tc.Controls.Add(cmbArea)
    End While
...
...
end sub



'Here, when user click a button, I want to be able to read the content of the dynamic controls.

VB.NET:
Protected Sub btnUpdateTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdateTable_Click

       Dim Sql as string

       Sql ="Update Table1 set field1='" & Ctype(Me.FindControl("lblIdArea1"), Label).Text & "'"

      msgbox(sql)
End Sub

Of course insted of the msgbox, there will be an actual update instruction, but at this point I should be able to see correctlly the hole SQL string.

When I put a breakpoint in the sql=... line, it shows me that lblIdArea1 is nothing.

Hope this example is more clear than my first question.

Thanks!!!
 
Hi,

Yes, I can see the newly created controls on screen. I have the problem that after clicking in a command button, they disapear, but that is another story (or not??).



here is the definition of tc and the rest of elements I'm using.


VB.NET:
            Dim tbl As Table = New Table()
            Dim tr As TableRow
            Dim tc As TableCell

            Dim lblIdArea As Label
            Dim cmbArea As DropDownList

            PlaceHolderArea.Controls.Add(tbl)
             i = 0
            While RsAlumArea.Read()
                  tr = New TableRow()
                  tc = New TableCell()

                  lblIdArea = New Label()
                  cmbArea = New DropDownList()

                  i += 1
                  ....
                  ....
                  ....

             End While

then continues with the code in previos post
 
One of the main challenges with working with dynamically added controls is that these controls must be programmatically added on each postback.
That is, you can't just load these controls on the first page load, and then not reload them on subsequent postbacks.
Failure to explicitly add the controls on each postback will cause the controls to literally disappear on postbacks.

Understanding ASP.NET View State
 
Ok, I understand the reason of the failure, but the problem is:

This dynamic controls are generated base on an DB query. If I reload the page, I can just requery the DB and display again the controls. But, what I need is to modifiy the DB based on the changes made by the user in those controls.
This is,

1. Read DB
2. Create controls dynamically based on previous results.
3. User changes values in controls.
4. Read the NEW changes made by the user
5. Update the DB
6. Requery DB
7. Recreate controls again with new values.

My problem is number 4. I cant read the new values!!

Maybe the problem is that I am using placeholder and that is not a proper solution for this situation.
So the new question is: how to solve this problem??

Thanks
 
Last edited:
Back
Top