Saturday, August 04, 2012

Adding a Control into a String Dynamically

You suppose you are wanted to add a string in a page that the string has to have a hyper link in it to link an especial page for any reason. 
So for doing this job you can do it in this way:


private string buildMessage(string message, int reportId) {
        StringBuilder sb = new StringBuilder();
        sb.Append(message);
        sb.Append("<br>");
 
        HyperLink hp = new HyperLink();
        hp.NavigateUrl = "~/index.aspx?reportId=" + reportId;
        hp.ID = "hpl";
        hp.Text = "Link Report";
        hp.Style.Add(HtmlTextWriterStyle.FontWeight, "Bold");
 
        using (StringWriter sw = new StringWriter(sb)) {
            using (HtmlTextWriter tw = new HtmlTextWriter(sw)) {
                hp.RenderControl(tw);
            }
        }
       sb.Append("Some other text");
 return sb.ToString();     }



Now you can you this function in different way. For example you 
can add a Panel Control to your page. Then call this function to 
set the Panel InnerHtml property.

Good Luck.
Post a Comment