VB ASP.NET 4.0 - Adding Example to new web project - asp.net

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.

Related

Why don't auto generated events for a "Web User Control" create the EventHandler arguments in the code behind for an aspx page

I am using
Visual Studio 2012
.NET Framework 4.5
Visual Basic 2012 (VB 11.0)
I have the web user control ascx page: MyControl.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="MyControl" %>
<asp:Button runat="server" ID="MyButton" OnClick="MyButton_Click" />
I have a web user control code behind: MyControl.ascx.vb
Public Class MyControl
Inherits System.Web.UI.UserControl
Public Event MyClick As EventHandler
Protected Sub MyButton_Click(sender as Object, e as EventArgs) Handles MyButton.Click
RaiseEvent MyClick(sender, e)
End Sub
End Class
Then I have a page which consumes the user control: MyPage.aspx
<%# Register Src="UserControls/MyControl.ascx" TagPrefix="ucMyControl" TagName="MyControl" %>
<div>
<ucMyControl:MyControl runat="server" ID="UcMyControl" OnMyClick="UcMyControl_MyClick" />
</div>
When I select <Create New Event> from the intellesence as shown in the picture below
The generated UcMyControl_MyClick does not contain the sender as Object, e as EventArgs parameters. See below:
Protected Sub UcMyControl_MyClick()
End Sub
What I expect is to have the EventHandler arguments created whith the event like this:
Protected Sub UcMyControl_MyClick(sender As Object, e As EventArgs)
End Sub
I am new to ASP.NET and Web User Controls. I have searched extensively for this issue to no avail. I have done the following in the MyControl.ascx.vb file
1.
Public Delegate Sub MyControlEventHandler(sender as Object, e as EventArgs)
Public Event MyClick as MyControlEventHandler
2.
Public Event MyClick as EventHandler(Of EventArgs)
3.
Public Event MyClick(ByVal sender As Object, ByVal e As EventArgs)
Related
ASP.NET event stub not created as expected in Visual Studio
Automatically create handler signature in Visual Studio

ASP.NET Webforms Repeater doesn't load custom control properly on button click/postback

I'd like to use a custom control within a repeater with access to the data item of the repeater from within the custom control. This works fine when loading the page, but when binding data to the repeater from a button click event it will not work.
Over the past fwe days I have tried so many different approaches, using ViewStates, Session, disabling ViewState, doing things with and without update panels but I always end at the same issue.
For some reason when binding data on the repeater in the button click event handler the "AssignedValue" property of the custom control will not be set, which works on page load without postback.
I am confused, as the repeater item is present but the custom control is loaded without it being assigned to "AssignedValue". How else would I bind a different data set to the repeater when clicking a button?
Any ideas how I can solve this issue?
Demo solution download: https://drive.google.com/file/d/10TvaCr0p6wPkQ6HoA0PnjwXgYgVJEKp8/view?usp=sharing or see the code below.
Default.aspx
<%# Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<%# Register TagPrefix="test" Src="~/WebUserControl1.ascx" TagName="ctrl" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="rptTest" ItemType="WebApplication1.Test">
<ItemTemplate>
<div>
<test:ctrl runat="server" AssignedValue="<%# Item.Value %>" /> (Item: <%# Item.Value %>)
</div>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:LinkButton runat="server" ID="btnLoadMore" OnClick="btnLoadMore_Click">Load More!</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Default.aspx.vb
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
rptTest.DataSource = Test.Set1
rptTest.DataBind()
End If
End Sub
Protected Sub btnLoadMore_Click(sender As Object, e As EventArgs)
rptTest.DataSource = Test.Set2
rptTest.DataBind()
End Sub
End Class
Test.vb
Public Class Test
Public Property Value As String
Public Shared Property Set1 As Test() = {New Test() With {.Value = "a"}, New Test() With {.Value = "b"}}
Public Shared Property Set2 As Test() = {New Test() With {.Value = "c"}, New Test() With {.Value = "d"}}
End Class
WebUserControl1.aspx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebApplication1.WebUserControl1" %>
Value: <asp:Literal runat="server" ID="litValue" />
WebUserControl.aspx.vb
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Public Property AssignedValue As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
litValue.Text = AssignedValue
End Sub
End Class
What I expect:
Value: a (Item: a)
Value: b (Item: b)
*Click on Load More!*
Value: c (Item: c)
Value: d (Item: d)
What I get:
Value: a (Item: a)
Value: b (Item: b)
*Click on Load More!*
Value: (Item: c)
Value: (Item: d)
Hi I have looked at your code and i think i know what the problem is.
in the WebUserControl1.ascx.vb you assign a value on Page_load so it works the first time but then on the binding the second time you don't assign a value so you need to add the assignment below WebUserControl1 prerender. Please let me know if it works. Fingers crossed.
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Public Property AssignedValue As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
litValue.Text = AssignedValue
End Sub
Private Sub WebUserControl1_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
litValue.Text = AssignedValue
End Sub
End Class
I'm not sure if this will help but I had a problem with repeaters before in UpdatePanels and was getting a pull page reload instead of a partial one so losing data.
The repeaters don't seem to handle client ids very well so it caused me problems.
I added ClientIDMode="AutoID" to the repeaters attributes which fixed the problem for me.
It may be worth a try. Somebody else may be able to explain this technically better thank me.

Process Bar With Process Completion Messages

I am Executing Multiple store procedures on Button click event of my .aspx page
On each Procedure Execution i have to show a message on Process baar that
process 1 is completed.
process 2 is completed.
process 3 is completed.
please help in this
One way is to use an Update Panel in conjunction with Ajax Timer.
The following article may be useful.
http://msdn.microsoft.com/en-in/library/cc295400.aspx
The example given in the article shows you how to update a label in the Update Panel with current server time on the timer event. You can easily adapt the code to your needs and show whatever message you want to show instead of server time.
EDIT
As requested here is some sample code.
Add a new aspx file to your application and name it Test2.aspx
Replace the contents of aspx file as given below.
Run the application and see the sample code working.
Customize the DoWorkAsync method to suit your needs.
Run application and test your modified code.
The ASPX file:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Test2.aspx.vb" Inherits="Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Asynchronous Update Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" ID="Timer1" Interval="1000" />
<asp:Button ID="Button1" runat="server" Text="Start" /><br />
<asp:Label runat="server" Text="Click Start button to being processing." ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Code behind file:
(pay attention to inline comments)
Partial Class Test2
Inherits System.Web.UI.Page
Private MyAsyncProcessor As AsyncProcessor
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("MyAsyncProcessor") IsNot Nothing Then
' we are still processing.. so extract the reference of our class back from session variable
Me.MyAsyncProcessor = Session("MyAsyncProcessor")
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' this event is fired everytime a timer ticks.
If MyAsyncProcessor IsNot Nothing Then
Label1.Text = MyAsyncProcessor.StatusMessage
If Not MyAsyncProcessor.IsProcessing Then
MyAsyncProcessor = Nothing
Session.Remove("MyAsyncProcessor")
Timer1.Enabled = False
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Button1.Enabled = False
Timer1.Enabled = True
MyAsyncProcessor = New AsyncProcessor
MyAsyncProcessor.Start()
Session("MyAsyncProcessor") = MyAsyncProcessor 'save reference in a session variable
End Sub
End Class
Public Class AsyncProcessor
Public IsProcessing As Boolean
Public StatusMessage As String
Public Sub Start()
Dim th As New Threading.Thread(AddressOf DoWorkAsync)
th.Start()
End Sub
Private Sub DoWorkAsync()
' *** Replace this code with whatever work you want to do ***
' Set IsProcessing=True at beginnning of task and set it to False when exitting.
' Update the StatusMessage at regular intervals.
IsProcessing = True
For i = 1 To 10
'' this sleep is only to simulate a long going task
Threading.Thread.Sleep(1000)
StatusMessage &= String.Format("Task #{0} completed... <br>", i)
Next
StatusMessage &= "All tasks completed."
IsProcessing = False
End Sub
End Class
Another way is to delegate the work to a page in an iframe. On the iframe page, as your code is processed, you can update a Session variable to contain 'Finished Processing first step' (or whatever) and use PageMethods on the parent page to call a Web Method that gets the value of the session being updated by the code on the page in the iframe.

Adding .net RadioButtonsList control dynamically in code file, no SelectedValue in Post Back

I am having trouble getting the SelectedValue from a RadionButtonsList that I'm creating dynmically in a code file. I'm adding it to a PlaceHolder control on the aspx page. After PostBack the radio button keeps its selection on the page, but I am not able to get the value from within the code file.
I understand I can create the RadioButtonsList in the HTML page, but I need to understand how to accomplish this from only the code file. The real problem I'm having is more complicated. (Creating dynamic number of true/false RadioButtonsList controls)
quiz.aspx
<%# Page Title="Quiz" MasterPageFile="~/MasterPage.master" Language="VB" CodeFile="quiz.aspx.vb" Inherits="quiz" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:PlaceHolder ID="testholder" runat="server"></asp:PlaceHolder>
<asp:Button ID="lblNext" runat="server" type="submit" Text="Next" accesskey="n"/>
</asp:Content>
MasterPage.master
<%# Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
quiz.aspx.vp
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim testlist As New RadioButtonList
testlist.Items.Add(New ListItem("test 1", "test1"))
testlist.Items.Add(New ListItem("test 2", "test2"))
testholder.Controls.Add(testlist)
If (IsPostBack) Then
Debug.WriteLine("testlist: " & testlist.SelectedValue)
End If
End Sub
My output after submit is: "testlist: ". Any ideas?
You have to load the controls before page_load event is triggered so that loadpostbackdata will get a chance to load the viewstate of the dynamic controls.
I am able to see the selected value after moving the dynamic controls to page_init event.
Dim testlist As New RadioButtonList
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
{
testlist.Items.Add(new ListItem("test1", "test1"))
testlist.Items.Add(new ListItem("test2", "test2"))
testholder.Controls.Add(testlist)
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
{
If (IsPostBack) Then
Debug.WriteLine(testlist.SelectedValue)
End If
}

How do you expose a Panel as a property in a User Control?

I have a user control with a panel on it:
CustomControl.ascx
<%# Control Language="vb" CodeBehind="CustomControl.ascx.vb" %>
<asp:Panel ID="myPanel" runat="server"></asp:Panel>
I'd lke to expose that panel as a property that I can use at design time.
CustomControl.ascx.vb
<ParseChildren(True), PersistChildren(False)> _
Partial Public Class CustomControl
Inherits System.Web.UI.UserControl
#Region "Members and Properties"
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property SomePanel() As Panel
Get
Return myPanel
End Get
Set(ByVal value As Panel)
myPanel= value
End Set
End Property
#End Region
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(myPanel.HasControls.ToString())
End Sub
End Class
And then bring the control onto a page like so:
<ucl:CustomControl runat="server">
<SomePanel>
<asp:Label ID="lblText" AssociatedControlID="txtBox" runat="server"></asp:Label>
<asp:TextBox id="txtBox" runat="server"></asp:TextBox>
</SomePanel>
</ucl:CustomControl>
When I run the page, HasControls is true but nothing is rendered. What am I doing wrong?
You could achieve a similar effect by designing your user control as a template control. It's not that hard to set up, if you choose to go the template route.
Check out this brief tutorial to determine if control templates are suitable for you:
http://msdn.microsoft.com/en-us/library/36574bf6.aspx

Resources