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!

Adding User Control Programmatically 1

Status
Not open for further replies.

jino

Programmer
Apr 1, 2004
41
CA
Hi ,

I am trying to add an user control to my aspx page programmatically. I need to add multiple instances of my user control , so I have to do it dynamically. While doing this I am getting an error that the buttons in my user control should be inside a <form> tag. If I add my buttons inside a <form> tag, it gives another error. I guess that is because your aspx page can have only one <form> tag. Can someone help me? Any help would be appreciated. Thanks in advance.

User Control Code

Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="MyButtons.ascx.vb" Inherits="WebApplication1.MyButtons" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] %>
<table>
	<tr>
		<td>
			<asp:Button id="btnBack" runat="server" Text="Back" ForeColor="white" BackColor="maroon"
				BorderColor="maroon" OnClick="btnBack_Click"></asp:Button>
		</td>
		<td>
			<asp:Button id="btnNext" runat="server" Text="Next" ForeColor="white" BackColor="maroon"
				BorderColor="maroon" OnClick="btnNext_Click"></asp:Button>
		</td>
	</tr>
</table>
<script language="vb" runat="server">

sub btnBack_Click(Sender as Object, E as EventArgs)
Response.Redirect("PersonalInformationPage.aspx")
End sub

sub btnNext_Click(Sender as Object, E as EventArgs)
Response.Redirect("ResultsPage.aspx")
End sub
</script>
<br>
<br>

Page_Load Event of aspx page

Code:
        Dim myControl1 As MyButtons = CType(LoadControl("MyButtons.ascx"), MyButtons)
        Controls.Add(myControl1)


Error Message I got

Control '_ctl0_btnBack' of type 'Button' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control '_ctl0_btnBack' of type 'Button' must be placed inside a form tag with runat=server.
 
I believe you need to use the LoadControl method to dynamically load a usercontrol.

Jim
 
Whoops ignore that..I didn't see your code.. sorry.. I will continue to look into it.
 
Ok, the problem is the way you add the control to the page. You need to look for the FORM then add it:
Code:
Me.FindControl("Form1").Controls.Add(myControl1)
 
Hi,

The user control I am adding has a drop down list. I want to add the selected value of the drop down to the session. Since there are multiple instances of the same user control added dynamically, the controls are also named dynamically. How will I access the values in the drop downs and add them to session during run time?

Thanks for all your help.

Jino
 
Hi,

I have been reading about the AddHandler methods. But all the examples I looked into are directly dealing with server controls rather than an user control. Maybe it is too simple, that I did not understand.

My Usercontrol 'myControl1' has two dropdowns - 'dropDown1' and 'dropDown2'. I might add one to six instances of this user control. The user may not change any values in some of the drop downs. But when i am unloading the Page, I want to be able to add the values from all the drop downs into the session. How do I code this? Please help since the VS.NET help was not very useful to me.

How do I code the AddHandler method if my usercontrol has buttons in it?

Thanks

Jino

 
Put the handlers inside the user control, then raise a public or friend event to notify the parent control/page what happened.


Public Class myUserControl
...
Public Event SaveWasClicked

Private Sub btn_Save_Click(sender as ... etc.
try... etc.

Raiseevent SaveWasClicked

End Sub

End Class

Brian Begy
Chicago Data Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top