0

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);
    }
}
1
  • You can only have the one form tag with webforms. I'm guessing your master page has this declared. Commented Dec 21, 2017 at 17:43

1 Answer 1

0

You are adding the IngredientQuantityControl.ascx as control twice.

  protected void Button1_Click(object sender, EventArgs e)
    {
        IngredientQuantityControl uc = (IngredientQuantityControl)Page.LoadControl("IngredientQuantityControl.ascx");
        uc.SetLabelText("Halleluyah!");
        Panel1.Controls.Add(uc);
        //string text = listBox1.GetItemText(listBox1.SelectedItem);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure what you mean.
"Page.Controls.Add" and "Panel1.Controls.Add". You can't add the control twice in one page. Another option is setting dynamically the ID of elements in the "IngredientQuantityControl.ascx". You can't render multiple control with same id in single page.
How would I dynamically set an element's ID?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.