6 Replies - 7730 Views - Last Post: 25 February 2011 - 05:56 PM Rate Topic: -----

#1 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Controlling ascx properties in .cs

Posted 23 February 2011 - 04:42 PM

Hello, I am using a ascx control to style properties in a asp.net website. There are multiple aspx pages that use this ascx control. Is there a way that I can change properties of the ascx page using the current page's aspx.cs file?

For example: (in ascx)<asp:Label Text="Hello" ID="helloText" runat="server"/>

(in index.aspx.cs) if("some 'if' statement")
helloText.Text="Goodbye";

My .cs files currently do not recognize the ascx attributes. Is this even possible? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Controlling ascx properties in .cs

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Controlling ascx properties in .cs

Posted 23 February 2011 - 06:51 PM

Definitely doable and really not that difficult. Just create a public property in the ascx class to set the values of the control inside the ascx file. So if you wanted to set the text of a label in the .ascx file, you could create a Text property (or name it whatever you want), then to set the value of the label you would use the ascx's .Text property.

Example:

I have a very simple user control
<%@ Control Language="C#" 
            AutoEventWireup="true" 
            CodeBehind="MyUserControl.ascx.cs" 
            Inherits="ControlTestSite.MyUserControl" %>


<asp:Label ID="lblName" runat="server" />



Simple enough, now in the codebehind for the ascx file I add a property to access the Text property of the label

public partial class MyUserControl : System.Web.UI.UserControl
    {
        public string Text
        {
            get
            {
                return lblName.Text;
            }
            set
            {
                lblName.Text = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }



Then to set the label text from the .cs file of an aspx page

protected void Page_Load(object sender, EventArgs e)
{
    MyUserControl.Text = "Hello World";
}



You would just need to create a property to access each control inside the .ascx file. Just remember that aspx and ascx pages are nothing more than classes which means they can have fields and properties just like any other class.

This post has been edited by Nakor: 23 February 2011 - 06:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: Controlling ascx properties in .cs

Posted 23 February 2011 - 08:43 PM

Hey great answer! Thanks alot
Was This Post Helpful? 0
  • +
  • -

#4 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: Controlling ascx properties in .cs

Posted 24 February 2011 - 01:55 PM

View Postsam.bengtson, on 23 February 2011 - 08:43 PM, said:

Hey great answer! Thanks alot


Ok I gave this a shot but it does not seem to work. I tried to use it exactly like you have written above, I also tried creating an instance of class "MyUserControl" but the other aspx.cs class cannot find MyUserControl. Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Controlling ascx properties in .cs

Posted 25 February 2011 - 04:43 AM

Can you post your code?
Was This Post Helpful? 0
  • +
  • -

#6 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: Controlling ascx properties in .cs

Posted 25 February 2011 - 07:05 AM

Here is my user control:
<%@ Page Language="C#" Inherits="publicClassTest.Default" CodeBehind="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
	<title>Default</title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:Button id="button1" runat="server" Text="Click me!" onclick="button1Clicked" />
	</form>
</body>
</html>



ascx.cs:
using System;
using System.Web;
using System.Web.UI;

namespace publicClassTest
{


	public partial class Default : System.Web.UI.Page
	{
		public string text = "it worked";
		public void button1Clicked (object sender, EventArgs args)
		{
			button1.Text = text;
		}
	}
}



aspx.cs:
using System;
using System.Web;
using System.Web.UI;

namespace publicClassTest
{


	public class test : System.Web.UI.Page
	{
		  
		protected void Page_Load()
		{
		Default.text = "IT WORKED :)/> ";	
		}
		
	}
}



thanks!

This post has been edited by sam.bengtson: 25 February 2011 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Controlling ascx properties in .cs

Posted 25 February 2011 - 05:56 PM

<%@ Page Language="C#" Inherits="publicClassTest.Default" CodeBehind="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
	<title>Default</title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:Button id="button1" runat="server" Text="Click me!" onclick="button1Clicked" />
	</form>
</body>
</html>




This is not a UserControl, it's just a normal aspx page. the

Quote

CodeBehind="Default.aspx.cs"

kind of hints toward that. Right now it appears your trying to put an entire aspx page inside another aspx page and that just can't be done. Asp.net will not allow you to have two form controls in one page, not to mention it just doesn't make sense to have multiple <html><head><body> tags on one page. If you look at my example above you'll see that my user control actually inherits from UserControl, and that there are no html page elements such as <html>, <body>, or <head>. Also if you are wanting to access the click event of the button in the user control from the page the user control is used in you would need to add an event handler to the user control but that's a little more complicated than just adding a text property to change the Text on something like a label which is what you originally said you wanted to do, but it is not what the code you posted is doing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1