Find Controls of Usercontrols in aspx.cs file in ASP.Net

In this example we show you how we can find the any type of controls like Literal, Lable, datalist, datagrids. When usercontrols used in master pages and for specific page, you need to change the content of control under certain conditions. For these type of cases we can find the control in aspx.cs/vb file from usercontrols.

Usercontrol Files :-
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="left.ascx.cs" Inherits="Usercontrols_left" %>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

Usercontrol called in Master page :-
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="member.master.cs" Inherits="Masterpages_member" %>

<%@ Register Src="~/Usercontrols/left.ascx" TagName="Left" TagPrefix="UC" %>
<%@ Register Src="~/Usercontrols/LoginSearch.ascx" TagName="Login" TagPrefix="UC" %>
<%@ Register Src="~/Usercontrols/Header.ascx" TagName="Header" TagPrefix="UC" %>

<UC:Left ID="ucLeft" runat="server" />
<form id="form1" runat="server"><asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        master page content

info.aspx.cs file code :-

To get the user control and assign the value. I find the litral control and assign the textual content to that control. You can use the same way to find any other control.

 UserControl usercontrols_login = Master.FindControl("login") as UserControl;
  Literal litTotal = usercontrols_login.FindControl("litWebName") as Literal;


This way we can get the controls from usercontrol and assign values at run time based on conditions.

Related Alrticles

Add Your Business in Free Listing

FREE!!! Registration