Hi Dipakagarwal ,
It is very much possible to call the methods of a user control from the page on which you have placed the user control. You just need to declare the methods in the user control code as public. I am giving the code for an application below. That will show what I mean.
Here is the html code for a user control WebUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="Label1" runat="server" Visible="false" Style="z-index: 100; left: 130px; position: absolute;
top: 29px" Text="This is what you typed." Width="93px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Style="z-index: 102; left: 258px; position: absolute;
top: 26px" Width="489px"></asp:TextBox>
and here is the code behind for WebUserControl.ascx.cs
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void initproc(string val, bool pb)
{
Label1.Visible = pb;
TextBox1.Visible = pb;
TextBox1.Text = val;
}
}
As you can see there is a public procedure called initproc in this user control.
And here is the html code for default.aspx containing the user control WebUserControl with id WebUserControl1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!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>
<asp:Button ID="mybutton" Text="Submit" runat="server" style="z-index: 100; left: 411px; position: absolute; top: 184px" OnClick="mybutton_Click" />
<asp:TextBox ID="mytextbox" Text="Type something in here and you will see the user control" runat="server" style="z-index: 102; left: 359px; position: absolute; top: 133px"></asp:TextBox>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
Here is the code behind for default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void mybutton_Click(object sender, EventArgs e)
{
WebUserControl1.initproc(mytextbox.Text, IsPostBack);
}
}
Hope this helps.
N.dinesh kummar
Software Developer
Dipakagarwal, if this helps please login to Mark As Answer. | Alert Moderator