?
Solved

How to set the value of a label control contained in a web user control I am using

Posted on 2009-02-11
8
Medium Priority
?
259 Views
Last Modified: 2012-06-21
I have a web user control named koyoto.ascx. with a code behind file named koyoto.ascx.cs
Now in koyoto.ascsx I register a to use another web user control named  EpandByCoFactors.ascx (See code snippet) In that file there is a label control named "title" and as you can see it is being set in the OnPreRenderMethod.

So here is my question. In the code behind file of koyoto.ascx.cs, how can I reference and change the value that is being set in the file EpandByCoFactors.ascx.cs?
// EpandByCoFactors.ascx.cs
using System;
using System.Web.UI;
 
namespace ScramNet.Web.Controls.Filters
{
    public partial class EpandByCoFactors : UserControl
    {
        public bool FilterApplied { get; set; }
 
        public string GroupName { get; set; }
 
        private ITemplate contentTemplate;
        public ITemplate ContentTemplate
        {
            get { return contentTemplate; }
            set
            {
                contentTemplate = value;
                ChildControlsCreated = false;
            }
        }
 
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
 
            if (ContentTemplate != null && content != null)
                ContentTemplate.InstantiateIn(content);
        }
 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
 
            string imagePath;
            if (FilterApplied)
            {
                title.Text = "Filtered";
                imagePath = "~/images/tablettl_back_orange.jpg";
            }
            else
            {
                title.Text = "Unfiltered";
                imagePath = "~/images/tablettl_back.jpg";
            }
 
            filterSpan.Style.Add("background", string.Format("url({0}) repeat-x top left", ResolveClientUrl(imagePath)));
        }
    }
}

Open in new window

0
Comment
Question by:brgdotnet
  • 5
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 23618926
Make a public property in EpandByCoFactors.ascx , which will internally modify the label control. like
public String SetTitle
        {
            get { return title.text; }
            set
            {
                title.text = value;
            }
        }
 
Then use that property in koyoto.ascx to set the label value.
Anurag
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 23619071
Thank you Anuragal sir. That makes sense. But could you show me how to access the property, since I will be accessing it from the other web user control code behind file koyoto.ascx.cs? I am thinking I might have to use the FindControl method, but I don't know how to do that?
0
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 23619090
As you ave mentioned that you have registered EpandByCoFactors.ascx in Koyoto.ascx.cs, that means you kuxt have made an object of this usercontrol in this Or given some ID to this user control.
Use that instance , that instance will have the new property you will made in EpandByCofactors.ascx.
Anurag
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:brgdotnet
ID: 23619248
Dearest Sir. God bless you for helping me!!! Now, I I created the getter setter SetTitle as you reccomended. And I followed your other advice. Please see my code below. For some reason the value is not being set. Could you please help me more on this? I believe I am close, but I can not see what I am doing wrong.

//My koyoto.ascx.cs file where I set the value. This currently does not work
        protected override void OnLoad(EventArgs e)
        {
            filterAlerts.SetTitle = "Hello";
Here is my koyoto.ascx file where I assign and id of filterAlerts at line 13 below. And then in my code behind file 
 
// first 13 line of koyoto.ascx
 
1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="koyoto.ascx.cs" Inherits="ScramNet.Web.Controls.Filters.koyoto" %>
2 <%@ Register TagPrefix="uc" TagName="EpandByCoFactors" Src="~/Controls/Filters/EpandByCoFactors.ascx" %>
3    
4    <table width="100%" cellpadding="0" cellspacing="10" class="formTable">   
5    <tr>
6   <div style="margin:10px 0">	
7    </div>
8    </tr>
9    <tr>
10    <asp:Label ID="Label40" runat="server" ForeColor="Red" Text="*Indicates Required Fields"></asp:Label>
11    </tr>
12    </table>
13    <uc:EpandByCoFactors id="filterAlerts" runat="server" >

Open in new window

0
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 23619261
can you paste the code where you have made the property.
Also debug your application & see whether it goes to EpandByCoFactors.ascx.cs file to set the property.
Anurag
0
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 23619269
In EpandByCoFactors.ascx.cs in function OnPreRender you are again setting title.text
if (FilterApplied)
            {
                title.Text = "Filtered";
                imagePath = "~/images/tablettl_back_orange.jpg";
            }
            else
            {
                title.Text = "Unfiltered";
                imagePath = "~/images/tablettl_back.jpg";
            }

 
This will overwrite the "Hello" value you are setting
Anurag
 
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 23622308
I traced through the code and it looks like the value I am trying to set, is being reset in the PreRender method of the user control. I tried overiding the pre-render method in the koyoto.ascx.cs code but that does not work. So it looks like the PreRender method of the control EpandByCoFactors.ascx.cs is being called last. Below is the code snipped.


using System;
using System.Web.UI;
 
namespace RamNet.Web.Controls.Expand
{
    public partial class EpandByCoFactors : UserControl
    {
        public bool FilterApplied { get; set; }
 
        public string GroupName { get; set; }
 
        private ITemplate contentTemplate;
        public ITemplate ContentTemplate
        {
            get { return contentTemplate; }
            set
            {
                contentTemplate = value;
                ChildControlsCreated = false;
            }
        }
 
        public String SetTitle
        {
            get { return title.Text; }
            set
            {
                title.Text = value;
            }
        }
 
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
 
            if (ContentTemplate != null && content != null)
                ContentTemplate.InstantiateIn(content);
        }
 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
 
            string imagePath;
            if (FilterApplied)
            {
                title.Text = "Filtered";
                imagePath = "~/images/tablettl_back_orange.jpg";
            }
            else
            {
                title.Text = "Unfiltered";
                imagePath = "~/images/tablettl_back.jpg";
            }
 
            filterSpan.Style.Add("background", string.Format("url({0}) repeat-x top left", ResolveClientUrl(imagePath)));
        }
    }
}

Open in new window

0
 
LVL 11

Accepted Solution

by:
Anurag Agarwal earned 900 total points
ID: 23624378
yes the pre render method is the one of the last methods which is get called.
Anurag
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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