Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Thursday, March 29, 2012

passing values from main webform to include template pages

View a printable version of this message!I'm new to ASP.Net... I have a web page (myform.aspx) that is put together using 3 include files, header.cscx, left.cscx, & footer.cscx (each has their own .cscx.vb files). I want to assign some custom values on myform.aspx.vb, how can I get these custom values in my one of my includes files, ie. header.cscx.vb?
'**********This is myform.aspx*************
<%@dotnet.itags.org. Register TagPrefix="UC" TagName="footer" src="http://pics.10026.com/?src=footer.ascx"%>
<%@dotnet.itags.org. Register TagPrefix="UC" TagName="left" src="http://pics.10026.com/?src=left.ascx"%>
<%@dotnet.itags.org. Register TagPrefix="UC" TagName="header" src="http://pics.10026.com/?src=header.ascx"%>
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false" Codebehind="myform.aspx.vb" Inherits="intranet.myform"%>
<UC:header id="header" runat="server" />
<UC:left id="left" runat="server" />
<TABLE><TR><td>My contents go here...</td></TR></TABLE>
<UC:footer id="footer" runat="server" />

'**********This is myform.aspx.vb*************
Public Class myform
Inherits System.Web.UI.Page
Protected WithEvents lblPageTitle As System.Web.UI.WebControls.Label
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblPageTitle.Text = "Welcome to my home page!"
End Sub
End Class

'**********This is header.ascx*************
<%@dotnet.itags.org. Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" Inherits="intranet.header" %>
'I don't have any HTML code here yet (all are in header.acsx.vb) b/c I'm still working on the sepration between HTML design and codebehind page.

'**********This is header.ascx.vb*************
Public Class header
Inherits System.Web.UI.UserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'How can I get the value of lblPageTitle here so I can display it in the HTML Title tag??
'I have other values that I want to pass to this page too, but for this example, I'm just trying to get the value of lblPageTitle from myform.aspx.vb
Response.Write("<HTML><HEAD>")
Response.Write("<title>lblPageTitle</title>")
Response.Write("</HEAD><BODY>")
...
End Sub
End Class1) Cookie
2) Session Variable
3) Query String
I think any of these will work for what you're describing. You'retrying to get som value to be accessible to your user controls from themain page.

Have you thought of using properties inside the usercontrols?
check this please:
An Extensive Examination of User Controls
The best reference to UserControls
Regards
Thank you so much, it's exactly what I need!
That is great, Im happy for you.
regards

Saturday, March 24, 2012

Passing variables between files

Hey All :)

Im new to asp.net, but come from a background of php so all my asp is in C#.

Now i have a ASP.NET cms system that i am currently making changes to. Now what i have is filename.aspx which up the top calls header.aspx, and i am trying to pass a string from filename.aspx.cs to header.aspx.cs (the name of the header graphic i want to set).


In php i would just set the variable, and in the header file i would call the variable in via global $varName; but in asp this doesnt seem to be working (go figure :p)

Hope it makes sense, all feedback appreciated.

1. you can pass it through the QueryString -

perhaps while you redirect to header.aspx do something like response.redirect("header.aspx?imgfile=" + theVariableHoldingName);
and in the header.aspx.cs do something like (in the Page_Load): string nameOfImage = Rquest.QueryString("imgfile");

2. you can use Session also:

in filename.aspx - Session.Add("imgfile",theVariableHoldingName);
and in header.aspx - string nameOfImage = Session["imgfile"];


ben14:

In php i would just set the variable,

just set the session object where you would set the variable as.. Session["myData"] = value

ben14:

and in the header file i would call the variable in via global $varName

and get the value of that session object where you would call the variable in via global $varName as... myValue = Session["myData"]

dont forget to cast the value... as Session returns an Object..

hope it helps./.


thanks for that :) should work fine.

cheers

Passing Variables between script files and aspx files

Hello all,

I have a aspx.vb files which some global declarations within,


<FONT color=#0000ff size=2>Partial</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Class</FONT><FONT size=2> _Default
</FONT><FONT color=#0000ff size=2>Inherits</FONT><FONT size=2> System.Web.UI.Page</FONT><FONT size=2> <P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> TheGood </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0
</FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> TheBad </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0
</FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> TheWarning </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0

...

How can I access these from a label within the .aspx file?


<FONT color=#0000ff size=2><</FONT><FONT color=#a31515 size=2>asp</FONT><FONT color=#0000ff size=2>:</FONT><FONT color=#a31515 size=2>Label</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>ID</FONT><FONT color=#0000ff size=2>="lbStatus"</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>runat</FONT><FONT color=#0000ff size=2>="server"</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>Text</FONT><FONT color=#0000ff size=2>="TheWarning"</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>Width</FONT><FONT color=#0000ff size=2>="300px"></</FONT><FONT color=#a31515 size=2>asp</FONT><FONT color=#0000ff size=2>:</FONT><FONT color=#a31515 size=2>Label</FONT><FONT color=#0000ff size=2>>

Dead6re:

Hello all,

I have a aspx.vb files which some global declarations within,


PartialClass _DefaultInherits System.Web.UI.Page

Dim TheGoodAsInteger = 0
Dim TheBadAsInteger = 0
Dim TheWarningAsInteger = 0...

How can I access these from a label within the .aspx file?

<asp:LabelID="lbStatus"runat="server"Text="TheWarning"Width="300px"></asp:Label>

lbStatus.Text = TheWarning.ToString()


1PartialClass _DefaultInherits System.Web.UI.Page

2Dim TheGoodAs Integer = 0Dim TheBadAs Integer = 0Dim TheWarningAs Integer = 0

1<asp:Label ID="lbStatus" runat="server" Text="TheWarning" Width="300px"></asp:Label>

i am not getting in which scenario it would be required...

can you tell me what is your exact requirement ?

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

Wednesday, March 21, 2012

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 !