Thanks, Sanjay
<form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div> </form> --------------- codebehind using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class WebUserControl : System.Web.UI.UserControl { static int i = 0; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { i++; Label1.Text = "Button is clicked" + i.ToString(); if(!Page.IsPostBack ) i++; Label1.Text = "Button is clicked" + i.ToString(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class webusercontroltest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { // showValue(); } } protected void Button1_Click(object sender, EventArgs e) { showValue(); } protected void showValue() { Control ctl = this.LoadControl("~/WebUserControl.ascx"); // Must set ctl ID ctl.ID = "viewctl"; this.form1.Controls.Add(ctl); Label mylbl = (Label)ctl.FindControl("Label1"); Response.Write(mylbl.Text); } }
Login to post response