Unsuccessful use of .FindControl () on div

I have an html div element containing several divs with values ​​that I want to add to the array server!

my html divs looks something like this:

<div id="clientGrid" runat="server"> <div id="cell_0_0" runat="server" class="box" onclick="clicked(this)">2</div> <div id="cell_0_1" runat="server" class="box" onclick="clicked(this)">1</div> <div id="cell_0_2" runat="server" class="box" onclick="clicked(this)">3</div> <div id="cell_0_3" runat="server" class="box" onclick="clicked(this)">4</div> <div id="cell_1_0" runat="server" class="box" onclick="clicked(this)">3</div> <div id="cell_1_1" runat="server" class="box" onclick="clicked(this)">2</div> <div id="cell_1_2" runat="server" class="box" onclick="clicked(this)">4</div> <div id="cell_1_3" runat="server" class="box" onclick="clicked(this)">1</div> <div id="cell_2_0" runat="server" class="box" onclick="clicked(this)">4</div> <div id="cell_2_1" runat="server" class="box" onclick="clicked(this)">3</div> <div id="cell_2_2" runat="server" class="box" onclick="clicked(this)">2</div> <div id="cell_2_3" runat="server" class="box" onclick="clicked(this)">1</div> </div> 

I want the values ​​of these divs to go to the array server. My current code is as follows:

 for (int i = 0; i < _grid.getGridSize(); i++) { for (int j = 0; j < _grid.getGridSize(); j++) { string divId = string.Format("cell_{0}_{1}", i.ToString(), j.ToString()); Control div = clientGrid.findControl(divId); //more code underneath } } 

so the div id is generated in string format, no problem ...

but clientGrid.findControl always = null ! even if I put sting directly in a constructor like clientGrid.findControl("cell_0_0"); , it still returns NULL! How is this possible?

As far as I understand, the findControl method finds server controls with a string identifier?

I cannot access child divs directly, like cell_0_0.clientID , because child divs are generated by the server at runtime.

when the page loads, this function starts ...

  for (int i = 0; i < _grid.getGridSize(); i++) { for (int j = 0; j < _grid.getGridSize(); j++) { returnElement += " <div "; returnElement += "id=\"cell_" + i + "_" + j + "\" "; returnElement += "runat=\"server\" "; returnElement += "tabindex=\"-1\" "; returnElement += "class=\"box\" "; returnElement += "onclick=\"clicked(this);\" "; returnElement += "onkeypress=\"changeValue(event, this);\" "; returnElement += "style=\"top:" + top + "%; left:" + left + "%;\">"; returnElement += test[i, j]; returnElement += "</div>\n "; left = left + 20; } left = 0; top = top + 20; } return returnElement; 

test [,] is the 2nd array containing integer values. these divs are then placed in clientGrid by clientGrid.innerHtml = allMyDivs

Then users change the values ​​of these sections to whatever they want, and then, when the user clicks the button, the values ​​of these divs must somehow go back to the serveride code so that I can process them.

ClientGrid.control.count = 0 ... ??? so it’s obvious ... for asp, these div elements do not exist ... for asp.net ... there is NOTHING inside the clientGrid div ... but ... the check element on my Chrome browser says othervise! What's happening?

+6
source share
2 answers

Try using the asp: Panel control, which is an HTML div element.

This will allow you to optimize the code:

Client side:

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

Server side:

 for each row for each col Panel p = new Panel(); p.ID = "cell_" + row.tostring() + "_" col.tostring(); p.Text = "&nbsp;"; p.OnClientClick = "javascript:blah"; clientGrid.Controls.Add(p); 

You can even simply use asp: Placeholder and programmatically add all the controls to it.

Then, in postback, you need to redisplay the page in the init event. All controls that were dynamically created must be re-created and re-added to the page for each postback to restore the state of the page.

Then you can:

 clientGrid.FindControl("Cell_0_0") 

after re-displaying all the controls.

There's a ton of dynamically displayed messages asp.net runs all over the internet.

Another option is to place a hidden control for each cell in the form and, using javascript, put the value from each cell into the corresponding hidden control for sending and use Request.Form ("Cell_0_0").

+4
source

Have you ever done _grid.AddControl(new cell) or clientGrid.AddControl(new cell) ?

You are looping for _grid, but doing a search on clientGrid. Maybe it should be _grid.FindControl() ?

Edit (1):

Then you should do in your PreRender() foreach(Control c in clientGrid) Response.Write(c.ID + "<br />");

Get a list of all identifiers that clientGrid knows about. This should help you find a solution.

Edit (2):

You can also try this ControlHelper method to get the control, as the control may be launched deeper than you expect.

 public static Control FindControlRecursive(this Control Root, string Id) { if (Root.ID == Id) return Root; foreach (Control Ctl in Root.Controls) { Control FoundCtl = FindControlRecursive(Ctl, Id); if (FoundCtl != null) return FoundCtl; } return null; } 

Then use it instead of FindControl.

Edit (3):

I just inserted your markup into the form, added an empty css class and an empty javascript (object) {;} handler, and then put this line in my page_Load ()

Management div = clientGrid.FindControl ("cell_0_0");

It's not a problem. So, something strange happens to you, which is not obvious from your publication in the code.

+2
source

Source: https://habr.com/ru/post/888866/


All Articles