页面为webform3.aspx,自定义控件为webcontrol.ascx. 自定义控件里面有两个linkbutton,页面调用自定义控件后,怎么给这两个linkbutton循环赋值,代码贴下
webform3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="生成单个"
style="height: 26px" />
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</div>
<div>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="生成多个" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>WebForm3。axpx。csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
Control test = new Control(); for (int i = 0; i < 1; i++)
{
test = LoadControl("controls/WebControl.ascx");
PlaceHolder1.Controls.Add(test);
} //Random rd = new Random();
for (int i = 0; i < 10; i++)
{
LinkButton lb = this.FindControl("LinkButton" + (i + 1).ToString()) as LinkButton;
LinkButton1.Text =
}
} protected void Button2_Click(object sender, EventArgs e)
{
Control test = new Control(); for (int i = 0; i < 5; i++)
{
test = LoadControl("controls/WebControl.ascx");
PlaceHolder1.Controls.Add(test); }
}
}
}webcontrol。ascx<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebControl.ascx.cs" Inherits="WebApplication1.controls.WebControl" %>
<div>
<div>
<asp:LinkButton ID="LinkButton1" runat="server" style="TEXT-DECORATION: none"
ForeColor="Black" onclick="LinkButton1_Click"></asp:LinkButton>
</div>
<div>
<img src="/image/jiantou.png" />
</div> <div>
<asp:LinkButton ID="LinkButton2" runat="server" style="TEXT-DECORATION: none;"
ForeColor="Black"></asp:LinkButton>
</div>
</div>webcontrol。ascx。cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1.controls
{
public partial class WebControl : System.Web.UI.UserControl
{
private int _Value = 0; public int Value
{
get { return _Value; }
set { _Value = value; }
} protected void Page_Load(object sender, EventArgs e)
{
} protected void LinkButton1_Click(object sender, EventArgs e)
{
Random rd = new Random(); // 产生随机数
for (int i = 0; i < 10; i++)
{
LinkButton lb = this.FindControl("LinkButton" + (i + 1).ToString()) as LinkButton;
LinkButton1.Text= rd.Next(100).ToString(); // 对已存在的Label控件进行赋值,随机产生的0到100之间的数字
}
}
}
}
webform3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="生成单个"
style="height: 26px" />
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</div>
<div>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="生成多个" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>WebForm3。axpx。csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
Control test = new Control(); for (int i = 0; i < 1; i++)
{
test = LoadControl("controls/WebControl.ascx");
PlaceHolder1.Controls.Add(test);
} //Random rd = new Random();
for (int i = 0; i < 10; i++)
{
LinkButton lb = this.FindControl("LinkButton" + (i + 1).ToString()) as LinkButton;
LinkButton1.Text =
}
} protected void Button2_Click(object sender, EventArgs e)
{
Control test = new Control(); for (int i = 0; i < 5; i++)
{
test = LoadControl("controls/WebControl.ascx");
PlaceHolder1.Controls.Add(test); }
}
}
}webcontrol。ascx<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebControl.ascx.cs" Inherits="WebApplication1.controls.WebControl" %>
<div>
<div>
<asp:LinkButton ID="LinkButton1" runat="server" style="TEXT-DECORATION: none"
ForeColor="Black" onclick="LinkButton1_Click"></asp:LinkButton>
</div>
<div>
<img src="/image/jiantou.png" />
</div> <div>
<asp:LinkButton ID="LinkButton2" runat="server" style="TEXT-DECORATION: none;"
ForeColor="Black"></asp:LinkButton>
</div>
</div>webcontrol。ascx。cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1.controls
{
public partial class WebControl : System.Web.UI.UserControl
{
private int _Value = 0; public int Value
{
get { return _Value; }
set { _Value = value; }
} protected void Page_Load(object sender, EventArgs e)
{
} protected void LinkButton1_Click(object sender, EventArgs e)
{
Random rd = new Random(); // 产生随机数
for (int i = 0; i < 10; i++)
{
LinkButton lb = this.FindControl("LinkButton" + (i + 1).ToString()) as LinkButton;
LinkButton1.Text= rd.Next(100).ToString(); // 对已存在的Label控件进行赋值,随机产生的0到100之间的数字
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货