本文介绍了如何使用asp.net的preinit事件从webform(.aspx页面)加载usercontrol(.ascx页面)中按钮的按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
usercontrol.ascx.cs
--------------------------
usercontrol.ascx.cs
--------------------------
namespace UserControlDemo
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
private string LabelText = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
public string AllNames
{
get
{
string RetVal = string.Empty;
if (this.lblUserControl != null)
{
RetVal = this.lblUserControl.Text;
}
return RetVal;
}
set
{
lblUserControl.Text = value;
}
}
usercontrol.aspx
----------------------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="UserControlDemo.WebUserControl1" %>
<asp:Table runat="server">
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:Label ID="lblUserControl" runat="server" Text="Label" >
<asp:Button ID="Button1" runat="server" Text="X" ForeColor="Maroon"
Height="20px" onclick="Button1_Click"/>
<%-- <div id="ControlHolder" runat="server">--%>
webform1.cs
----------------------
namespace UserControlDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
WebUserControl1 wb = new WebUserControl1();
protected void Page_PreInit(object sender, EventArgs e)
{
wb.button_Click +=new EventHandler(wb_button_Click);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void chkList_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
WebUserControl1 user1 = null;
//user1 = (WebUserControl1)LoadControl(user1.GetType(), null);
user1 = (WebUserControl1)LoadControl("WebUserControl1.ascx");
user1.AllNames = chkList.Items[i].Text;
div1.Controls.Add(user1);
}
}
}
protected void button_Click(object sender, EventArgs e)
{
wb.button_Click +=new EventHandler(wb_button_Click);
}
protected void wb_button_Click(object sender, EventArgs e)
{
div1.Controls.Remove(wb);
}
}
}
webform.aspx
--------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" runat="server">
<div>
<asp:CheckBoxList ID="chkList" runat="server"
onselectedindexchanged="chkList_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Name1
<asp:ListItem>Name2
<asp:ListItem>Name3
<asp:ListItem>Name4
</div>
<form>
</body>
</html>
Here user control with a label and button is being added but on click of close button all controls are getting closed but i want only that control to be closed...can anyone please help??/
推荐答案
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication3.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
}
网页代码
Web Page code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ajax.aspx.cs" Inherits="WebApplication3.Ajax" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="divControlHolder" runat="server">
</div>
</div>
</form>
<script>
</script>
</body>
</html>
public partial class Ajax : System.Web.UI.Page
{
public string message;
protected void Page_Load(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
WebUserControl1 user1 = null;
//user1 = (WebUserControl1)LoadControl(user1.GetType(), null);
for (int i = 0; i < 10; i++)
{
user1 = (WebUserControl1)LoadControl("WebUserControl1.ascx");
divControlHolder.Controls.Add(user1);
}
}
}
这篇关于如何使用asp.net的preinit事件从webform(.aspx页面)加载usercontrol(.ascx页面)中按钮的按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!