how to get the controls that are added dynamically to the panel bar

2 posts, 0 answers
  1. chaitanya elluri
    chaitanya elluri avatar
    14 posts
    Member since:
    Nov 2009

    Posted 15 Mar 2010 Link to this post

    Hi All,

    I have a requirement where i need to add the controls to the page dynamically

    here i have 5 sections under each section there will be fields that are to be added dynamically....

    i am adding these 5 sections as radpanelitems....

    and under each panel item i added a table in which the controls based on the datatype are added to the cells as rows to the table
    dynamically.

    in brief(that is if i have the datatype for a field as varchar(50) then i will add the textbox control... like wise..)

    at the end of the page ... i have add button...

    on the click of 'add' button i need to get the values entered in the controls by the end user.

    how do i...

    i tried using Request.Form["txtRequirementname"].toString()...

    it isthrowing the exception 'object reference is not set to an instance"

    did i do some thing mistake here... or is there any other way to retrieve the values of the controls.

    below is the code written to add the controls to the table.

    and also i attached a image of file 

     

    private void AddControlstoRow(HtmlTableRow htRow, FieldDefinition fd, double labelwidth, double controlwidth, int colspan)

     

    {

     

    if (!fd.FieldDataTypeReference.IsLoaded)

     

    {

    fd.FieldDataTypeReference.Load();

    }

     

    //for adding label control

     

     

    HtmlTableCell htcell = new HtmlTableCell();

     

    htcell.Width =

    Unit.Percentage(labelwidth).ToString();

     

    htcell.Align =

    "left";

     

     

    Label lblName = new Label();

     

     

    if (fd.ListPageMandatory == "Y")

     

    {

    lblName.Text = fd.FieldLiteral +

    ":" + "<font color='red'>&nbsp;*</font>";

     

    }

     

    else

     

    {

    lblName.Text = fd.FieldLiteral +

    ":";

     

    }

     

    //lblName.ForeColor = System.Drawing.Color.Red;

     

     

    //lblName.CssClass = strCellCss;

     

    htcell.Controls.Add(lblName);

    htRow.Cells.Add(htcell);

     

    //adding control as per fielddatatypeid

     

     

    HtmlTableCell htControlcell = new HtmlTableCell();

     

    htControlcell.Width =

    Unit.Percentage(controlwidth).ToString();

     

    htControlcell.Align =

    "Left";

     

    htControlcell.ColSpan = colspan;

     

    TextBox txtBox = new TextBox();

     

    txtBox.EnableViewState =

    true;

     

    txtBox.ID =

    "txt" + fd.FieldLiteral;

     

     

    switch (fd.FieldDataType.FieldDataTypeID)

     

    {

     

    case 12:

     

    txtBox.CssClass =

    "TextBox";

     

    htControlcell.Controls.Add(txtBox);

     

    if ((fd.FieldLiteral.ToUpper().Trim().Contains("TIME") || fd.FieldLiteral.ToUpper().Trim().EndsWith("EFFORT") || fd.FieldLiteral.ToUpper().Trim().Contains("DURATION")) && (fd.FieldDataType.FieldDataTypeID != 16))

     

    {

     

    Label lblUnits = new Label();

     

    lblUnits.Text =

    "&nbsp;&nbsp;<I>[Hrs]</I>";

     

    htControlcell.Controls.Add(lblUnits);

    }

     

    break;

     

     

    case 13:

     

    txtBox.CssClass =

    "TextBox";

     

    txtBox.TextMode = System.Web.UI.WebControls.

    TextBoxMode.MultiLine;

     

    txtBox.Width = System.Web.UI.WebControls.

    Unit.Pixel(550);

     

    txtBox.Height = System.Web.UI.WebControls.

    Unit.Pixel(50);

     

    htControlcell.Controls.Add(txtBox);

     

    break;

     

     

    case 14:

     

    txtBox.CssClass =

    "TextBox";

     

     

    if ((fd.FieldLiteral.ToUpper().Trim().Contains("TIME") || fd.FieldLiteral.ToUpper().Trim().EndsWith("EFFORT") || fd.FieldLiteral.ToUpper().Trim().Contains("DURATION")) && (fd.FieldDataType.FieldDataTypeID != 16))

     

    {

    txtBox.Width = System.Web.UI.WebControls.

    Unit.Pixel(60);

     

    htControlcell.Controls.Add(txtBox);

     

    Label lblUnits = new Label();

     

    lblUnits.Text =

    "&nbsp;&nbsp;<I>[Hrs]</I>";

     

    htControlcell.Controls.Add(lblUnits);

    }

     

    else

     

    {

    txtBox.Width = System.Web.UI.WebControls.

    Unit.Pixel(140);

     

    htControlcell.Controls.Add(txtBox);

    }

     

    break;

     

     

    case 18:

     

     

    DropDownList ddlLOV = new DropDownList();

     

    ddlLOV.ID =

    "ddl" + fd.FieldLiteral;

     

    ddlLOV.CssClass =

    "DDL";

     

    ddlLOV.EnableViewState =

    true;

     

    ddlLOV.Width = System.Web.UI.WebControls.

    Unit.Pixel(185);

     

    ddlLOV.Items.Add(

    new ListItem("--Select--"));

     

     

    List<ListOfValues> lstValues = new RequirementBO().SelectListOfValues(fd.FieldID, 4, 36);

     

     

    for (int i = 0; i < lstValues.Count; i++)

     

    {

     

    ListItem li = new ListItem(lstValues[i].ListDescription, lstValues[i].ValueID.ToString());

     

    ddlLOV.Items.Add(li);

    }

    htControlcell.Controls.Add(ddlLOV);

     

    break;

     

     

    case 24:

     

     

    DropDownList ddsdlLOV = new DropDownList();

     

    ddsdlLOV.ID =

    "ddl" + fd.FieldLiteral;

     

    ddsdlLOV.CssClass =

    "DDL";

     

    ddsdlLOV.EnableViewState =

    true;

     

    ddsdlLOV.Width = System.Web.UI.WebControls.

    Unit.Pixel(185);

     

    ddsdlLOV.Items.Add(

    new ListItem("--Select--"));

     

     

    List<Module> lstModules = new RequirementBO().selectModules(4);

     

     

    for (int i = 0; i < lstModules.Count; i++)

     

    {

     

    ListItem li = new ListItem(lstModules[i].ModuleName, lstModules[i].ModuleCode.ToString());

     

    ddsdlLOV.Items.Add(li);

    }

    htControlcell.Controls.Add(ddsdlLOV);

     

    break;

     

     

    case 16:

     

     

    RadDatePicker rdPicker = new RadDatePicker();

     

    rdPicker.Skin =

    "Simple";

     

    rdPicker.Width = System.Web.UI.WebControls.

    Unit.Pixel(150);

     

    htControlcell.Controls.Add(rdPicker);

     

    if ((fd.FieldLiteral.ToUpper().Trim().Contains("TIME") || fd.FieldLiteral.ToUpper().Trim().EndsWith("EFFORT") || fd.FieldLiteral.ToUpper().Trim().Contains("DURATION")) && (fd.FieldDataType.FieldDataTypeID != 16))

     

    {

     

    Label lblUnits = new Label();

     

    lblUnits.Text =

    "&nbsp;&nbsp;<I>[Hrs]</I>";

     

    htControlcell.Controls.Add(lblUnits);

    }

     

    break;

     

     

    case 17:

     

     

    RadDateTimePicker rdtPicker = new RadDateTimePicker();

     

    rdtPicker.Skin =

    "Simple";

     

    rdtPicker.Width = System.Web.UI.WebControls.

    Unit.Pixel(150);

     

    htControlcell.Controls.Add(rdtPicker);

     

    if ((fd.FieldLiteral.ToUpper().Trim().Contains("TIME") || fd.FieldLiteral.ToUpper().Trim().EndsWith("EFFORT") || fd.FieldLiteral.ToUpper().Trim().Contains("DURATION")) && (fd.FieldDataType.FieldDataTypeID != 16))

     

    {

     

    Label lblUnits = new Label();

     

    lblUnits.Text =

    "&nbsp;&nbsp;<I>[Hrs]</I>";

     

    htControlcell.Controls.Add(lblUnits);

    }

     

    break;

     

     

    case 23:

     

     

    break;

     

     

    case 25:

     

     

    break;

     

     

    case 15:

     

    txtBox.CssClass =

    "TextBox";

     

     

    if ((fd.FieldLiteral.ToUpper().Trim().Contains("TIME") || fd.FieldLiteral.ToUpper().Trim().EndsWith("EFFORT") || fd.FieldLiteral.ToUpper().Trim().Contains("DURATION")) && (fd.FieldDataType.FieldDataTypeID != 16))

     

    {

    txtBox.Width = System.Web.UI.WebControls.

    Unit.Pixel(60);

     

    htControlcell.Controls.Add(txtBox);

     

    Label lblUnits = new Label();

     

    lblUnits.Text =

    "&nbsp;&nbsp;<I>[Hrs]</I>";

     

    htControlcell.Controls.Add(lblUnits);

    }

     

    else

     

    {

    txtBox.Width = System.Web.UI.WebControls.

    Unit.Pixel(160);

     

    htControlcell.Controls.Add(txtBox);

    }

     

    break;

     

    }

    htRow.Cells.Add(htControlcell);

    }



    Thanks in Advance
    Regards:
    Chaitanya.E

  2. Schlurk
    Schlurk avatar
    812 posts
    Member since:
    May 2009

    Posted 15 Mar 2010 Link to this post

    This is a bit simplified but the way to find a control within an ItemTemplate of a RadPanelBar would be the following:

    ASPX:
        <asp:Button ID="findButton" runat="server" Text="Find" OnClick="findButton_OnClick" /> 
        <br /> 
        <telerik:RadPanelBar ID="RadPanelBar1" runat="server"
            <Items> 
                <telerik:RadPanelItem Value="findItem"
                    <ItemTemplate> 
                        <asp:Label ID="findLabel" runat="server" Text="Not Found"></asp:Label> 
                    </ItemTemplate> 
                </telerik:RadPanelItem> 
            </Items> 
        </telerik:RadPanelBar> 

    C#:
        protected void findButton_OnClick(object sender, EventArgs e) 
        { 
            RadPanelItem foundItem = RadPanelBar1.FindItemByValue("findItem"); 
            Label foundLabel = foundItem.FindControl("findLabel"as Label; 
            foundLabel.Text = "Found"
        } 

    As you can see from this code - the foundLabel properly holds the instance of the "findLabel" control within a RadPanelItem's ItemTemplate.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top