Thursday, February 8, 2007

Code to render a control to HTML

This code shows you how to render a control datagrid,repeater and other controls to a html.


private void ClearControls(System.Web.UI.Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}

if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}





private void gethtml(Control dgView,string type,int numofrows)
{
///////create pager
///
int numofpages = 25;

int noofindex = 0;

if(numofrows > numofpages)
{

int div = numofrows / numofpages;
int mod = numofrows % numofpages;

if(mod != 0)
{
div = div + 1;
}
noofindex = div;

}

int g;
string str8="";
string str=" ";
for(g=0;g{
str8 = SF.ToSafeString(g);
if(Request.QueryString["pindex"]!= null)
{
if(str8 == Request.QueryString["pindex"])
{
str8 = "" + str8 + "";
}

}
str += @"" + str8 + " ";
}

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(dgView);
dgView.RenderControl(oHtmlTextWriter);

string str1 = oStringWriter.ToString();
string pattern = @"pattern";
foreach (Match m in Regex.Matches(str1, pattern))
{
str1 = str1.Replace(m.ToString(),"");
}
str1 = str1.Replace("###",str);
Response.Write(str1);
Response.Write(str);
Response.End();


}

gethtml(rptSIP ,"1",0);

No comments: