-
C# code
-
HtmlGenericControl span = new HtmlGenericControl(); span.ID = "divname"; span.Attributes.Add("class", "className"); Page.Controls.Add(span); TextBox tb = new TextBox(); tb.CssClass = "className"; form1.Controls.Add(tb);
------其他回答(10分)---------
添加runat="server"
HtmlGenericControl div;
HtmlGenericControl span;
div = new HtmlGenericControl();
div.TagName = "div";
div.ID = "divTextBox" + i.ToString();
div.Attributes["class"] = "item2";
------其他回答(10分)---------
<input type="button" id="button1" value="测试" runat="server" /> //转为
服务器控件 便于操作
后台设置样式:
C# code
button1.Attributes.CssStyle.Value="border:1px solid red";
------其他回答(10分)---------
str = "parent.document.getElementById('div2').style.display = 'block';"; //写控制的样式
ClientScript.RegisterStartupScript(ClientScript.GetType(),"myscript",str);//由他实现
你在后台的按钮什么的来引起就行了。
------回答---------
------其他回答(10分)---------
-
C# code
-
HtmlGenericControl span = new HtmlGenericControl(); span.ID = "divname"; span.Attributes.Add("class", "className"); Page.Controls.Add(span); TextBox tb = new TextBox(); tb.CssClass = "className"; form1.Controls.Add(tb);
------其他回答(10分)---------
添加runat="server"
HtmlGenericControl div;
HtmlGenericControl span;
div = new HtmlGenericControl();
div.TagName = "div";
div.ID = "divTextBox" + i.ToString();
div.Attributes["class"] = "item2";
------其他回答(10分)---------
<input type="button" id="button1" value="测试" runat="server" /> //转为 服务器控件 便于操作
后台设置样式:
C# code
button1.Attributes.CssStyle.Value="border:1px solid red";
------其他回答(10分)---------
str = "parent.document.getElementById('div2').style.display = 'block';"; //写控制的样式
ClientScript.RegisterStartupScript(ClientScript.GetType(),"myscript",str);//由他实现
你在后台的按钮什么的来引起就行了。
////创建服务器端控件.
////指定的标记"LINK"初始化此类的新实例.
HtmlGenericControl objLink = new HtmlGenericControl("LINK");
objLink.ID = ID;
objLink.Attributes["rel"] = "stylesheet";
objLink.Attributes["type"] = "text/css";
objLink.Attributes["href"] = "css/indexcss.css";
////此控件不产生任何可见输出,仅作为其他控件的容器,可在其中添加,插入或移除控件.
Page.Controls.Add(objLink);
C#动态生成按钮及定义按钮事件的方法
1、后台生成input的button按钮
复制代码 代码如下:
HtmlGenericControl control = new HtmlGenericControl("input");
control.Attributes.Add("type", "button");
control.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(this, "btnMyQuery")
control.Attributes.Add("type", "button");
control.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(this, "btnMyQuery")
写入到界面中
2、后台回发拦截与处理
|
1
2
3
4
5
6
7
8
9
10
|
if
(Page.IsPostBack)
{
//获得回发参数
string
eventArgument = Page.Request.Form[
"__EVENTARGUMENT"
];
if
(!String.IsNullOrEmpty(eventArgument)
&& eventArgument.Equals(
"btnMyQuery"
))
{
//执行您的回发处理代码
}
}
|
C#动态添加控件后的单击事件
Label lbl = new Label();
lbl.Click += new EventHandler(lbl_click); //用代码动态连接事件
.....
再实现lbl_click的定义:
private void lbl_click(object sender,EventArgs e){
Label lbl = (Label)(sender); //这么一转换就知道是哪个label点击了
if (lbl.Name=="lbl001")
//是001 label
}
C#动态控件创建与事件处理
本文介绍如何使用C#动态创建HTML控件,包括按钮和标签,并定义其样式与点击事件处理方法。通过示例代码展示了如何将控件添加到页面,设置属性,以及如何在服务器端捕获并响应这些控件的事件。

被折叠的 条评论
为什么被折叠?



