Showing posts with label header. Show all posts
Showing posts with label header. 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

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

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 !

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.