I am trying to design a form using VB.Net for WebForms. I have been following the following link My label's text isn't changing on page_load asp.net. But the solution there is not working for me. I already have Handles Me.Load defined for my Page_Load event handler, however the result is a "blank" page, with the HTML output below.
form.aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="form.aspx.vb" Inherits="MyApp.form" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<asp:Label id="vcode" runat="server" />
</body>
</html>
form.aspx.vb:
Imports System.Web
Public Class form
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vcode.Text = "Why not?"
End Sub
End Class
HTML Output:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Test
</title></head>
<body>
<span id="vcode"></span>
</body>
</html>
I suspect it has to do with the Inherits attribute, but if I take out Page_load from that class definition, I get an error:
Statement is not valid in a namespace
I also tried setting the AutoEventWireup attribute on the page to true, but no change.. still blank label.
What am I missing here?
Try adding a namespace like so:
Namespace MyApp
Public Class form Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vcode.Text = "Why not?"
End Sub
End Class
End Namespace
Related
Error in CallbackUpdateSchema.Callback
BC30506 Visual Basic AND ASP.net Handles clause requires a WithEvents variable defined in the containing type or one of its base types. Callback
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo.DB
Public Class UpdateSchema
Inherits System.Web.UI.Page
Dim uow As UnitOfWork
Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
uow = XpoHelper.GetNewUnitOfWork
End Sub
Protected Sub CallbackUpdateSchema_Callback(source As Object, e As DevExpress.Web.CallbackEventArgs) Handles CallbackUpdateSchema.Callback
uow.UpdateSchema()
uow.CreateObjectTypeRecords()
End Sub
End Class
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="UpdateSchema.aspx.vb" %>
<%# Register assembly="DevExpress.Xpo.v18.2, Version=18.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Xpo" tagprefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxButton ID="ASPxButtonUpdateSchema" runat="server" AutoPostBack="False" Text="Update Schema">
<ClientSideEvents Click="function(s, e) {CallbackUpdateSchema.PerformCallback();}" />
</dx:ASPxButton>
<dx:ASPxCallback ID="CallbackUpdateSchema" runat="server" ClientInstanceName="CallbackUpdateSchema">
</dx:ASPxCallback>
</div>
</form>
</body>
</html>
Use the overridable method OnInit
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.oninit?view=netframework-4.8#System_Web_UI_Page_OnInit_System_EventArgs_
Events are meant for users. For sub-classing, you should use overridable methods instead.
Protected Overrides Sub OnInit(e As EventArgs)
MyBase.OnInit()
uow = XpoHelper.GetNewUnitOfWork
End Sub
The problem I'm having is: when a node is clicked, the selected node index is always -1 and the selected node text is always the text of the first node.
As you can see in the code below, I've created a test-app. This test-app shows the same problem
This is my Default.aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Treeviewer._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="tvDir" OnSelectedNodeChanged="tvDir_SelectedNodeChanged" OnTreeNodeExpanded="tvDir_TreeNodeExpanded" runat="server"></asp:TreeView>
</div>
</form>
</body>
</html>
And this is the code-behind:
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tvDir.Nodes.Add(New TreeNode("C:\"))
For Each aDir As String In IO.Directory.EnumerateDirectories("C:\")
Dim aNode As TreeNode = New TreeNode(aDir, 0)
If hasSubDirectories(aDir) Then
aNode.ChildNodes.Add(New TreeNode("", 1))
End If
aNode.SelectAction = TreeNodeSelectAction.Select
tvDir.Nodes(0).ChildNodes.Add(aNode)
Next
tvDir.CollapseAll()
End Sub
Protected Sub tvDir_SelectedNodeChanged(sender As Object, e As EventArgs)
Dim tvIndex = tvDir.Nodes.IndexOf(tvDir.SelectedNode)
Debug.Write(tvIndex & " " & tvDir.SelectedNode.Text)
End Sub
Protected Sub tvDir_TreeNodeExpanded(sender As Object, e As TreeNodeEventArgs)
End Sub
Private Function hasSubDirectories(inDirectory As String) As Boolean
Try
If IO.Directory.Exists(inDirectory) Then
Return IO.Directory.EnumerateDirectories(inDirectory).Count > 0
End If
Catch ex As Exception
'
End Try
Return False
End Function
End Class
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 am using Visual Studio 2010, VB.NET, target framework .NET 4.0.
I have a GridView which I am binding to some object collection, with a CommandField column that should allow edit of the selected row.
I am setting the EditIndex property correctly in the RowEditing eventhandler.
The problem is: When I click the "Edit" link that is generated, nothing apparently happens, the row gets rendered again in "view mode", not "edit mode".
But if I do some random postback, like clicking my "doNothing" button, the row gets rendered in "edit mode" in the next postback.
I have managed to reproduce the problem in the following code:
ASPX:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="MyForm.aspx.vb" Inherits="MySandBox.MyForm" %>
<%# Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>
<!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">
<asp:GridView ID="gvInfrator" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField HeaderText="MyField" DataField="MyField" />
</Columns>
</asp:GridView>
<asp:Button ID="btnDoNothing" runat="server" Text="Just do a postback" />
</form>
</body>
</html>
Code Behind:
Public Class MyDto
Public Property MyField As String
End Class
Public Class MyForm
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.BindMyData()
End Sub
Private Sub BindMyData()
Dim myData As New MyDto
myData.MyField = "My field value"
Me.gvInfrator.DataSource = New MyDto() {myData}
Me.gvInfrator.DataBind()
End Sub
Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
Me.BindMyData()
Me.gvInfrator.EditIndex = e.NewEditIndex
End Sub
End Class
Am I doing something wrong? Is this a bug in .NET Framework 4? Is there any workaround?
You need to bind your data after you set the EditIndex of your GridView.
Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
Me.gvInfrator.EditIndex = e.NewEditIndex
Me.BindMyData()
End Sub
I'm trying to learn ASP.NET (after many years of using classic ASP, jQuery, Ajax etc). I've installed VS 2010 and have IIS running on my W7 64bit PC.
I've created a new web project (in a folder called C:\ASP.NET Testing\ASP.NET 4.0 Examples) called ASP.NET-4.0.
I can compile the project fine and run the default page etc.
I am trying to load in a simple example from a book (ASP.NET 4.0 In Practice). So, I've created a sub folder CH01, and then copied in the .aspx and .aspx.vb files.
When I debug this, I get
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'ASP.NET_4._0.Global_asax'.
Source Error:
Line 1: <%# Application Codebehind="Global.asax.vb" Inherits="ASP.NET_4._0.Global_asax" Language="vb" %>
in the browser window.
The sample code (which has been downloaded from the website) is :
<%# Page Language="VB" AutoEventWireup="false" CodeFile="1-4.aspx.vb" Inherits="_1_4" %>
<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:literal id="ResponseText" runat="server" />
<br />
Enter your name:
<asp:textbox runat="server" ID="Name" />
<br />
<asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
</div>
</form>
</body>
</html>
and
Partial Class _1_4
Inherits System.Web.UI.Page
Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)
ResponseText.Text = "Your name is: " & Name.Text
End Sub
End Class
In VS, I also get an error message highlighting the ResponseText.Text = "Your name is: " & Name.Text
'ResponseText' is not declared. It may be inaccessible due to its protection level.
Global.asxa file
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub
End Class
I'm obviously missing something really simple, but I don't get it. I can't see any instructions as to anything which I need to set within VS.
Thanks.
This page works OK if I add from VS. In effect the same page as I'm copying in from the examples, but with slight changes to the page tag. It alos has a .designer.vb page which was automatically generated by VS.
14.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="14.aspx.vb" Inherits="ASP.NET_4._0._14" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:literal id="ResponseText" runat="server" />
<br />
Enter your name:
<asp:textbox runat="server" ID="Name" />
<br />
<asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
</div>
</form>
</body>
</html>
14.aspx.vb
Public Class _14
Inherits System.Web.UI.Page
Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)
ResponseText.Text = "Your name is: " & Name.Text
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
14.aspx.designer.vb
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _14
'''<summary>
'''Form1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''<summary>
'''ResponseText control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ResponseText As Global.System.Web.UI.WebControls.Literal
'''<summary>
'''Name control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Name As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''ClickButton control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ClickButton As Global.System.Web.UI.WebControls.Button
End Class
Check you global.asax on the root of your website. There seems to be something wrong with the class name/namespace. I don't think it has anything to do with the file you added, but rather with something you probably moved or removed by mistake.