?
Solved

find control from usercontrol i my masterpage?

Posted on 2011-10-04
3
Medium Priority
?
497 Views
Last Modified: 2012-05-12
Hi.

I have:
Content page
Master page
usercontrol loaded on master page including liAmount

From content page, I want to change liAmount in my usercontrol at my masterpage.

How is this possible.

It would make sense if following would work;
((Literal)BasketAmount.Master.FindControl("liAmount")).Text = "1";
0
Comment
Question by:Jesper Christensen
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 36913471
Hello, in this way, follow this example:

MyUserControl.ascx
<%@ Control Language="C#" ClassName="MyUserControl" %>
<script runat="server">
</script>
<asp:Label Text="text" runat="server" ID="liAmount" />

Open in new window


MasterPage.master
<%@ Master Language="C#" %>

<%@ Register Src="MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:MyUserControl ID="MyUserControl1" runat="server" />
        <br />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Open in new window


DemoUserControl.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        UserControl myControl = (UserControl)this.Master.FindControl("MyUserControl1"); 
        ((Label)myControl.FindControl("liAmount")).Text = Environment.TickCount.ToString();
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Set Value" />
</asp:Content>

Open in new window


Or you can download the files from this link:
DemoUserControl.zip
0
 

Author Comment

by:Jesper Christensen
ID: 36913558
Ok i Will try that and get back to you. Thank you so far :))
0
 

Author Comment

by:Jesper Christensen
ID: 36915567
Whank you, it works perfectly !!

This was just what I was looking for:
UserControl myControl = (UserControl)this.Master.FindControl("MyUserControl1");
((Label)myControl.FindControl("liAmount")).Text = Environment.TickCount.ToString();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question