I'm trying to generate a user control, which consists of a label, dropdown menu, and text box, dynamically so I can set the value of the label. When I run it, I have an issue with the dropdown and textbox because they need to be in a form tag with runat server. When I add the form tag, the compiler complains that there can only be one form tag. Please help. This is my code:
User Control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="IngredientQuantityControl.ascx.cs" Inherits="IngredientQuantityControl" %>
<asp:Label ID="IngredientDisplay" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem Value=null >Please Choose a Unit</asp:ListItem>
<asp:ListItem Value="Unit">Unit(s)</asp:ListItem>
<asp:ListItem>Cup</asp:ListItem>
<asp:ListItem>Tbsp</asp:ListItem>
<asp:ListItem>Tsp</asp:ListItem>
<asp:ListItem>To Taste</asp:ListItem>
<asp:ListItem>Milligrams</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class IngredientQuantityControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetLabelText(String s)
{
IngredientDisplay.Text = s;
}
}
Dynamically Adding to the panel
protected void Button1_Click(object sender, EventArgs e)
{
Page.Controls.Add(LoadControl("IngredientQuantityControl.ascx"));
IngredientQuantityControl uc = (IngredientQuantityControl)Page.LoadControl("IngredientQuantityControl.ascx");
uc.SetLabelText("Halleluyah!");
Panel1.Controls.Add(uc);
//string text = listBox1.GetItemText(listBox1.SelectedItem);
}
}