开发者

Accees to dynamically added HTML tages to ASP.NET page

I add dynamically html code in page load to section of page. like this:

 protected void Page_Load(object sender, EventArgs e)
{
    //HTML file
    string file = Server.MapPath("~/ProductsF/DetailFiles/"+Request.QueryString["Page"]);        
    FileInfo fi = new FileInfo(file);
    HtmlGenericControl hgc = new HtmlGenericControl();
    if (File.Exists(file))
    {
        //load html file
        StreamReader sr = File.OpenText(file);
        hgc.InnerHtml = sr.ReadToEnd();            
        sr.Close();
        sr.Dispose();
    }
    this.ContentPanel.Controls.Add(hgc);
}

but i want to access some html control in prerender event and change some attribute. but i can't to access those html controls.

protected override void OnPreRender(EventArgs e)
{
    MakeAllUrlFullyQualified(Page);
    base.OnPreRender(e);
}


private void MakeAllUrlFullyQualified(Control contr开发者_如何转开发ol)
{
    if (control.HasControls())
    {
        foreach (Control child in control.Controls)
            MakeAllUrlFullyQualified(child);
    }

    if (control is HtmlAnchor)
    {
        HtmlAnchor anchor = control as HtmlAnchor;
        anchor.HRef = WebHelper.GetFullyUri(ResolveUrl(anchor.HRef));
    }
    else if (control is HtmlImage)
    {
        HtmlImage image = control as HtmlImage;
        image.Src = WebHelper.GetFullyUri(ResolveUrl(image.Src));
    }
    else if (control is HtmlLink)
    {
        HtmlLink link = control as HtmlLink;
        link.Href = WebHelper.GetFullyUri(ResolveUrl(link.Href));
    }
}

what is wrong in this code?


You are adding HTML to your page through the HtmlGenericControl.InnerHTML property. You are not adding controls to it.

The only control you are adding is the HtmlGenericControl (hgc), but you are not adding any others.

Look through the documentation on User Controls and Master Pages to see how to properly template a website in ASP.NET

Edit:

To access the HTML you are adding, you can do the following:

if (control is HtmlGenericControl)
{
    HtmlGenericControl myGenericControl = control as HtmlGenericControl;
    string innerHtml = myGenericControl.InnerHtml;
}

What you can't do is convert the HTML to controls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜