samy
samy

Reputation: 1969

ASP.NET:finding html controls in server side afther adding them programmitcly

im trying to do somthing and got into a problem.

i got a function that adding html elements and thire attributes. now, i want to get the controls in server side (code behind) so i can do some stuff with them.

my problem is: i cant "find" them.

this is part of the function im using to add them, its a bit longer so i show only the controls i want to get in the server side:

 public string EditPhoto(int x)
{
    using (StringWriter stringWriter = new StringWriter())
    {
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            // Some strings for the attributes.
           string classValue = "thumb";

            //Begin #5 <div class=image-title">
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "image-title");
            writer.AddAttribute("runat", "server"); //--> server side att
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "title" + x);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            TextWriter innerTextWriter = writer.InnerWriter;
            innerTextWriter.Write(title);
            writer.RenderEndTag(); //#End 5 </div>


            //Begin #6 <div class="image-desc">
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "image-desc");
            writer.AddAttribute("runat", "server"); //--> server side att
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "desc" + x);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            innerTextWriter = writer.InnerWriter;
            innerTextWriter.Write(descreption);
            writer.RenderEndTag(); //#End 6 </div>

            writer.RenderEndTag();//#End 4 </div>
            writer.RenderEndTag(); // End #1 </li>

        }
        // Return the result.
        return stringWriter.ToString();
    }

afther the function is done i got this Test code to try and look for them:

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

    if (FindControl("title" + i) != null)
        Response.Write("Found 1 title control");
    else
        Response.Write( i +"There is no control");
    }

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

            if (FindControl("desc" + i) != null)
                Response.Write("Found 1 descreption control");
            else
                Response.Write(i + "Thre is no control");
        }

sorry for my english

Upvotes: 0

Views: 5296

Answers (4)

andrea404
andrea404

Reputation: 149

You can also use the PlaceHolder control for to add and find all controls that you need.

AddControl:

RadioButtonList wRadioButtonList = new RadioButtonList { ID= "myID" };
wRadioButtonList.Items.Add( new ListItem( "Yes", "yes" ) );
wRadioButtonList.Items.Add( new ListItem( "No", "no" ) );

m_plh_PlaceHolde.Controls.Add( wRadioButtonList );

FindControl:

 RadioButtonList wRbl = m_plh_PlaceHolde.FindControl("myID" ) as RadioButtonList;

Upvotes: 0

KodeKreachor
KodeKreachor

Reputation: 8882

Check it dude....

You'd have some sort of container in your aspx, like this:

    <asp:Panel ID="controlPanel" runat="server"></asp:Panel>

Then in your code behind you could have something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        InsertControls();
    }

    private void InsertControls()
    {
        TextBox textBox = new TextBox();
        textBox.ID = "textBox1";
        textBox.Text = "Cool Beans";

        controlPanel.Controls.Add(textBox);

        TextBox locatedTextBox = TraverseControlTree(controlPanel, "textBox1") as TextBox;
    }

    public static Control TraverseControlTree(Control root, string Id)
    {
        if (root.ID == Id) { return root; }

        foreach (Control Ctl in root.Controls)
        {
            Control control = TraverseControlTree(Ctl, Id);
            if (control != null) { return control; }
        }

        return null;
    }

Upvotes: 2

e wagness
e wagness

Reputation: 301

You can absolutely add controls literally, but you must add them to something like a panel that is already on the page. if you try to add a control at runtime straight to the page, you will get an error.

as for finding your controls, you might have to search recursively. controls are often nested, and i believe FindControl does not search recursively, only in the current naming container.

I solved this problem by writing code like this:

private void AddControls(ControlCollection page, ArrayList controlList)
{
    foreach (Control c in page)
    {
        if (c is WebChartControl)
        {
            WebChartControl chart = c as WebChartControl;
            controlList.Add(chart);
        }
        if (c.HasControls())
        {
            AddControls(c.Controls, controlList);
        }
    }
}

I was searching for all webchart controls on a page and adding them to an array to be used later, but you could just as easily search by ID, and when you find it just return; a note, when searching by ID, you might not be able to do "control.ID == "some string"" you might have to cast it as the desired datatype before you test for an ID match

Upvotes: 1

shriek
shriek

Reputation: 5197

This blog series might be of help. Understanding Dynamic Controls

Upvotes: 1

Related Questions