Hi,
I'm new to asp. I have been creating a user control. It works fine if i place it in the main page. I have been trying to get it to display x number of controls depending on the value of the drop down box.
The code woked fine when i tried it with a label or text box but when i change it to my user control it dosn't. The page load event of the control fires as i can get it to do a messagebox, it wont display anything and if i try and load values in to a drop down list it says its not created yet.
Source code:
WebForm1.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="testcreatecontolsonfly.WebForm1" %>
<%@ Reference Control = "WebUserControl1.ascx" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml" ><
head runat="server"><title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" >
</asp:DropDownList>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="outlabel" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Code Behind page:
Partial
Public Class WebForm1Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
DropDownList1.Items.Add("1")
DropDownList1.Items.Add("2")
DropDownList1.Items.Add("3")
DropDownList1.Items.Add("4")
DropDownList1.Items.Add("5")
DropDownList1.SelectedIndex = 0
End If
For i = 1 To DropDownList1.SelectedIndex + 1
Dim a As WebUserControl1
a = New WebUserControl1()
a.ID = "mymixcontrol" & i
PlaceHolder1.Controls.Add(a)
PlaceHolder1.Controls.Add(GetLiteral("<br>"))
PlaceHolder1.Controls.Add(GetLiteral("<br>"))
Next
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim b As Control
b = PlaceHolder1.FindControl("mymixcontrol" & DropDownList1.SelectedIndex + 1)
outlabel.Text = CType(b, WebUserControl1).getNewArtist
End Sub
Function GetLiteral(ByVal text As String)
Dim rv As Literal
rv = New Literal
rv.Text = text
GetLiteral = rv
End Function
End
Classfile WebControl1.ascx:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="testcreatecontolsonfly.WebUserControl1" %>
here
<
style type="text/css">.style1
{
width: 67%;
}
.style2
{
width: 148px;
text-align: right;
}
.style3
{
width: 148px;
text-align: right;
}
.style4
{
width: 148px;
text-align: left;
}
.style5
{
width: 4px;
}
.style6
{
width: 100%;
}
.style7
{
width: 73px;
text-align: right;
}
.style8
{
width: 3px;
}
</
style><
table align="center" bgcolor="#FFFFCC" class="style1"><tr>
<td class="style4">
Mix
<asp:Label ID="MixNum" runat="server" style="text-align: left"></asp:Label>
</td>
<td class="style5">
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
Mix Letter:</td>
<td class="style5">
</td>
<td>
<asp:TextBox ID="mixLetterTextBox" runat="server" Height="18px" Width="50px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<asp:RadioButton ID="exisitingRadioButton" runat="server"
GroupName="exisNewGroup" />
</td>
<td class="style5">
</td>
<td>
<asp:DropDownList ID="ExistingDropDownList" runat="server" Enabled="False"
Height="19px" Width="395px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style3" valign="top">
<asp:RadioButton ID="newRadioButton" runat="server" Checked="True"
GroupName="exisNewGroup" />
</td>
<td class="style5">
</td>
<td>
<table class="style6">
<tr>
<td class="style7">
Name:</td>
<td class="style8">
</td>
<td>
<asp:TextBox ID="nameTextBox" runat="server" Width="305px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
Artist:</td>
<td class="style8">
</td>
<td>
<asp:TextBox ID="artistTextBox" runat="server" Width="305px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
Description:</td>
<td class="style8">
</td>
<td>
<asp:TextBox ID="descriptionTextBox" runat="server" Width="305px"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
</table>
Code Behind page:
Public
Partial Class WebUserControl1Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
ExistingDropDownList.Items.Add("Fish - Frog")
ExistingDropDownList.Items.Add("Fish - Dog")
ExistingDropDownList.Items.Add("Fish - Cat")
ExistingDropDownList.Items.Add("Bunny - Frog")
ExistingDropDownList.Items.Add("Bunny - Zebra")
ExistingDropDownList.Items.Add("Dog - Giraffe")
ExistingDropDownList.SelectedIndex = 0
End If
'MsgBox("here")
End Sub
Protected Sub newRadioButton_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles newRadioButton.CheckedChanged
ExistingDropDownList.Enabled = False
nameTextBox.Enabled = True
artistTextBox.Enabled = True
descriptionTextBox.Enabled = True
End Sub
Protected Sub exisitingRadioButton_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles exisitingRadioButton.CheckedChanged
ExistingDropDownList.Enabled = True
nameTextBox.Enabled = False
artistTextBox.Enabled = False
descriptionTextBox.Enabled = False
End Sub
Public Function getLetRef()
Return mixLetterTextBox.Text
End Function
Public Function isItNew()
Return newRadioButton.Enabled
End Function
Public Function getExistingRef()
Return ExistingDropDownList.SelectedValue
End Function
Public Function getNewName()
Return nameTextBox.Text
End Function
Public Function getNewArtist()
Return artistTextBox.Text
End Function
Public Function getNewDescription()
Return descriptionTextBox.Text
End Function
End Class
It works fine if I put <uc1:WebUserControl1 ID="WebUserControl11" runat="server" /> on the main page
Please help, its friving me nuts!!