I have masterpage.master.vb where I have properties, such as;
Private _SQLerror As String
Public Property SQLerror() As String
Get
Return _SQLerror
End Get
Set(ByVal value As String)
_SQLerror = String.Empty
End Set
End Property
Then I have an aspx page in which I need to use this property in, such as;
If **[masterpage property sqlerror]** = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
Can anyone give me an idea how to go about this? I've tried searching but most articles talk in the context of web controls...
Thanks.
Here you go:
How to: Reference ASP.NET Master Page Content
From the article, it looks like
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
should work for you.
Just be sure to add the MasterType directive as described or you might get a type conversion error. (Or you could use a variable of your master page type instead of Master, as daRoBBie suggests in his answer.)
I have created a test Web site just to test this out, and it works. Here is the full source of the site:
Site1.Master:
<%# Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebApplication1.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is the Master Page content.
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Site1.Master.vb:
Public Partial Class Site1
Inherits System.Web.UI.MasterPage
Private _SQLerror As String
Public Property SQLerror() As String
Get
Return _SQLerror
End Get
Set(ByVal value As String)
_SQLerror = String.Empty
End Set
End Property
End Class
WebForm1.aspx:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master"
CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%# MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
This is the Content Page content.
<asp:Label ID="InternalSQLErrLabel" runat="server" Text="Label"></asp:Label>
</asp:Content>
WebForm1.aspx.vb:
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
End Sub
End Class
you can cast the masterpage to the correct type:
MyMasterPageType m = (MyMasterPageType)Master;
Then you can access your properties:
m.SqlError
If you have multiple master pages, let all your masterpages inherit from an interface, and cast the masterpage to that interface.
You can use <%# MasterType %> also for this.
If you have followed the steps in Andy West's answer and have one or many compile errors reading: Foo is not a member of 'System.Web.UI.MasterPage', make sure that they are the only compile error in your list. If there are any other compile errors that need to be fixed, they should be fixed before you continue troubleshooting your MasterPage.
These other compile errors may be preventing the compiler from parsing your MasterPage and detecting the extra properties. Once they are resolved, do a full recompile.
Related
This is a simple example of what is happening here.
Default.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% Response.Write(sName) %>
</div>
</form>
</body>
</html>
and the code behind default.aspx.vb
Public Class _default1
Inherits System.Web.UI.Page
Public sName As String = "Jimmy"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
the error is
BC30451 'sName' is not declared. It may be inaccessible due to its protection level. base C:\Users\Jimmy\Documents\Visual Studio 2015\Projects\base\base\teste\default.aspx 12
Where is the problem ?
The biggest issue is your class _default1 is not marked as partial class.
It should rather be
Public partial Class _default1
Inherits System.Web.UI.Page
One more point is that your page directive has the property AutoEventWireup="false" and with that the way you have your page events mapped right now will not work. So set it to true rather AutoEventWireup="true"
Your #Page directive is total weird as can be seen below.
<%# Page AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
To summarize; below are the issues you should take care off
mark the class partial
set AutoEventWireup="true"
change CodeBehind property to CodeBehind="default1.aspx.vb"
Change your Inherits property to Inherits="your_namespace._default1"
I have a project and the strangest thing is happening. I have been trying to figure it out for multiple hours. The issue is if I create a new web form, the code behind will see all the controls, but from the .aspx page I can use any public functions from the code behind. For example if I create the following function:
Public Function Hey()
Return True
End Function
Then I go to the .aspx source and put the following:
<%# hey%>
I get the following error: Error 2 'hey' is not declared. It may be inaccessible due to its protection level.
The following is WebForm5.aspx:
<%# Page Title="Web Form" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm5.aspx.vb" Inherits="InventoryManagement.WebForm5" Async="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%# hey%>
</div>
</form>
</body>
</html>
WebForm5.aspx.vb:
Public Class WebForm5
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function Hey()
Return True
End Function
End Class
I have "older" pages that I can call the exact same function from code behind. Any help would be greatly appreciated as this issue has been a problem for the last 3+ hours. Thanks in advance!!
The solution to this issue was to clean and rebuild project. This solved the issue right away!
I try to create custom control, loaded dynamically from .vb code.
Here is my custom control "ControlCar" in file "controlcar.ascx"
<%# Control Language="VB" ClassName="ControlCar" %>
<script runat="server">
Private m_car As Car = Nothing
Public Property Car() As Car
Get
Car= m_car
End Get
Set(ByVal value As Car)
m_car = value
End Set
End Property
Protected Sub Panel_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.m_car Is Nothing Then
lit_color.Text = "(m_car Is Nothing)"
Else
lit_color.Text = "color of Me.m_car is (" & Me.m_car.Color & ")"
End If
End Sub
</script>
<asp:Panel ID="panel" OnLoad="Panel_OnLoad" runat="server">
this is a car<br />
color = <asp:Literal ID="lit_color" runat="server"></asp:Literal><br />
<br />
</asp:Panel>
Here is my ASP web page in file "cars.aspx" which use .vb code for events, ...
<%# Page Language="vb" Explicit="true" Inherits="PageBase" Src="/code/cars.vb"%>
<html>
<body>
<!-- Html code here --->
<asp:panel ID="panel_cars" runat="server">
</asp:panel>
</body>
And here is my .vb code in file "cars.vb"
Private Sub CreateCar()
Dim car As Car = new Car()
Dim control As ControlCar = Nothing
control= CType(LoadControl("/code/controlcar.ascx"), ControlCar)
control.Car = car
panel_cars.Controls.Add(control)
End Sub
But it fails, saying 'ControlCar' is unrecognized in cars.vb.
I know it works, if move .vb code in .aspx file and using directive
<%# Register TagPrefix="uc" TagName="ControlCar" Src="/code/controlcar.ascx" %>
But I need to separate .vb code and .aspx code like in my example.
How can I make recognizing 'ControlCar' type (defined in .ascx) in .vb file?
You need to add some kind of reference. You can use a reference directive:
<%# Reference Control="controlcar.ascx" %>.
The control is being dynamically compiled. If you have no reference, the compiler has no idea what you are trying to use.
EDIT:
You may be able to use:
Imports ASP.controlcar_ascx
If the reference must be in the code behind. I have had issues with this in the past though.
Suppose i have two text fields in one page, one for name and another one for age.
When i click the submit button those values should appear in another page. Can any one give the example for that one.. i am totally confused.
please help me
Thank you
MSDN has a page on this, How to: Pass Values Between ASP.NET Web Pages:
The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web page:
Use a query string.
Get HTTP POST information from the
source page.
The following options are available only when the source and target pages are in the same ASP.NET Web
application.
Use session state.
Create public properties in the source page and access the property values in the target page.
Get control information in the target page from controls in the source page.
For your scenario, it sounds like using POST is the way to go, since you have textboxes on the first page. Example:
First page:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page 1</title>
</head>
<body>
<form id="form1" runat="server" action="WebForm2.aspx">
<div>
Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox><br />
Age: <asp:TextBox ID="tbAge" runat="server"></asp:TextBox><br />
<asp:Button ID="submit" runat="server" Text="Go!" />
</div>
</form>
</body>
</html>
Notice the action="WebForm2.aspx" which directs the POST to the second page. There's no code-behind.
Page 2 (receiving page):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication2.WebForm2" EnableViewStateMac="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page 2</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="litText" runat="server"></asp:Literal>
</form>
</body>
</html>
Notice the EnableViewStateMac="false" attribute on the Page element. It's important.
The code-behind, grabbing the values using a simple Request.Form():
Public Class WebForm2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
litText.Text = Request.Form("tbName") & ": " & Request.Form("tbAge")
End Sub
End Class
That should work... :)
Put this code to your submit button event handler,
private void btnSubmit_Click(object
sender, System.EventArgs e) {
Response.Redirect("AnotherPage.aspx?Name="
+ this.txtName.Text + "&Age=" + this.txtAge.Text); }
Put this code to second page page_load,
private void Page_Load(object sender,
System.EventArgs e) {
this.txtBox1.Text =
Request.QueryString["Name"];
this.txtBox2.Text =
Request.QueryString["Age"]; }
You have a couple of options.
**
1. Use Query string.
(Cons)
- Text might be too lengthy
- You might have to encrypt/decrypt query string based on your requirement
(Pros)
- Easy to manage
2. Use Session
(Cons)
- May increase processing load on server
- You have to check and clear the session if there are too many transactions
(Pros)
- Values from different pages, methods can be stored once in the session and retrieved from when needed
I am trying to learn asp.net (vb.net) and I'm having some trouble. I want to change a pages content based on the querystring.
In classic asp I would do:
<% If request.querystring("page") = 1 THEN %>
-entire page-
<% Else %>
-different page-
<% End If %>
The closest I could get in .net is
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
If Request.QueryString("page") = 1 Then
lblMessage1.Text = "message"
Else
lblMessage1.Text = "message2"
End If
End Sub
That only seems good for small things. What would be the best method to change an entire page?
You could do the following (simple redirect):
If Request.QueryString("page") = 1 Then
Response.Redirect("MyPage1.aspx")
Else
Response.Redirect("MyPage2.aspx")
End If
You could also do this (read more here):
If Request.QueryString("page") = 1 Then
Server.Transfer("MyPage1.aspx")
Else
Server.Transfer("MyPage2.aspx")
End If
And finally one more option (show/hide different panels on the page):
If Request.QueryString("page") = 1 Then
MyPanel1.Visible = true
MyPanel2.Visible = false
Else
MyPanel1.Visible = false
MyPanel2.Visible = true
End If
I would suggest using the MultiView control.
In a nutshell, you would create two multiview "Views", each with the html that you would want to show. Then you could look at the querystring parameter and switch the active view of the multiview accordingly.
This has a lot of advantages to Response.Redirect() like others suggested. First off, that would always generate at least two browser requests. Also, Response.Redirect() throws a ThreadAborted exception behind the scenes, which can confuse people diagnosing the application.
Example MultiView control:
ASPX:
<form id="form1" runat="server">
<div>
<asp:MultiView ID="MultiView1" runat="server">
<asp:View runat="server">
Hi, this is Page 1
</asp:View>
<asp:View runat="server">
Hi, this is Page 2
</asp:View>
</asp:MultiView>
</div>
</form>
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Request.QueryString("page") = "1" Then
MultiView1.ActiveViewIndex = 0
Else
MultiView1.ActiveViewIndex = 1
End If
End Sub
You really have a few options, you could:
Response.Redirect(url) to a different page based on the input.
You could have an ASP:Panel with the "visible" property set to false and toggle that value based on the input.
Why not use different files instead? redirect to different pages. That would avoid having to have if statements everywhere.
OR
put your data in panels and just hide one or the other panel1.visible = (true/false). That's the best thing to do if you have to have it all in the same aspx page.
I prefer doing it on the ASPX page using DataBinding:
<asp:PlaceHolder runat="server" ID="Messages">
<asp:Label runat="server" Visible=<%# Request.QueryString("page") = 1 %> Text="Message 1" />
<asp:Label runat="server" Visible=<%# Request.QueryString("page") <> 1 %> Text="Message 2"/>
</asp:PlaceHolder>
Then on the server side:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Messages.DataBind()
End Sub
For future reference, you can still use the classic ASP way to control content. Here's an ASPX page I wrote just now:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
if (3 == 9)
{%>
<span>Hello</span>
<%
}
else
{
%> <span>What?</span > <%
}
%>
</div>
</form>
</body>
</html>
When the page renders, it displays 'What?' on the page.
However, I would say that this is bad practise and poor design! Use either womp's suggestion of a multiview, or a page redirect.