How to call Usercontrol in Asp.Net or Js file

Ascx Page(User control page)—–

<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”WebUserControl1.ascx.cs” Inherits=”WebApplication2.UControls.WebUserControl1″ %>
<asp:TextBox ID=”txt1″ runat=”server”></asp:TextBox>

Asp.net Page—

<%@ Page Language=”C#” MasterPageFile=”~/Site.master” CodeBehind=”Default.aspx.cs” Inherits=”WebApplication2._Default” %>
<%@ Register  Src=”~/UControls/UserControlMultipleInstance.ascx”  TagName=”UserControlMultipleInstance” TagPrefix=”uc1″%>
<%@ Register Src=”~/UControls/WebUserControl1.ascx” TagName=”uio” TagPrefix=”kl” %>

<asp:Content ID=”headContent” ContentPlaceHolderID=”head” Runat=”Server”>

</asp:Content>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

<script type=”text/javascript” src=”~/Scripts/JavaScript1.js”></script>
<asp:ScriptManagerProxy ID=”ScriptManagerProxy1″ runat=”server” />

<h2>My tables</h2>
<div id=”div1″>
<kl:uio ID=”UserControlMultipleInstance1″  runat=”server” />
</div>
<br />
<div id=”div2″>
<kl:uio ID=”UserControlMultipleInstance2″  runat=”server” />
</div>
<br />
<div id=”div3″>
<kl:uio ID=”UserControlMultipleInstance3″  runat=”server” />
</div>

<br /><br />
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” ClientIDMode=”Static”
Text=”Call UserControl Instance1″ />
&nbsp;&nbsp;
<asp:Button ID=”Button2″ runat=”server” onclick=”Button1_Click” onClientClick=”abc();”
Text=”Call UserControl Instance2″ />
&nbsp;&nbsp;
<asp:Button ID=”Button3″ runat=”server” onclick=”Button1_Click” onClientClick=”abc();”
Text=”Call UserControl Instance2″ />

</asp:Content>

—-In Js file

var ContainerId;

function SetContainerID(ParentContainerID) {
ContainerId = ParentContainerID;
}

function abc(ParentContainerID) {
//var ParentContainerID=’div1′;
SetContainerID(ParentContainerID);
alert(“TextBox Contentx: ” + $(‘#’+ContainerId+’ [id$=txt1]’).val());
}

 

—If codebehind use–

protected void Button1_Click(object sender, EventArgs e)
{
//Control ctrl = “”;
TextBox ctrl1 = UserControlMultipleInstance1.FindControl(“txt1”) as TextBox;

string de=ctrl1.Text;
}

  1 comment for “How to call Usercontrol in Asp.Net or Js file

  1. Kumar
    September 10, 2014 at 1:04 pm

    at first create usercontrol(.ascx) file and then register in aspx or web.config file.
    after that whatever register control id ,use this id in codebehind or js file.for more detail explain above with example.

Leave a Reply