5

Hye all.

i have div in index.aspx page like this

<div id="MainDiv" runat="server">

and i am creating div from server side(At run time) like

 for (Int32 i = 0; i < 4; i++)
    {

        //Create here divFinal
        HtmlGenericControl divFinal = new HtmlGenericControl("div");
        divFinal.ID = i.ToString();

        divFinal.Attributes.Add("class", "column");


        mainDiv.Controls.Add(divFinal);
        //add to maindiv


        HtmlGenericControl div = new HtmlGenericControl("div");
        div.ID = "t_e_" + i.ToString() + "_a";
        div.Style["background-color"] = "#CFD8E6";
        div.Attributes.Add("class", "grid");
        div.Attributes.Add("onclick", "OnMouseDownDiv(this)");

        div.Attributes.Add("onmouseover", "OnMouseDown(this)");
        div.Attributes.Add("onmouseout", "OnMouseUp(this)");
        divFinal.Controls.Add(div);

        // add to dvfinal


    }

After gen rate its look like this in HTML form

 <div id="mainDiv"><div id="0" class="column"><div id="t_e_0_a"></div></div><div id="1" class="column"><div id="t_e_1_a"></div></div></div>

Now I need to find div id t_e_0_a inside main Divdiv.

HtmlGenericControl div = 
    ((HtmlGenericControl)showdiv.FindControl("0")); 

But its give me error....

Ankur Monga
  • 53
  • 1
  • 1
  • 8

2 Answers2

3

You can't do it like you want as it is not a control. You should place runat="server" attribute on it, or you can get it somehow from showdiv.InnerHtml - there it should be presented as a string which you can parse with some HTML parser for .net (for instance, HTMLAgilityPack suggested here)

To create server side controls at run time, you can use something like below:

for (Int32 i = 0; i < 2; i++)
{
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.ID = i.ToString();
        div.InnerHtml = i.ToString();
        div.ClientIDMode = ClientIDMode.Static; //this is for .NET 4.5 only. Required to have ClientID the same as ID.
        showdiv.Controls.Add(div);
}

and than, after controls are added, you should be able to use something like this:

HtmlGenericControl div=((HtmlGenericControl)showdiv.FindControl("1"))

to get those controls. But please remember that controls added like that, must be added for each request.

Community
  • 1
  • 1
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • Thanks FAngel,let me implement it . – Ankur Monga Dec 24 '12 at 08:51
  • i am geting error Cannot get inner content of 0 because the contents are not litera – Ankur Monga Dec 25 '12 at 11:15
  • And... Let me guess (you know, I'm telepathist and can read peoples mind), that happens on client side. When you are trying to get an element like `document.getElementById('1').innerText`, right? Or I'm bad sensitive? If I'm good telepathist, than you should remember that runat="server" controls will have different IDs on client side from those you have on server. See [ClientID](http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx) property – Viktor S. Dec 25 '12 at 11:24
  • If my guess is correct, and you are working with .net 4.5, you can use [ClientIDMode](http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx) property to get ClientID the same as ID. Let me update an answer – Viktor S. Dec 25 '12 at 11:26
  • I m using 4.0..and my problem is that i have four div which are create at run time with id a,b,c,d.Suppose user click on c div then i can get div id which is now c at server side.Then After adding the controls again with showdiv(which is parent div), i want to append div which id is c. – Ankur Monga Dec 25 '12 at 11:37
  • and i am doing like. HtmlGenericControl div=((HtmlGenericControl)showdiv.FindControl("c")); and its giive me error..SO CAN U TELL ME HOW TO APPEND DIV AT RUN TIME WITH THERE SPECIFIC ID.PLEASE help – Ankur Monga Dec 25 '12 at 11:38
  • Its server side error while i m finding div.Cannot get inner content of c because the contents are not literal. – Ankur Monga Dec 25 '12 at 11:49
  • When does it happens? First you make a div, next client clicks something in browser and you are trying to find it during postback request, right? – Viktor S. Dec 25 '12 at 11:54
  • Please have look at question again. – Ankur Monga Dec 25 '12 at 12:08
  • You should generate items dynamically in Page_Load or Page_Init functions. Is your for loop located there? And it should happen after any request. Also where `HtmlGenericControl div = ((HtmlGenericControl)showdiv.FindControl("0"));` is located? In what function? Also - do you have `FindControl("0")` or `FindControl("t_e_0_a")` as it should be? – Viktor S. Dec 25 '12 at 12:25
0

you should have tried with recursive function that how Page.FindControl works.

private Control getFollowingControl(Control c, string key,out Control returnControl)
{        
    if(c.hasChild)
    {
        foreach(Control item in c.controls)
        {
            getFollowingControl(item,key,out returnControl);
        }
    }
    else
    {
        if(c.Id==key)
        {
            returnControl=c;
            break;
        }

    }
}

Now you can use above recusive function to find any control at any depth....

Control getThisControl=null;
getFllowingControl(this,"myButton1",out getThisControl);

At the end it would give you the control that has Id="myButton1" in GetThisControl control object.

Hiren Desai
  • 941
  • 1
  • 9
  • 33