Showing posts with label ascx. Show all posts
Showing posts with label ascx. Show all posts

Monday, March 26, 2012

Passing variable into ASCX in vb.net

I have an ASCX which I have as the header for all my pages in my site.

In this ASCX page I determine which banner to show. It's been working great so far except now I have to add the capabilities of "zones" for my pages. This means I need to pass a variable to it.

Does anyone have an example of this ? I can't seem to find anything. I'm assuming this can be done.

Thanks alot
mike123Create a public field in the user control. Then on the page that you are including the usercontrol, add the property and the value to the HTML declaration to pass in the value to the usercontrol.

So if you create a public field like so in your usercontrol:

VB:
Public myProperty As String

C#:
public string myProperty;

Then in the HTML portion of the page that has the usercontrol on it, you would have:

<[TagPrefix]:[TagName] ID="myUserControl1" myProperty="This is what I am passing." runat="server" /
Now when the control is rendered, the public field myProperty that was defined in the UserControl has the value of 'This is what I am passing.', without the single quotes.

The TagPrefix and the TagName is defined in the Register tag at the top of the page. If you are using Visual Studio, it will automatically handle that for you when you drag the user control onto the page. All you need to to is put in the myProperty portion in the appropriate tag.
You may add a property in your usercontrol class.
Hi Jimmy,

Thanks for the response. Would you happen to have an example of how this is done? I haven't attempted this before and am unsure how to go about doing that

thanks again
In your ascx class, add this code before page_load event:
Assume your usercontrol called "usercontrol1"
In usercontrol1.ascx.vb


Dim strTest As String = ""
Public Property Test() As String
Get
Return strTest
End Get

Set(ByVal Value As String)
strTest = Value
End Set

In aspx.vb


Protected WithEvents propertyTest As usercontrol1

Private Shadows Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
usercontrol1.Test = "Test"
End Sub

Passing Variable to UserControl From Parent Page

Hi All, I would like to know how to pass a variable to a user control.

An example would be _menu.ascx, which is a user control and is used in every aspx page such as default.aspx. It's properly set using the @dotnet.itags.org.Register tag in the html pages.

CODEBEHIND FOR DEFAULT.ASPX.CS
--------------
public class _default : System.Web.UI.Page
{
````public string strVariable; // want to use this variable in usercontrol

````private void Page_Load(object sender, System.EventArgs e)
````{
````````strVariable = "Hello World";
````}
}

CODEBEHIND FOR _MENU.ASCX.CS
--------------
public abstract class __menu : System.Web.UI.UserControl
{
````protected System.Web.UI.WebControls.Repeater repMenu;

````private void Page_Load(object sender, System.EventArgs e)
````{
````````DataAccess hdlData = new DataAccess();
````````repMenu.DataSource = hdlData.getMenu(??????); // stuck here!!
````````repMenu.DataBind();
````}
}use a Session["name"] object

psi
Is there another way of passing it, instead of using a Session["name"] variable? My variable is actually in the base class (business class), which all the aspx pages inherit from

CODEBEHIND FOR BUSINESS CLASS PAGEHELPER.CS
--------------------
public class PageHelper : System.Web.UI.Page
{
````public string strVariable; // want to use this variable in usercontrol

````public PageHelper()
````{
````````strVariable = "Hello World";
````}
}

CODEBEHIND FOR DEFAULT.ASPX.CS
--------------
public class _default : PageHelper
{
````private void Page_Load(object sender, System.EventArgs e)
````{
````````// this page inherits the PageHelper.cs class, which
````````// sets all the necessary variables.
````}
}

CODEBEHIND FOR _MENU.ASCX.CS
--------------
public abstract class __menu : System.Web.UI.UserControl
{
````protected System.Web.UI.WebControls.Repeater repMenu;

````private void Page_Load(object sender, System.EventArgs e)
````{
````````DataAccess hdlData = new DataAccess();
````````// this needs to get variable in PageHelper.cs class
````````repMenu.DataSource = hdlData.getMenu(??????); // stuck here!!
````````repMenu.DataBind();
````}
}
yes, you should be able to do this, and assess it from the user control:

PageHelper class is public
public string strVariable;

add this property ...
public string StrVariable
{
get{return var;}
set{var = value;}
}

you would then have to instatiate an instance of PageHelper and call the Property:
PageHelper ph = new PageHelper();

//get variable ...
ph.StrVariable

hope that helped
psi
Thank you for all the help psinyc!!!

It works now =)
good

Saturday, March 24, 2012

passing variables from an aspx file to an ascx file...

I had a page that used includes, the include files used a variable which was set in the main page for certain functions...

I want to duplicate this using asp.net but haven't been able to successfully do it.

For clarity, I want to:

*define a variable in my aspx page
*call a user control in an ascx file
*inside the user control I want to see and use the variable set in the aspx file...

Help!!

Thanks in advance!

GeorgeI'm pretty sure if you create public global variables in your ascx file you can then set their value in the aspx file:


//In your .aspx file

MyUserControl.VariableA = "this and that";


check this thread...

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=332537

passing variables from aspx page to ascx control

Is there a SOP for setting variables on an ASPX page that the usercontrols
on that page can read?
I'd like to set a 'template=' variable that various other controls can see
(the
header, navigation, footer, etc.).
Would simply setting a public variable be enough?
-DarrelYes, it is enough if you have public properties in the aspx page, you can
access those values using "Page" property of user control. Even you can
access all the controls in that page using this property.
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com
"darrel" <notreal@.hotmail.com> wrote in message
news:uOW2qIrPEHA.3748@.TK2MSFTNGP09.phx.gbl...
> Is there a SOP for setting variables on an ASPX page that the usercontrols
> on that page can read?
> I'd like to set a 'template=' variable that various other controls can see
> (the
> header, navigation, footer, etc.).
> Would simply setting a public variable be enough?
> -Darrel
>
>
> Yes, it is enough if you have public properties in the aspx page, you can
> access those values using "Page" property of user control.
Thanks, Saravana!
-Darrel

Passing variables from ASPX page to ASCX control

I'm sure this has come up before but I could not find any post on it.

How can I read a variable or property that has been set on a ASPX page from
inside a ASCX control.

ASPX code:

public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
....etc...

ASCX code:

public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected void Page_Load(object sender, EventArgs e)
{
Page callingPage = (Page) this.Page;
//How to read the "CompanyID" property from the calling page??
}

Thanks,
FernandoChange this line of code:

Page callingPage = (Page) this.Page;

to:

MyClass callingPage = (MyClass) this.Page;

Fernando Chilvarguer wrote:

Quote:

Originally Posted by

I'm sure this has come up before but I could not find any post on it.
>
How can I read a variable or property that has been set on a ASPX page from
inside a ASCX control.
>
ASPX code:
>
public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
...etc...
>
ASCX code:
>
public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected void Page_Load(object sender, EventArgs e)
{
Page callingPage = (Page) this.Page;
//How to read the "CompanyID" property from the calling page??
}
>
Thanks,
Fernando


Thanks KJ.

But when I change to MyClass my code does not compile.
How to I add a reference to that class in my Control?

Thanks,
Fernando
"KJ" <n_o_s_p_a__m@.mail.comwrote in message
news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

Change this line of code:
>
Page callingPage = (Page) this.Page;
>
to:
>
MyClass callingPage = (MyClass) this.Page;
>
Fernando Chilvarguer wrote:

Quote:

Originally Posted by

>I'm sure this has come up before but I could not find any post on it.
>>
>How can I read a variable or property that has been set on a ASPX page
>from
>inside a ASCX control.
>>
>ASPX code:
>>
>public partial class MyClass: System.Web.UI.Page
>{
> protected string _companyID;
> public string CompanyID
> {
> get
> {
> return _companyID;
> }
> set
> {
> _companyID = value;
> }
> }
>...etc...
>>
>ASCX code:
>>
>public partial class Controls_MyControl: System.Web.UI.UserControl,
>IPostBackDataHandler
>{
> protected void Page_Load(object sender, EventArgs e)
> {
> Page callingPage = (Page) this.Page;
> //How to read the "CompanyID" property from the calling page??
> }
>>
>Thanks,
>Fernando


>


I should have recognized that you are using .net 2.0 (this would
compile in 1.x).

In this case, I do not know of a means for referencing the parent page
because doing so (using the @.Reference directive, for example) would
create a circular reference.

I think a better design approach would be for the parent page to *tell*
the user control what its CustomerID is.

That is to say, create a method or property, such as SetCustomerId() or
CustomerID, on the control, rather than having the control attempt to
pull the value from the hosting page.

This may seem like double work, but, it is actually better design
because you can now put your control on *any* page without requiring
the hosting page to declare certain properties. The control is supposed
to be a self-contained entity (black box).

For example:

public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
myControl.CompanyID = value; //keeps the control and page in sync
}
}

ASCX code:

public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
}

Fernando Chilvarguer wrote:

Quote:

Originally Posted by

Thanks KJ.
>
But when I change to MyClass my code does not compile.
How to I add a reference to that class in my Control?
>
Thanks,
Fernando
"KJ" <n_o_s_p_a__m@.mail.comwrote in message
news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

Change this line of code:

Page callingPage = (Page) this.Page;

to:

MyClass callingPage = (MyClass) this.Page;

Fernando Chilvarguer wrote:

Quote:

Originally Posted by

I'm sure this has come up before but I could not find any post on it.
>
How can I read a variable or property that has been set on a ASPX page
from
inside a ASCX control.
>
ASPX code:
>
public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
...etc...
>
ASCX code:
>
public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected void Page_Load(object sender, EventArgs e)
{
Page callingPage = (Page) this.Page;
//How to read the "CompanyID" property from the calling page??
}
>
Thanks,
Fernando



define an interface in app_code that returns companyid. the have you pages
implement the interface (could inherit from a base page that implments the
interface. cast the page to the interface, and call method.

-- bruce (sqlwork.com)

"Fernando Chilvarguer" <fernando@.no-spam-impex.comwrote in message
news:uQ%23o97RzGHA.2036@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

I'm sure this has come up before but I could not find any post on it.
>
How can I read a variable or property that has been set on a ASPX page
from inside a ASCX control.
>
ASPX code:
>
public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
...etc...
>
ASCX code:
>
public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected void Page_Load(object sender, EventArgs e)
{
Page callingPage = (Page) this.Page;
//How to read the "CompanyID" property from the calling page??
}
>
Thanks,
Fernando
>


Thanks again KJ.

I used the suggested aproach (black-box) and it's all working great!
I set all the control properties from the aspx page. I also implemented all
the error-handling in case the control gets dropped into a page that does
not set up its properties.

THANKS!

"KJ" <n_o_s_p_a__m@.mail.comwrote in message
news:1157061762.574176.201880@.h48g2000cwc.googlegr oups.com...

Quote:

Originally Posted by

>I should have recognized that you are using .net 2.0 (this would
compile in 1.x).
>
In this case, I do not know of a means for referencing the parent page
because doing so (using the @.Reference directive, for example) would
create a circular reference.
>
I think a better design approach would be for the parent page to *tell*
the user control what its CustomerID is.
>
That is to say, create a method or property, such as SetCustomerId() or
CustomerID, on the control, rather than having the control attempt to
pull the value from the hosting page.
>
This may seem like double work, but, it is actually better design
because you can now put your control on *any* page without requiring
the hosting page to declare certain properties. The control is supposed
to be a self-contained entity (black box).
>
For example:
>
public partial class MyClass: System.Web.UI.Page
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
myControl.CompanyID = value; //keeps the control and page in sync
}
}
>
>
ASCX code:
>
public partial class Controls_MyControl: System.Web.UI.UserControl,
IPostBackDataHandler
{
protected string _companyID;
public string CompanyID
{
get
{
return _companyID;
}
set
{
_companyID = value;
}
}
}
>
Fernando Chilvarguer wrote:

Quote:

Originally Posted by

>Thanks KJ.
>>
>But when I change to MyClass my code does not compile.
>How to I add a reference to that class in my Control?
>>
>Thanks,
>Fernando
>"KJ" <n_o_s_p_a__m@.mail.comwrote in message
>news:1157043962.662681.143570@.m73g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

Change this line of code:
>
Page callingPage = (Page) this.Page;
>
to:
>
MyClass callingPage = (MyClass) this.Page;
>
Fernando Chilvarguer wrote:
>I'm sure this has come up before but I could not find any post on it.
>>
>How can I read a variable or property that has been set on a ASPX page
>from
>inside a ASCX control.
>>
>ASPX code:
>>
>public partial class MyClass: System.Web.UI.Page
>{
> protected string _companyID;
> public string CompanyID
> {
> get
> {
> return _companyID;
> }
> set
> {
> _companyID = value;
> }
> }
>...etc...
>>
>ASCX code:
>>
>public partial class Controls_MyControl: System.Web.UI.UserControl,
>IPostBackDataHandler
>{
> protected void Page_Load(object sender, EventArgs e)
> {
> Page callingPage = (Page) this.Page;
> //How to read the "CompanyID" property from the calling page??
> }
>>
>Thanks,
>Fernando
>


>

passing variables from aspx page to ascx control

Is there a SOP for setting variables on an ASPX page that the usercontrols
on that page can read?

I'd like to set a 'template=' variable that various other controls can see
(the
header, navigation, footer, etc.).

Would simply setting a public variable be enough?

-DarrelYes, it is enough if you have public properties in the aspx page, you can
access those values using "Page" property of user control. Even you can
access all the controls in that page using this property.

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"darrel" <notreal@.hotmail.com> wrote in message
news:uOW2qIrPEHA.3748@.TK2MSFTNGP09.phx.gbl...
> Is there a SOP for setting variables on an ASPX page that the usercontrols
> on that page can read?
> I'd like to set a 'template=' variable that various other controls can see
> (the
> header, navigation, footer, etc.).
> Would simply setting a public variable be enough?
> -Darrel
> Yes, it is enough if you have public properties in the aspx page, you can
> access those values using "Page" property of user control.

Thanks, Saravana!

-Darrel

Wednesday, March 21, 2012

passing variables to user controls

how do i pass a variable from one page or user control to the other?
i.e i have a header ascx file, and the title can be in 2 languages, the data is picked out of a db, so what i want to do is generate the sql string based on a variable that is defined from a hypelink within in another ascx file. But both these files will be common inside an/each aspx file.

Any ideas/suggestion? (I assume this is possible).

Thanks in advance.
DeianActually, you don't need to keep track of this kind of information on your own; HTTP requests include an 'Accept-Language' header which contains a setting for the language content the user prefers. (If the user is using Internet Explorer, she can set this from the Genral tab in Internet Options by clicking the 'Languages' button.)

The code for extracting this information on a Page would look like this:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ci As System.Globalization.CultureInfo
Dim FullName As String

Try
ci = New System.Globalization.CultureInfo(Request.UserLanguages(0))
FullName = ci.EnglishName
Catch
' CultureInfo only supports a subset of all valid language identifiers, so the above
' might fail for some languages.
FullName = Request.UserLanguages(0)
End Try

Label1.Text = FullName
End Sub


On a UserControl, you would need to replace 'Request' with 'Context.Request' to get to the same information.

Hope this helps,
> Any ideas/suggestion? (I assume this is possible).

Yes, it's possible.

I've put up a sample for you atwww.edition3.com/developers/deiwales
The boxes show the two separate user controls.

The code can be downloaded fromwww.edition3.com/developers/deiwales/code.zip

For anyone following this thread later, here's the code:



ASPX Page:
<%@. Page Language="VB" %>
<%@. Register TagPrefix="my" TagName="UserControlA" src="http://pics.10026.com/?src=mycontrol7.ascx" %>
<%@. Register TagPrefix="my" TagName="UserControlB" src="http://pics.10026.com/?src=mycontrol7b.ascx" %>
<script runat="server"
Sub UserControlB_Click( ByVal sender As Object, ByVal eArgs As EventArgs )
UserControlA.SetLanguage( UserControlB.Language )
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<div style="border: solid 1px silver;padding: 20px;">
<my:UserControlB id="UserControlB" onclick="UserControlB_Click" runat="server" />
</div>
<div style="border: solid 1px silver;padding: 20px;">
<my:UserControlA id="UserControlA" runat="server" />
</div>
</form>
</body>
</html>



UserControlA (to display language):
<%@. Control Language="VB" %>
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )
If Not Page.IsPostBack Then
SetLanguage( "English" )
End If
End Sub

Public Sub SetLanguage( Which As String )
If Which = "French" Then
Message.Text = "Bonjour, je suis"
Else
Message.Text = "Hello, I am"
End If
End Sub

</script>
<asp:Label id="Message" runat="server" /> UserControlA.ascx



UserControlB (to set the language):
<%@. Control Language="VB" %>
<script runat="server"
Public Language As String

Public Event Click(ByVal sender As Object, ByVal eArgs As EventArgs)

Sub Submit_Click( ByVal sender As Object, ByVal eArgs As EventArgs )
' set the language
Language = SelectedLanguage.SelectedItem.Text
' raise the event, so that our ASPX page can handle it
RaiseEvent Click( Me, EventArgs.Empty )
End Sub

Protected Overrides Function OnBubbleEvent(ByVal sender As Object, ByVal eArgs As System.EventArgs) As Boolean
' and now bubble it up
RaiseBubbleEvent(sender, eArgs)
End Function

</script>
<asp:RadioButtonList id="SelectedLanguage" runat="server">
<asp:ListItem text="English" selected="True" />
<asp:ListItem text="French" />
</asp:RadioButtonList>
<asp:Button id="Submit" text="Submit" onclick="Submit_Click" runat="server" />


Good stuff... that clarifies it.

But as for the internal support for the language ye that won't work cos my idea is to do web sites in english and provide welsh language translation at the touch of a button. So in the db the data will be held, and the string will be used to decide the logic for the sql string. Hope that clairfies my scenario.

The DB.Table is done in a way that holds data and cssstyles against page number, language and content type, and the id of everything will be generated out of the cirteria that define the content/style.

Thanks guys.

Friday, March 16, 2012

passthrough properties

(I didn't get any response on the first post of this. Is this the right
forum
for ASCX user controls?)
I am writing a user control that has a combo box in it (3rd party). I want
to pass thru properties from the control so I wrote the following:
Public WriteOnly Property AutoPostBack(ByVal TF) As Boolean
Set(ByVal Value As Boolean)
cbVendors.AutoPostBack = TF
End Set
End Property
It compiles fine but it doesn't show when I use the user control. What am I
doing wrong? Is there any good articles on passing through properties in
user controls?
Thanks,
GTry making it Public Shared...
"G. Dean Blake" wrote:

> (I didn't get any response on the first post of this. Is this the right
> forum
> for ASCX user controls?)
>
> I am writing a user control that has a combo box in it (3rd party). I wan
t
> to pass thru properties from the control so I wrote the following:
> Public WriteOnly Property AutoPostBack(ByVal TF) As Boolean
> Set(ByVal Value As Boolean)
> cbVendors.AutoPostBack = TF
> End Set
> End Property
> It compiles fine but it doesn't show when I use the user control. What am
I
> doing wrong? Is there any good articles on passing through properties in
> user controls?
> Thanks,
> G
>
>