Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindControl()

Status
Not open for further replies.

digimortal

Programmer
Oct 12, 2003
28
TR
Hi,
I'm creating a number of radiobuttonlists at runtime and assign them different IDs but when I try to access them using FindControl, it can not find them:

This is what I'm trying to do
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myControl1 As Control = FindControl("HoSe1")
        If (Not myControl1 Is Nothing) Then
            Label1.Text = myControl1.NamingContainer.ID.ToString
        Else
            Label1.Text = "Control not found....."
        End If

    End Sub
and this is how I create them:
Code:
            Dim HType As New TableCell
            Dim HourSelect As New RadioButtonList 
            HourSelect.ID = "HoSe" & i
            HType.BackColor = HType.BackColor.FromArgb(204, 204, 255)
            HType.ForeColor = HType.ForeColor.Black
            HType.Font.Bold = True

            HourSelect.RepeatDirection = HourSelect.RepeatDirection.Horizontal
            HourSelect.CellPadding = 3
            HourSelect.CellSpacing = 3
            HourSelect.DataValueField = WorkHoursDS1.WorkHours.TypeIDColumn.ToString
            HourSelect.DataTextField = WorkHoursDS1.WorkHours.TypeNameColumn.ToString

            HourSelect.ForeColor = HourSelect.ForeColor.White
            HourSelect.Font.Bold = True
            HourSelect.DataSource = WorkHoursDS1.WorkHours
            HourSelect.DataBind()
            RadioButtonList1.DataBind()

            HType.Controls.Add(HourSelect)
            Table3.Rows(i).Cells.Add(HType)
and I create them at a button click event..
By the way I don't know how to access the value of a dynamically created control...
Please Help..
Thnx in advance
 
there is a logical error i ur code:

When the page loads you create the control at runtime using lets say Button1. In Button2 you have the reading event code.

When you click Button1 the controls are created (at run time). When you click on button2 since button1 click is NOT called the controls are NOT created. Therefore your findcontrol() method doesnt work. Why not create the controls in page_load or page_prerender() events???

Known is handfull, Unknown is worldfull
 
Controls created on the fly will not persist unless you add them during page_initialize or page_load and re-add them each time the page re-post back to the server.

You can add the max number in advance and then set thier visible property to True or False as needed.

You could let a repeater add them then you could easliy
assign thier OnClick event to some public sub in your code.
<ItemTemplate>
<asp:button onclick="MyPublicSubForAll" CommandName="delete" CommandArgument="ID101" />
</ItemTemplate>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top