Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
I'm trying to created a simple solution to a problem that requires GridViews to be added dynamically.

I put known code for the grid into a StringBuilder object and passed its value to the InnerHtml of a div. I'm not using a Panel because the I don't want to spend more time testing a pragmatically created grid.

The problem is that I'm not able to access the control from the code behind. I have been trying to do this via findcontrol and the recursive findcontrol but to no avail.

Help.

**
Hi Nelek. I hope this helps.

I have left out grid definition.
XML
StringBuilder sbGridHtml = new StringBuilder();
sbGridHtml.Append("<table>");

for (int i = 0; i < Count; i++)
{
    sbGridHtml.Append("<tr>");
    sbGridHtml.Append("<td>");
    sbGridHtml.Append("<div>");
    sbGridHtml.Append("<asp:GridView ID=\"gvSubject"+ i.ToString() +"\" runat=\"server\" />");
    sbGridHtml.Append("</div>");
    sbGridHtml.Append("</td>");
    sbGridHtml.Append("</tr>");
    #endregion Grid HTML

}
sbGridHtml.Append("</table>");
DivGrid.InnerHtml = sbGridHtml.ToString();

I later try to access the grid using the below code. 
for (int i = 0; i < Count; i++)
{
GridView gv = (GridView)Page.FindControlRecursively("gv" + i.ToString());
.......
}


**
Hi.

@SA I'm sorry, I'm not sure I understand what you are saying. I have however switched custom controls to asp controls in the code.
@GregWyatt: Yes to both. The will be arranged one per row of the table and they will have actionable buttons.
Posted
Updated 14-Oct-12 18:18pm
v6
Comments
Nelek 12-Oct-12 6:50am    
Could you use "improve question" and add a bit of your relevant/related code?
Sergey Alexandrovich Kryukov 12-Oct-12 14:15pm    
It must be ASP.NET, right? Do you know that ASP.NET is not actually ASP? Please tag it properly, to avoid confusions.
--SA
GregWyatt 12-Oct-12 16:41pm    
Will the gridviews be one after the other on the page? For example, if you had to add three gridviews, do you want them to show up one beneath the other one? Also, are there actionable buttons/events that you want fired from the dynamically created gridviews or are the gridviews for presentation purposes only?

1 solution

I does not work that way. .aspx page is not a file with string contents. It is later compiled to complex class witch later spit it's contents with Response.write.
If you have to add dynamic controls you have to add it as objects to some oth

er control via ControlColection property.
If you want do this with div, add to this div control collection.

If you don't want do this with creation of objects
new AspxGridView(); 

Do this in markup:
ASP.NET
 <%if(CreateGrids) %> 
 <asp:gridview runat="server" xmlns:asp="#unknown">
</asp:gridview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900