0

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

5
  • You cannot add server controls literally. They won't be part of the page's control-collection. Commented Mar 22, 2012 at 21:50
  • Are you locked into adding your controls using the HtmlTextWriter? There are certainly much easier and extensible means of accomplishing that which would make finding them in the control tree much easier as well. Commented Mar 22, 2012 at 21:53
  • oh, didnt know that. what are suggesting to do? Commented Mar 22, 2012 at 21:53
  • @KodeKreachor can you give me some examples for better way to add and then find controls? i would love to learn new thing! :) Commented Mar 22, 2012 at 22:05
  • For sure, check out my answer below Commented Mar 22, 2012 at 22:17

4 Answers 4

2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

This blog series might be of help. Understanding Dynamic Controls

Comments

1

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

3 Comments

hope that helps samy, so instead of if(c is WebChartControl) you should put if(((HtmlTextWriter)c).ID == "Title" + x)
i tried changing the WebChartControl to HtmlTextWriter but i cant get the id attribute, only the to functions of the HtmlTextWriter class. i dont know what class represnt a control that is add with HtmlTextWriter.
ah, HtmlTextWriter is not a control, it is a way you can create html elements without explicitly creating html controls. you can create an html input control, which you can give an ID and add to a panel, like what @KodKreachor showed you. Im not really sure you are using the HtmlTextWriter, it would be easier to do this with aspx text boxes and such. if you really must use pure html, create a HtmlInputControl and add that to a panel already on your page
0

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;

Comments

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.