从code中的.aspx追加新的标记背后 [英] append new markup in .aspx from code behind

查看:79
本文介绍了从code中的.aspx追加新的标记背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一些额外的文本在现有的.aspx页面中运行。但没有找到任何合适的方式来做到这一点。

I am trying to add some extra texts in an existing .aspx page in runtime. But not finding any suitable way to do it.

有没有办法在运行时新的标记追加到从code .aspx页后面?

Is there any way to append new markups to an .aspx page from code behind in runtime?

例如:

我有这个在.aspx页面 -

I have this in an .aspx page-

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

和我想从code后面追加本 -

And i want to append this from code behind-

    <div>
    hghjggjg
    </div>
    </form>
</body>
</html>

感谢您。

推荐答案

动态...
..aspx

dynamically... ..aspx

<%@ Control Language="C#" AutoEventWireup="true"     CodeBehind="xx.ascx.cs"   Inherits="xx.layouts.xxSublayout" %>
<%@ Register Assembly="System.Web" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:LiteralControl runat="server" ID="openingTag" />
<asp:PlaceHolder runat="server" id="contentPanel" />
<asp:LiteralControl runat="server" ID="closingTag" />

code behind..add与文本的div来比的Page_Load ...

code behind..add a div with text to some method other than PAGE_LOAD...

 var inputWrapper = new HtmlGenericControl("div"){ID = "d1"};
        inputWrapper.Attributes.Add("class", "yourcssclass");
        inputWrapper.InnerText = "hghjggjg";
        contentPanel.Controls.Add(inputWrapper);

要坚持标签回发...

to persist tag postback...

            //add div ID to list, add list to session so it can be recreated on the page_INIT, this must be done or btn will NOT exist when "Get vCard" is clicked.
            PostbackIDs.Add(inputWrapper.ID);
            Session["pb"] = PostbackIDs;

在INIT ...

//Must recreate controls from session, so label is existing
       if (Session["pb"] != null)
        {
            var pblist = (List<string>)Session["pb"];
            foreach (var id in pblist)
            {                   
                // label
                var inputWrapper= new HtmlGenericControl("div") { ID = id };
                inputWrapper.Attributes.Add("class", "yourcssclass");                    
                contentPanel.Controls.Add(inputWrapper);
            }
        }

由于init方法页面加载之前命中,控件的ID将到位文本添加到它。

Since the INIT method is hit before page load, the ID of the control will be in place to add text to it..

这篇关于从code中的.aspx追加新的标记背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆