[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setting property value in Usercontrol from ASPX

Posted on 2012-08-14
8
Medium Priority
?
500 Views
Last Modified: 2012-08-17
Hi Experts,

I am working on ASP.Net 2.0 using C#

On my aspx page I have user control as below:

<ucgnews:gnews Alias="dgyt" ID="gnews2" runat="server" />

In the user control I have property called Alias

I need to set value of Alias from ASPX page in user control

My below code is not finding
ucgnews.Alias

protected void Page_Load(object sender, EventArgs e)
{
      UserControl ucgnews = (UserControl)Page.FindControl("gnews2");

      ucgnews.Alias
}

Please suggest

Thanks
0
Comment
Question by:tia_kamakshi
  • 5
  • 2
8 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38293018
Is the property Alias a public property?

gnews2 ucgnews = (gnews2)Page.FindControl("gnews2");

should be used instead of

UserControl ucgnews = (UserControl)Page.FindControl("gnews2");
0
 

Author Comment

by:tia_kamakshi
ID: 38293082
Yes, Alias is public property in User control

private string mAlias = "";

public string Alias
    {
        get { return mAlias; }
        set { if (value != null) mAlias = value; }
    }
   
Also, It is not finding gnews2
when writing below code:

gnews2 ucgnews = (gnews2)Page.FindControl("gnews2");

Please suggest

Many Thanks
0
 
LVL 5

Accepted Solution

by:
Gerry Bartley earned 2000 total points
ID: 38294091
Assuming you have added the reference for the user control to your aspx page or to web config
e.g
<%@ Register TagPrefix="ucgnews" TagName="gnews" Src="Controls/gnews.ascx" %>

Then  it should be accessible like any other control from code behind

gnews2.alias ="whatever";

or string strAlias=gnews.alias;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:tia_kamakshi
ID: 38294879
aspx code
<%@ Register Src="~/controls/media/news/GlobalNewsMainIndex.ascx" TagName="gnews" TagPrefix="ucgnews" %>
<ucgnews:gnews ID="gnews2" runat="server" />

Code behind

UserControl ucgnews = (UserControl)Page.FindControl("gnews2");
ucgnews.Alias = string.Empty;

Error
'System.Web.UI.UserControl' does not contain a definition for 'Alias'

Please suggest
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38294954
You cannot cast it to type of UserControl class.

You have to cast it to type of your UserControl classname. I'm guessing it is GlobalNewsMainIndex.

GlobalNewsMainIndex ucgnews = (GlobalNewsMainIndex)Page.FindControl("gnews2");
0
 

Author Comment

by:tia_kamakshi
ID: 38294991
I tried

The type or namespace name 'GlobalNewsMainIndex' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 14:     protected void Page_Load(object sender, EventArgs e)
Line 15:     {
Line 16:         GlobalNewsMainIndex ucgnews = (GlobalNewsMainIndex)Page.FindControl("gnews2");
Line 17:         //ucgnews.Alias = string.Empty;        
Line 18:     }
 
Please suggest
0
 

Author Comment

by:tia_kamakshi
ID: 38299073
Hi,

I found the solution.

We don't need to find usercontrol as it is already there

so the code is:
gnews2.Alias

Thanks
0
 

Author Closing Comment

by:tia_kamakshi
ID: 38305018
Applogies... This has the same solution which I found.

I misunderstood at first time. My fault. Your explanation is good

Thanks for your help
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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