Hi all bit of a two question in one here.

firstly im creating a site that is going to be multi language so all copy on the site needs to be changable. to do this ive given each tag on the page and id and runat=server for eg

<h1 id="siteH1" runat="server">Page h1 here</h1>

then i have a db table with a list of id's in 1 field and the copy in the other. so for get i have

ContentId PageContent
siteH1 Page h1 here

now to my questions firstly when i get my results how do i convert the string variable "siteH1" into and object so i can do
siteH1.InnerHtml = PageConent

and secondly ive tried numerous ways but how do i check to find out if the element id exists and if not carry on. no matter what i try i keep getting the error

"The variable 'blartest' does not exist in the current context

the reason i need to check is that i have numerous pages with different tags on each page.

thanks

Recommended Answers

All 8 Replies

hi thanks for that however its still throwing errors, ive copied my code below plus the error

string control = "blartest";
            Control test = FindControl(control);
            if (test != null)
            {
                Response.Write("found");
                Object objControl = (Object)control;
                objControl.InnerHtml = "jjjjjj";
            }
            else
            {
                Response.Write("not found");
            }

'object' does not contain a definition for 'InnerHtml' and no extension method 'InnerHtml' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

I think you might need to load them as HtmlContainerControl rather than converting them to Objects as the Object class doesnt contain an InnerHtml property.
I've not done a lot of ASP.NET so forgive me if thats wrong

your actually on the right path but i need to convert a string into a HtmlContainerControl pointer

I did a little digging because this was tugging at my memory; i had a similar problem on an old project where i couldn't find the control i was looking for. Turns out FindControl is a shallow search so if your control is nested it wont be found unless you use a recursive search:

private Control FindControlRecursive(Control Root, string Id)
        {
            //if current control is the one to be searched for then return it
            if (Root.ID == Id)

                return Root;

            //if not, call search on each sub control
            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);

                //Bubble the located control back up to original call if it is found
                if (FoundCtl != null)

                    return FoundCtl;

            }

            //if nothing is found and no sub controls remain then return null for this branch
            return null;

        }

You feed in the top level control to begin the search then for each subcontrol found the function calls itself with the subcontrol as the root.

You can use this method to find the control you are looking for

As for the HtmlContainerControl, what i meant when i said "load them" was to convert the control you find into a HtmlContainerControl:

string control = "blahtest";
    HtmlContainerControl ctrl = FindControlRecursive(this, control) as HtmlContainerControl;
    ctrl.InnerText = "value here";

Hope this helps :D

Hi thanks for that im getting the following error

FindControlRecursive' does not exist in the current context

any clues?

Its not a .net method it is a custom one. You need to copy and paste the FindControlRecursive method I posted above into your class before you can call it :)

Thanks, just tried it and it works a treat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.