Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Thursday, March 29, 2012

passing values between pages (one is previouspage) with asp.net 1.x

i have a page opens a popup in that popup i get some property values for control in the previous page

i need to pass the values to the control vwhen i clik a button

what is the best choise

when i need session i can'nt refresh the previous page correctly so it fails for me (browser asks me a msgbox)

when i want to pass with query string my value is a html tag it allso fails because of security

thanks for helps

i realy need help talk to me guys

Can you clarify the question a bit.

You have a window you pop up that gathers some data that you want to write back to the previous page that the window popped up from?

If so you can use javascript to do it:


function SelectItem(item, invalue)
{
if (window.opener)
if (window.opener.closed == false)
if (window.opener.document && window.opener.document.forms[0] &&
window.opener.document.forms[0].elements[item])
{
window.opener.document.forms[0].elements[item].value = invalue;
}
window.close();
}

Passing values between User Controls

I programmatically add a user control to a page which has a data grid on it. The user can then click on a link button to drill down to detail information.

What I would like to do is unload the existing user control and add the new user control with the data grid showing the detail information.

The question is how do I pass the key values from the existing user control to the new user control ?

Thanks

MarkYou can add your Items to the CurrentContext in order to pass from one control to another or from Page to an control


Context.Items.Add("MyValue", myValue)

To retrieve


Context.Items["MyValue"]

The problem here is when I click on the LinkButton on the DataGrid its going to cause the Parent page to reload itself and in the parents Page_Load subroutine its going to unload the existing Datagrid and load up the User Control with the detail DataGrid on. Therefore, it will never pass through the LinkButton's event to set the Context value.

Unless the secret is to load up both User Controls, on the Post Back, but hide the Header DataGrid ? Or to use client side code to pass the key of the selected record to a hidden field which will get submitted back to the server and be available much sooner in the process ?

I would be interested to here what people think would be the best way of doing this or whether there is a way to still use the Context stuff.

Cheers

Mark
Hi,

you could use a delegate.This article explains it how you could do it.

Grz, Kris.
Here Context is the HttpContext which is always available in a page and user controls. You can use this on the Page Load to store some values and then retrieve it later in the User controls as shown in my previous post.

Monday, March 26, 2012

Passing Values To A User Control

I have seen examples where you can set values in a user control by typing in
the value like te following:
<uc1:MyUserCtrl id="myUserCtrl" ScreenName="User Entry" runat="server" />
So in the case I am populating a variable called "ScreenName" with "User Ent
ry".
Can I not create properties in the main codebehind form and make these prope
rties availble to the usercontrol? I know you can create properties in the
control and make those availble to the main page, but how can you go the oth
er way?
Thanks!"Issac Gomez" <anonymous@.discussions.microsoft.com> wrote in message
news:D7323912-CD68-461C-8DB0-CDC414DD4E8E@.microsoft.com...
> I have seen examples where you can set values in a user control by typing
in the value like te following:
> <uc1:MyUserCtrl id="myUserCtrl" ScreenName="User Entry" runat="server" />
> So in the case I am populating a variable called "ScreenName" with "User
Entry".
> Can I not create properties in the main codebehind form and make these
properties availble to the usercontrol? I know you can create properties in
the control and make those availble to the main page, but how can you go the
other way?
That's the reverse of what you should do. The Page should be in charge, not
the user control. If you want to pass something from the page to the user
control, then have the page set a property of the user control.
--
John Saunders
johnwsaundersiii at hotmail
sure ya can
Make sure to set a public property in the control
From there you should be able to
1) declare the control in the codebehind
2) populate it
myUserCtrl.ScreenName = "User Entry"
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Issac Gomez" <anonymous@.discussions.microsoft.com> wrote in message
news:D7323912-CD68-461C-8DB0-CDC414DD4E8E@.microsoft.com...
> I have seen examples where you can set values in a user control by typing
in the value like te following:
> <uc1:MyUserCtrl id="myUserCtrl" ScreenName="User Entry" runat="server" />
> So in the case I am populating a variable called "ScreenName" with "User
Entry".
> Can I not create properties in the main codebehind form and make these
properties availble to the usercontrol? I know you can create properties in
the control and make those availble to the main page, but how can you go the
other way?
> Thanks!
You can access public property of the main page in usercontrol like this,
DirectCast(Page, <mainpage> ).<publicproperty>
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com
"Issac Gomez" <anonymous@.discussions.microsoft.com> wrote in message
news:D7323912-CD68-461C-8DB0-CDC414DD4E8E@.microsoft.com...
> I have seen examples where you can set values in a user control by typing
in the value like te following:
> <uc1:MyUserCtrl id="myUserCtrl" ScreenName="User Entry" runat="server" />
> So in the case I am populating a variable called "ScreenName" with "User
Entry".
> Can I not create properties in the main codebehind form and make these
properties availble to the usercontrol? I know you can create properties in
the control and make those availble to the main page, but how can you go the
other way?
> Thanks!

Passing values to a user control

Hi,

I need to pass a value to an user control. If the value is static I know that I can use this syntax

<HEAD:HEADER id="Header2" value="some value" runat="server"></HEAD:HEADER>

and in my user control I created a public variableVALUE

This works fine.

Now I need to pass a value that I receive in my page via Request.Params(), so I try this way:

<HEAD:HEADER id="Header2" value="<%=MyValue%>" runat="server"></HEAD:HEADER>

Where myValue is a public variable in my aspx page. The problem is that I'm getting an error saying I cannot use <% .. %> in a server tag.

Then I tried this:

<HEAD:HEADER id="Header2" value="<%#MyValue%>" runat="server"></HEAD:HEADER>

Now, I don't get the error but I don't get the value in my usercontrol.

Any ideas??

Thank you so much.

SandraUser controls do have this problem. I solved it by setting the value in code-behind.
Hi JimRoss,

May you show me with and example how to solve this problem?

Thanks in advance.

Sasarivera
1> Declare your control in the codebehind (I am showing it in C# style):
blabla.blabla.Header Header2 ;
2> assign the value:
Header2.value = Request.Params["MyValue"];
Hope this helps.
Hi chunshahab,

Thanks for your code, It solved my problem.

Thanks again.

Sasarivera

passing variable from code behind to function in javascript

Just wondering if someone could provide an example of passing a variable from
the code behind to javascript in vb. I want to have one control have focus
with the page loading with one condition and have another control with focus
after the page loads from a different condition. A gets set in the code
aspx.vb file and the script might look something like,
<script language="javascript">
function DoHighlight(a)
{
if a = 1 {
document.Form1.dr_lst_system.focus();
}
else
{
Form1.textbox1.focus();
}
}
</script
thanks.
--
Paul G
Software engineer.Hi Paul,

It is impossible to pass a variable value from the server to the client.
Server-side code executes on the server. Client-side code executes on the
client.

That said, it is certainly possible to WRITE client-side scripting code
(which will be executed on the client) dynamically from the server side.
That is what you need to do.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> Just wondering if someone could provide an example of passing a variable
> from
> the code behind to javascript in vb. I want to have one control have
> focus
> with the page loading with one condition and have another control with
> focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
> --
> Paul G
> Software engineer.
Paul,

This is normally done with hidden input controls. Put a line

<input type=hidden runat=server id=inhA
in the .aspx file. If you switch from Html view to Design and back, you will
find in the code-behind a definition for inhA as an obyect of type
System.Web.UI.HtmlControls.HtmlInputHidden.

You can now set inhA.Value="1" on server side and check if (inhA.value ==
"1") on client side.

Eliyahu

"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> Just wondering if someone could provide an example of passing a variable
from
> the code behind to javascript in vb. I want to have one control have
focus
> with the page loading with one condition and have another control with
focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
> --
> Paul G
> Software engineer.
Hi thanks for the response. Here is what I have but it does not seem to be
working. In the Java script I have
<form id="Form1" method="post" runat="server">
<input type="hidden" runat="server" id="inhA" NAME="inhA">
<script language="javascript" event="onload" for="window">
if (inhA.value == 0)
{Form1.dr_tx_names.focus();}
if (inhA.value == 1)
{Form1.dr_lst_system.focus();}
</script>and in the aspx.vb file I have
in page load event
If Not Page.IsPostBack Then 'only first time page loads
Me.inhA.Value = 0
and in a dropdown event select-causes the page to reload I have
Me.inhA.Value = 1
**************so I want one control two have focus if inhA = 0 and the other
to have focus if inhA = 1.
thanks for the information.
--
Paul G
Software engineer.

"Eliyahu Goldin" wrote:

> Paul,
> This is normally done with hidden input controls. Put a line
> <input type=hidden runat=server id=inhA>
> in the .aspx file. If you switch from Html view to Design and back, you will
> find in the code-behind a definition for inhA as an obyect of type
> System.Web.UI.HtmlControls.HtmlInputHidden.
> You can now set inhA.Value="1" on server side and check if (inhA.value ==
> "1") on client side.
> Eliyahu
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> > Just wondering if someone could provide an example of passing a variable
> from
> > the code behind to javascript in vb. I want to have one control have
> focus
> > with the page loading with one condition and have another control with
> focus
> > after the page loads from a different condition. A gets set in the code
> > aspx.vb file and the script might look something like,
> > <script language="javascript">
> > function DoHighlight(a)
> > {
> > if a = 1 {
> > document.Form1.dr_lst_system.focus();
> > }
> > else
> > {
> > Form1.textbox1.focus();
> > }
> > }
> > </script>
> > thanks.
> > --
> > Paul G
> > Software engineer.
>
On Wed, 29 Jun 2005 11:02:04 -0700, Paul wrote:

> Just wondering if someone could provide an example of passing a variable from
> the code behind to javascript in vb. I want to have one control have focus
> with the page loading with one condition and have another control with focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
Something like
if(<%=a%> = 1) will work (a is a server-side variable)
ok thanks for the information.
--
Paul G
Software engineer.

"intrader" wrote:

> On Wed, 29 Jun 2005 11:02:04 -0700, Paul wrote:
> > Just wondering if someone could provide an example of passing a variable from
> > the code behind to javascript in vb. I want to have one control have focus
> > with the page loading with one condition and have another control with focus
> > after the page loads from a different condition. A gets set in the code
> > aspx.vb file and the script might look something like,
> > <script language="javascript">
> > function DoHighlight(a)
> > {
> > if a = 1 {
> > document.Form1.dr_lst_system.focus();
> > }
> > else
> > {
> > Form1.textbox1.focus();
> > }
> > }
> > </script>
> > thanks.
> Something like
> if(<%=a%> = 1) will work (a is a server-side variable)

Passing Variable To Controls

If we have a web page in which any simple control is present for example a TextBox

<asp:TextBoxID="TextBox1"runat="server"Text=""></asp:TextBox>

Now in code file a public variable is defined like

publicstring txtValue ="Khan";

Now I want to assign thetxtValue inText filed of TextBox Just like

<asp:TextBoxID="TextBox1"runat="server"Text="<%=txtValue%>"></asp:TextBox>

But this approach is not working.

Can anyone tell it?

Modify your markup to:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# GetValue %>' />
 Then you should make an at least protected function in your code behind to return the string:
 
protected string GetValue(){return txtValue;}

I have tried it but it is getting error.

Please check it on you own side.


This syntax should work. I've used it a dozen times in my apps.

What is the error you're getting?


Default.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclass_Default : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedstring GetValue()

{

return txtValue;

}

}

Default.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"Debug="true" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue %>"></asp:TextBox>

</div>

</form>

</body>

</html>

And the error which I am getting is

Error 1 The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments D:\New Project\dawateislamix.net\Default.aspx 12

Error 2 Argument '1': cannot convert from 'method group' to 'object' D:\New Project\dawateislamix.net\Default.aspx 12


Can you try with<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


Modify your html as follows:

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


There is not any error now.

But still the value is not passed to the control.


Pls try to set the value in

Page_Load() event.

txtValue ="Zeeshan" ;


Default2.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:textboxID="Textbox1"runat="server"Text="<%# GetValue() %>"></asp:textbox>

</div>

</form>

</body>

</html>

Default2.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclassDefault2 : System.Web.UI.Page

{

privatestring txtValue;protectedstring GetValue()

{

return txtValue;

}

protectedvoid Page_Load(object sender,EventArgs e)

{

txtValue ="Zeeshan";

}

}

Please check it.

It still does not show the text.


sorry guys its as simple as this

in your code behind you have this

publicpartialclasscodedisplay : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedvoid Page_Load(object sender,EventArgs e)

{

TextBox1.Text = txtValue;

}

}

and in your aspx you have this

<asp:TextBoxID="TextBox1"runat="server"/>

with all form and html elements.

Good luck


You can do it in your code behind instead of in your markup:

protectedvoid Page_Load(object sender,EventArgs e)

{

if (!IsPostBack)

TextBox1.Text = GetValueStr();

}

privatestring txtValue ="test";protectedstring GetValueStr()

{

return txtValue;

}


But If I want to do it on Mark up then.


Well if you want to do it on mark up then, put it straight in

text="Zeeshan"

text is a property and hence

Don't use code blocks in ASPNET. Avoid if you can avoid it because they are written for backward compatability; instead write an event handler or startup routine in the codebehind which populates the property on the control as necessary.

For example, you could do it in Page_Load - but be sure to surround it with if (! PostBack) so that the value doesn't get overwritten when posting back.

In late binding controls i.e. say a textbox in a gridview etc, you can databind but that again uses reflection etc and hence is slow.


Hi,

If you have to do it in the markup use single quotes instead Text='<%=MyPublicVariable %>'

HTH

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 between web forms

Hi There,
My question is as following,
I have a webform (Webform1) which contains one label control and command
button, when the user clicks the button another webform (Webform2) appears,
webform2 contains only one control which is the Calendar control, I want to
do the following
1- when the user selects a date from the webforms, I want the value
(selected date) appears in the label control in the webform1
2- Webform2 to be closed automatically after the date selection
Kind regards and thanks in advancesearch MSDN for showModalDialog method. That will do just what you're
looking for.
Dale
"Aitham alama" <h_salama@.yahoo.com> wrote in message
news:eSfH8gcZEHA.3716@.TK2MSFTNGP11.phx.gbl...
> Hi There,
> My question is as following,
> I have a webform (Webform1) which contains one label control and command
> button, when the user clicks the button another webform (Webform2)
appears,
> webform2 contains only one control which is the Calendar control, I want
to
> do the following
> 1- when the user selects a date from the webforms, I want the value
> (selected date) appears in the label control in the webform1
> 2- Webform2 to be closed automatically after the date selection
> Kind regards and thanks in advance
>

Passing Variables between web forms

Hi There,

My question is as following,

I have a webform (Webform1) which contains one label control and command
button, when the user clicks the button another webform (Webform2) appears,
webform2 contains only one control which is the Calendar control, I want to
do the following

1- when the user selects a date from the webforms, I want the value
(selected date) appears in the label control in the webform1
2- Webform2 to be closed automatically after the date selection

Kind regards and thanks in advancesearch MSDN for showModalDialog method. That will do just what you're
looking for.

Dale

"Aitham alama" <h_salama@.yahoo.com> wrote in message
news:eSfH8gcZEHA.3716@.TK2MSFTNGP11.phx.gbl...
> Hi There,
> My question is as following,
> I have a webform (Webform1) which contains one label control and command
> button, when the user clicks the button another webform (Webform2)
appears,
> webform2 contains only one control which is the Calendar control, I want
to
> do the following
> 1- when the user selects a date from the webforms, I want the value
> (selected date) appears in the label control in the webform1
> 2- Webform2 to be closed automatically after the date selection
> Kind regards and thanks in advance

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

Passing variables from datagrid to code-behind?

This is a bit embarassing, but I just can't seem to figure it out...
I have a simple datagrid that uses a calendar control to display information by a selected date. One field of the datagrid is a hyperlink column that allows the user to see additional details about each item in a pop-up window.
The pop-up window is another simple datagrid, using a code-behind to look-up the extra information.
The (Access) database uses separate tables for each month of the year (0105, 0205, etc.) and which table we need to read is determined by the selected month of the calendar control.
But I cannot figure out how to pass the month to the code behind to use the correct table in the query for detailed information.
Here is the primary datagrid hyperlink column:
<asp:HyperLinkColumn
DataTextField="Title"
DataNavigateUrlField="ResvNum"
DataNavigateUrlFormatString="javascript: var popWindow=window.open('event-detail.aspx?id={0}', 'popWindow'); popWindow.focus();"
Target="_self"
HeaderText="Title">
</asp:HyperLinkColumn>
[ResvNum is the primary key for each table, Access auto-increment]
And here is the code-behind (minus 'Import' statements):
public class Detail : Inherits System.Web.UI.Page
public eventDetail as DataGrid

private sub Page_Load (sender as object, e as System.EventArgs)
dim _id as String = Request.QueryString("id")
dim strSQL as String = "SELECT [0805].ResvNum, [0805].Dy, [0805].BegTime, [0805].Ending, [0805].EventType, [0805].Title, [0805].Person, Facility.BldgName, Facility.Abbrev " & _
"FROM Facility LEFT JOIN [0805] ON Facility.FacNum = [0805].Room " & _
"WHERE [0805].ResvNum = " & _id & ""
eventDetail.DataSource = getDetail(strSQL)
eventDetail.DataBind()
end sub

private function getDetail (strSQL as string) as oleDbDataReader
dim dtrDetail as oleDbDataReader
dim detailConn as oleDbConnection = new oleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("../TESTDATA/TEST.mdb") & ";")
dim detailCmd as oleDbCommand = new oleDbCommand(strSQL, detailConn)
detailCmd.Connection.Open()
dtrDetail = detailCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
return dtrDetail
end function
end class
This is hard-coded to read the data table for August 2005. It is the [0805] I need to dynamically change depending on the selected date.
How do I pass that date to the code-behind query?
Thanks,
Tinker

You can only send 1 value if you use the datanavigatfield - but you canconstruct your own datanavigaturl and use template columns
<ItemTemplate>
<asp:HyperLink id="linkColumn1" runat="server"Text='<%# DataBinder.Eval(Container,"DataItem.MyDataField") %>'Target=_blank NavigateUrl='<%#"event-detail.aspx?id="& DataBinder.Eval(Container,"DataItem.MyDataField") & "?other_param=" & DataBinder.Eval(Container,"DataItem.MyOtherDataField") %>'>
</asp:HyperLink>
</ItemTemplate>
HTH

uncleb --
Thank you for this, and it makes sense -- but the file identifier is not a dataitem. It is derived from the calendar control as
qryFile = calDate.SelectedDate.ToString("MMyy")
and used in the query as
strSQL = "SELECT ResvNum, Person FROM qryFile"
So I can't pass it as part fo the dataitems collection.
Or -- at least -- I still don't know how to identifiy it to send it on to the code-behind.
If there's a refernce article, please send me off to read it.
Thanks again,


<ItemTemplate>
<asp:HyperLink id="linkColumn1" runat="server"Text='<%# DataBinder.Eval(Container,"DataItem.MyDataField") %>'Target=_blank NavigateUrl='<%#"event-detail.aspx?id="& DataBinder.Eval(Container,"DataItem.MyDataField") & "?qryFile=" &calDate.SelectedDate.ToString("MMyy") %>'>
</asp:HyperLink>
</ItemTemplate>
if the calendar is on the same page and loaded with the right date . Ifnot, I would use a button - then in the click event set the urlstringusing the datagrid datakeyfield and the calDate

HTH

YES! This works!
Thanks for hanging-in and guiding me through this. Appreciate it HUGELY.

Okay, I spoke too soon...
<asp:HyperLink id="linkColumn1" runat="server"
Text = '<%# DataBinder.Eval(Container,"DataItem.Title") %>'
NavigateUrl = '<%# "event-detail.aspx?qryFile = " & calDate.SelectedDate.ToString("MMyy") & "& ?id = "& DataBinder.Eval(Container, "DataItem.ResvNum") %>'
Target = _blank>
</asp:HyperLink>

Does not seem to be passing the second variable to the code behind, regardless of whether I rearrange the variable to make "id" first or second.
In the code-behind, I added:
trace.warn ("Incoming File " & Request.QueryString("qryFile "))
trace.warn ("Incoming ID " & Request.QueryString("id"))

dim _qryFile as String = Request.QueryString("qryFile ")
dim _id as String = Request.QueryString("id")

trace.warn ("Query File " & _qryFile)
trace.warn ("Query ID " & _id)
And these are the results of the trace.warnings:
Incoming File = 0805
Incoming ID =
Query File = 0805
Query ID =

(Reversing the order in which I pass the variables -- putting "id" first -- passes the ID, but not the qryFile date)
What am I doing wrong in the HyperLink NavigateUrl statement?


try..
NavigateUrl = '<%# "event-detail.aspx?qryFile=" &calDate.SelectedDate.ToString("MMyy") & "?id=" &DataBinder.Eval(Container, "DataItem.ResvNum") %>'
I took out an " &" and got rid of some spaces - no spaces in querystrings.



Well, you're still haniging in with me, and I appreciate it.
Didn't work. When I remove the '&' in the navurl, the variables are passed as a single variable, and the trace showsFileID = 0805?id=1769andResvNum=. I did remove all of the blank spaces, however.
I'm not sure what I'm seeing here, but maybe this will help: when the cursor is over the link on the (first page) data grid, the IE status bar shows that the link is
events-detail.aspx?qryFile=0805&?id=1120 -- which is a valid record id (ResvNum) in an existing table, and that LOOKS right to me. But the "id" is still not making it to the code-behind...
I'm kind of at a loss with this, and I'm just not smart enough to see what I'm doing wrong.
If there's anything more I can add to this thread that might help pinpoint my error, let me know?

Sorry , my fault

try..
NavigateUrl = '<%# "event-detail.aspx?qryFile=" &calDate.SelectedDate.ToString("MMyy") & "&id=" &DataBinder.Eval(Container, "DataItem.ResvNum") %>'
there is only one question mark in a querystring- params are seperated by "&"

So was it all just a test to see if I'd finally admit how dumb I am? 8-)
The two "?" was the problem; it is all working. I've been staring at it for three days and didn't see it.
Again, many thanks.

Wednesday, March 21, 2012

passing variables from page to control

I have a simple index.aspx page that declares and sets a sectionID in the
Page_Load. The in a user control later on in the page, I read the sectionID
variable (in the control's Page_Load) to determine which panel to show. It
does not work
"Name 'sectionID' is not declared" is my error while compiling the user
control.
What am I doing wrong?
This is in my main page's Page_Load
Dim sectionID As String = "about"
--
This is in my user control's Page_Load
If sectionID = "about" Then
pnlAbout.Visible = True
ElseIf sectionID = "home" Then
pnlHome.Visible = True
End If
_____
DC GMake a public property or method on your control that accepts the value from
your page. Something like this:
Public Sub SetSection(Section as Integer)
'Now I have the value I need
End Sub
The from your page, call it like this to pass the value along:
MyControlName.SetSectionID(sectionID)
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"DC Gringo" <dcgringo@.visiontechnology.net> wrote in message
news:OHsJ5uBREHA.2876@.TK2MSFTNGP09.phx.gbl...
> I have a simple index.aspx page that declares and sets a sectionID in the
> Page_Load. The in a user control later on in the page, I read the
sectionID
> variable (in the control's Page_Load) to determine which panel to show.
It
> does not work
> "Name 'sectionID' is not declared" is my error while compiling the user
> control.
> What am I doing wrong?
>
> This is in my main page's Page_Load
> Dim sectionID As String = "about"
> --
>
> This is in my user control's Page_Load
> If sectionID = "about" Then
> pnlAbout.Visible = True
> ElseIf sectionID = "home" Then
> pnlHome.Visible = True
> End If
>
> _____
> DC G
>

passing variables to a server control

Hi,

I'm maintaining a website with alot of files, and every page uses server controls for both the header and the footer. On some of the pages I don't want to show part of the header, however I am still including the header. The only way I can think to do this is to have a seperate header for the pages I want to display different . This is a major pain if I have to do it this way. Is there anyway I can pass a variable to the header server control ? I'd love to pass an "on" or "off" and display the part of the header accordingly. This is what my relevant parts of my page look like on every page

<%@dotnet.itags.org.RegisterTagPrefix="abc"TagName="myHeader"Src="../includes/myHeader.ascx" %>

<html>

<body>

<abc:myHeaderrunat="server"id="myHeader"/>

...........

</body>

</html>

If you have source access to your header/footer server control, then you can create a property called something like 'DisplayMode' and assign it with a default value, i.e.: ON. And inside these controls, do a check on the mode what to display.

Your server control on the page files would be look like:

<abc:myHeader runat="server" id="myHeader" DisplayMode="Off" />

hi Jack,

This sounds great. How would I go about creating the property? Could you possibly give me an example ? :)

Much appreciated,

mikeg123


Hi,

Well I think you can do it in various ways.

1. Make some public properties within your UserControl and the pages where you don't want to use certain areas make them invisible through the properties.
2. You can write some code in your Web user control in which you can check the current URL and make the portion available or not available. You can get the current url by Request.Path.ToString(); But for sure this method will be a lengthy one because you have to mention all the url's for which you want to make changes in the header.
3. You can pass a query string variable and check it on the UserControl's load event with Request.QueryString["variableName"]. I think this would be a reasonable way because for certain situations you just have to pass the value in the query string variable and you write code in the user control to handle the situation.

These are the possible ways that I can advise you there might be more.

I hope it will solve your problem

Thanks and best regards,


#2 & #3 wont work for me, however your first suggestion sounds great

I have created this property in the user control, but I'm not sure how I am supposed to access the information I am passsing ?

PublicProperty displayBanner()

Get

EndGet

Set(ByVal value)

EndSet

EndProperty

thanks very much!!


Example in c#:

public class myHeader{// private fieldsprivate string _displayMode ="On";// you can work little more to create an Enum to limit types ...// public propertypublic string DisplayMode {set {if (!string.IsNullOrEmpty(value)) { _displayMode =value; } } } ...// eventsprotected void OnLoad() {if (string.Compare(_displayMode,"off",true) == 0) {// hide control }else if (string.Compare(_displayMode,"alternate",true) == 0) {// display an alternate view }else {// show up normally } }}

Hi Jack,

You wouldnt be able to give me one in VB would you? I apologize for not specifying before, I thought I would be able to convert it, but this is pretty confusing for me.

Very much appreciated, have a good day!

Thanks again,

hehe, I'm not a VB.NET developer. however, I've converted to VB.NET, let me know if it works. I think you should get the idea at least.

Public Class myHeader' private fieldsPrivate _displayModeAs String ="On"' you can work little more to create an Enum to limit types ...' public propertyPublic WriteOnly Property DisplayMode()As String Set (ByVal ValueAs String)If Not String.IsNullOrEmpty(value)Then _displayMode = valueEnd If End Set End Property ...' eventsProtected Sub OnLoad()If String.Compare(_displayMode,"off",True) = 0Then' hide controlElse If String.Compare(_displayMode,"alternate",True) = 0Then' display an alternate viewElse' show up normallyEnd If End SubEnd Class

awesome thanks so much it worked!!

this is a problem ive been pushing under the rug for literally two years, much apprecaited !

passing variables to a user control

Hi
I'm new to C#, moving from ASP, and slightly so bear with me!
Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user contro
l
based on the page (everything else would be duplicated, hence the user
control).
Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..
public int pageID = 1;
However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!
Alternatively, I could use a URL object to get the page were on directly in
the usercontrol, but I couldnt find a URL method/property to give me just th
e
file name.
Hope all that makes sense!
Cheers
DanYou should try to put a public property on your user control. In your html
code set your property.
<uc1:UserControl id="MyControl" runat="server"
MyPublicProp="1"></uc1:UserControl>
If you want to do this from code, first, you should declare your user
control in your page because visual studion didn't do this by default.
it should look like this
protected MyPath.To.Control.ControlName MyControl;
after this you can acces your control properties normally..
MyControl.MyPublicProp ="1";
Cheers
"Dan Nash" wrote:

> Hi
> I'm new to C#, moving from ASP, and slightly so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user cont
rol
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly i
n
> the usercontrol, but I couldnt find a URL method/property to give me just
the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
>
Hi,
Create an interface. eg:
public interface ICommonPage
{
int PageID{get;}
}
then implement it in the classes that contain the usercontrol in question
like this:
public class page1 : System.Web.UI.Page, ICommonPage
{
//Hardcode the value in a field:
int _PageID = 1; // or 2 in page2, 3 in page3, etc.
// The actual implementation:
public ICommonPage.PageID
{
get{return _PageID;}
}
// the rest of the class as it is...
}
And finally in the usercontrol cast the Page property to the interface and
get the value:
int PageID = ((ICommonPage)Page).PageID;
// work with PageID...
Hope this helps
Martin
"Dan Nash" <dan@.musoswire.co.uk> wrote in message
news:1D302C45-765A-46AE-AB82-631BB104CB6A@.microsoft.com...
> Hi
> I'm new to C#, moving from ASP, and slightly so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user
control
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly
in
> the usercontrol, but I couldnt find a URL method/property to give me just
the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
>
Thanks guys!
Psychos post solved it, I guess you would say, the easy way! However,
Martin's solution looks interesting.. might try that one when I know a bit
more about C# (only got it Friday!).
Would there be any advantage with ICommonPage over the second method that
Psycho suggested?
Cheers guys
Dan
"Dan Nash" wrote:

> Hi
> I'm new to C#, moving from ASP, and slightly so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user cont
rol
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly i
n
> the usercontrol, but I couldnt find a URL method/property to give me just
the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
>

passing variables to a user control

Hi

I'm new to C#, moving from ASP, and slightly confused so bear with me!

Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user control
based on the page (everything else would be duplicated, hence the user
control).

Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..

public int pageID = 1;

However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!

Alternatively, I could use a URL object to get the page were on directly in
the usercontrol, but I couldnt find a URL method/property to give me just the
file name.

Hope all that makes sense!

Cheers

DanYou should try to put a public property on your user control. In your html
code set your property.

<uc1:UserControl id="MyControl" runat="server"
MyPublicProp="1"></uc1:UserControl
If you want to do this from code, first, you should declare your user
control in your page because visual studion didn't do this by default.

it should look like this
protected MyPath.To.Control.ControlName MyControl;
after this you can acces your control properties normally..

MyControl.MyPublicProp ="1";

Cheers
"Dan Nash" wrote:

> Hi
> I'm new to C#, moving from ASP, and slightly confused so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user control
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly in
> the usercontrol, but I couldnt find a URL method/property to give me just the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
>
Hi,

Create an interface. eg:

public interface ICommonPage
{
int PageID{get;}
}

then implement it in the classes that contain the usercontrol in question
like this:

public class page1 : System.Web.UI.Page, ICommonPage
{
//Hardcode the value in a field:
int _PageID = 1; // or 2 in page2, 3 in page3, etc.

// The actual implementation:
public ICommonPage.PageID
{
get{return _PageID;}
}

// the rest of the class as it is...
}

And finally in the usercontrol cast the Page property to the interface and
get the value:

int PageID = ((ICommonPage)Page).PageID;

// work with PageID...

Hope this helps
Martin
"Dan Nash" <dan@.musoswire.co.uk> wrote in message
news:1D302C45-765A-46AE-AB82-631BB104CB6A@.microsoft.com...
> Hi
> I'm new to C#, moving from ASP, and slightly confused so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user
control
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly
in
> the usercontrol, but I couldnt find a URL method/property to give me just
the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
Thanks guys!

Psychos post solved it, I guess you would say, the easy way! However,
Martin's solution looks interesting.. might try that one when I know a bit
more about C# (only got it Friday!).

Would there be any advantage with ICommonPage over the second method that
Psycho suggested?

Cheers guys

Dan

"Dan Nash" wrote:

> Hi
> I'm new to C#, moving from ASP, and slightly confused so bear with me!
> Basically I've got 4 pages, each of which runs the same user control (some
> header information). I want to be able to change the data in the user control
> based on the page (everything else would be duplicated, hence the user
> control).
> Anyway, i thought the easiest way would be to use a variable set in the
> Page_load event of the calling page. So I've got a variable defined in my
> page class as..
> public int pageID = 1;
> However, when I try to access that from the user control, it says the
> variable is not defined in the namespace. What am i missing!?!
> Alternatively, I could use a URL object to get the page were on directly in
> the usercontrol, but I couldnt find a URL method/property to give me just the
> file name.
> Hope all that makes sense!
> Cheers
>
> Dan
>

Passing Variables to Nested User Controls

Question: How do I pass variables to a nested user control ?

Description: I want to call a user control that calls another user control for encapsulation as shown below. I have a very hard time making the child control see the grandparent variable passed to it In the example shown below, Default.aspx calls the uc:Control, which in turn calls the uc:SubControl. I can get uc:Control to see Variable1="Value1", but I cannot get the uc:SubControl to see that value. What am I doing wrong. I use VB for the code behind and I have a public variable called Variable1 in both user controls.

Default.aspx:
<uc:Control id="Parent" Variable1="Value1" runat="server"></uc:ParentControl
ParentControl.ascx:
<uc:SubControl id="Child" Variable1="<%=Value1%>" runat="server"></uc:ChildControlYou can't use the <%= %> syntax to set an attribute of a server control.
Use <%# %> instead, and DataBind().

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.

passing XDocument in DayRenderEvent?

This is probably a very simple solution but I am stumped. I am trying to render events on an asp:calendar control using LINQ. I have code behind that loads up my XDocument during Pageload and calls a dayrender event. But I can't figure out how to get my XDocument into the event argument to be tested on during the "if ..." statements. Here is the code, the commented out section is what is supposed to be run if I could get 'thismonth' into the event handler. The yellow-coloring section was just for testing.

protected void Page_Load(object sender, EventArgs e)
{
XDocument eventsXML = XDocument.Load(Server.MapPath("Events.xml"));

var events = from theeventin eventsXML.Descendants("event")
selectnew
{
Organization = theevent.Element("organization").Value,
Creator = theevent.Element("creator").Value,
Name = theevent.Element("name").Value,
Description = theevent.Element("description").Value,
Location = theevent.Element("location").Value,
EventDate = DateTime.Parse(theevent.Element("Date").Element("Day").Value)
};

var thismonth = from itemin events
where item.EventDate.Month == DateTime.Now.Month
select item;

Calendar1.DayRender +=new DayRenderEventHandler(Calendar1_DayRender);
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth && !e.Day.IsWeekend) <-------- Just for testing.
e.Cell.BackColor = System.Drawing.Color.Yellow;

//foreach (var item in thismonth) <------ "thismonth does not exist in this context" how do I get it here?
//{
// if (item.EventDate.Date.Equals(e.Day.Date))
// e.Cell.BackColor = System.Drawing.Color.PaleVioletRed;
//}
}

Still no progress I've given up for now. When I move the entire data-access into the Calendar1_dayrender event it just "Object not an instance" errors out on "select new"

Weldon