This tutorials shows how we can use the FindControl method to reference controls in naming containers. C# version.
Referencing controls in ASP.NET is usually straight-forward. However, because of its complexity and adding new features such as MasterPages, the ease in referencing controls gets a little harder. Because such things as a MasterPage are naming containers, we have to reference these as well, when trying to reference a control within in. For example, if we have a label within a MasterPage, and we want to reference this label from a Content Page, then we would have to use the FindControl method of the MasterPage. This tutorial will show how we can do this. We will start by creating a Master Page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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 id="Head1" runat="server">
<title>Using FindControl with MasterPages in ASP.NET 2.0 and C#.NET</title> </head> <body>
<form id="form1" runat="server">
<div style="width:50%;border:1px solid black;">This text is in the MasterPage.<br /> <asp:Label ID="Label1" runat="server"></asp:Label><br /> This text is in the MasterPage.<br /></div> <br /> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder> </form>
</body> </html> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Then we have the Content Page:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Type in text and then click button to display text in a Label that is in the MasterPage.<br /> This is done using FindControl.<br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /><br /> <br /> Choose an item from the below list and it will be displayed in the Label that is in the MasterPage.<br /> This is done using FindControl.<br /> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> </asp:DropDownList> </asp:Content> |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The code-behind will look something like this:
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If TextBox1.Text <> "" Then
Dim Label1 As Label = CType(Master.FindControl("Label1"), Label) Label1.Text = "<b>The text you entered was: " & TextBox1.Text & ".</b>" End If End Sub Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim Label1 As Label = CType(Master.FindControl("Label1"), Label) Label1.Text = "<b>You chose <u>" & DropDownList1.SelectedValue & "</u> from the dropdown menu.</b>" End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!