如何使用ASP.Net和C#动态添加HTML div元素 [英] How to dynamically add HTML div element using ASP.Net and C#

查看:507
本文介绍了如何使用ASP.Net和C#动态添加HTML div元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是yogesh,我想知道如何在ASP页面中动态添加div。

请建议动态更改其高度和宽度。

请举个例子。

Hi I am yogesh and I want to know how to dynamically add the div in ASP page.
Please also suggest to change its height and width dynamically.
Please give an example.

推荐答案

如果你想在Javascript中做,那就用 createElement()



查看示例



If you want it to do in Javascript then use createElement()

have a look on the example

function addDiv(innertext) 
{
      var r  = document.createElement('Div');
      r.style.height="20px";
      r.style.width="25px";
      r.appendChild(innertext);
}





这将在页面上添加div。如果你想在特定的控件中添加它,那么使用可以使用





this will add a div on the page. If you want to add this in a specific control then use can use

document.getElementById('specific control name').createElement('Div');


System.Web.UI.HtmlControls.HtmlGenericControl newdiv = System.Web.UI.HtmlControls.HtmlGenericControl

newdiv.style.height="200px";
newdiv.style.width="250px";


protected void Page_PreInit(object sender, EventArgs e)
    {
        GenerateControls();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void GenerateControls()
    {
        if (Session["divCount"] != null)
        {
            int divID = (int)Session["divCount"];

            for (int i = 0; i < divID; i++)
            {
                HtmlGenericControl divYogesh = new HtmlGenericControl("div");
                divYogesh.Attributes.Add("class", "myClass");
                divYogesh.Attributes.Add("id", (i + 1).ToString());

                pnlYogesh.Controls.Add(divYogesh);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["divCount"] == null)
            Session["divCount"] = 1;
        else
        {
            int divID = (int)Session["divCount"];
            divID++;
            Session["divCount"] = divID;
        }

        HtmlGenericControl divYogesh = new HtmlGenericControl("div");
        divYogesh.Attributes.Add("class", "myClass");
        divYogesh.Attributes.Add("id", Session["divCount"].ToString());

        pnlYogesh.Controls.Add(divYogesh);
    }





希望它能帮助你:)



hope it heps you :)


这篇关于如何使用ASP.Net和C#动态添加HTML div元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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