How do AppState As WebTemplates.State works (ASP .Net)? - asp.net

The AppState values can be changed/stored through page requests, when (Page_init ?) and how are they changed?
//On some other page
Response.Redirect("mypage.aspx?ID=10")
//On Page_Load of myPage.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim id = AppSate("ID").Value //id will equal 10
...
end sub

Related

How do I change values of hex in file and save again

How do I change the last 6 values of hex in file and save again using vb or c or c++ or java
Imports System.IO
Public Class Form1
Dim hexString As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles open_button.Click
If open_openfiledialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Using file As New IO.FileStream(open_openfiledialog.FileName, IO.FileMode.Open)
Dim value As Integer = file.ReadByte()
Do Until value = -1
hexString = hexString & (value.ToString("X2"))
value = file.ReadByte()
Loop
End Using
hex_richtextbox.Text = hexString
End If
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles open_openfiledialog.FileOk
End Sub
Private Sub save_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_button.Click
IO.File.WriteAllBytes("C:myfile.ax2", hex_richtextbox.Text.Split(" "c).Select(Function(s) Convert.ToByte(s, 16)).ToArray())
End Sub
End Class

How to fix the error "Object reference not set to an instance of an object"?

In this program (VB, ASP.NET 2010) I create three fields: accno, name and balance, and the following buttons: create, destroy, set and get.
But while clicking on set or get method it gives the following exception: object reference not set to an instance of an object
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Dim obj As account 'declaring the obj of class account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
obj = New account 'initializing the object obj on class accounts
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class
Account.vb
Imports Microsoft.VisualBasic
Public Class account
Private accno As Integer
Private acc_name As String
Private bal As Integer
'constructor
Public Sub New()
MsgBox("object created")
End Sub
'public method to populate above three private variable
Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
Me.accno = a
Me.acc_name = b
Me.bal = c
End Sub
Public Sub getdata()
MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
End Sub
'destructor
Protected Overrides Sub finalize()
MsgBox("object destroyed")
End Sub
End Class
You have use New Keyword for Initializing Objects...
Dim obj As NEW account
Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As NEW account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class
Please mark it as answer if it's working.
use below code. You need to initialize in page load method and need to check is postback property.
Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As account
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
if not ispostback then
obj = New account
end
End Sub
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class

Asp.net losing values of variables

I am trying to write my first WebApplication in ASP.NET. Here is my code:
Public Class WebForm2
Inherits System.Web.UI.Page
Public n As Integer
Public zetony As Integer
Public liczba As Boolean
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Function TextBox1_Validate(Cancel As Boolean)
If Not IsNumeric(TextBox1.Text) Then
MsgBox("Prosze podaj liczbe dobry uzytkowniku :)", vbInformation)
Cancel = True
Else : Cancel = False
End If
Return Cancel
End Function
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
liczba = TextBox1_Validate(liczba)
If (liczba = False) Then
n = Convert.ToInt32(TextBox1.Text)
Label2.Text = n
End If
End Sub
Protected Sub graj()
Label2.Text = n
End Sub
Protected Sub Image1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click
If zetony < 2 Then
n -= 1
ImageButton1.ImageUrl = "red_coin.gif"
zetony += 1
End If
End Sub
Protected Sub Image2_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton2.Click
If zetony < 2 Then
n -= 1
ImageButton2.ImageUrl = "red_coin.gif"
zetony += 1
End If
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
graj()
End Sub
End Class
My problem is that, only on Button1_Click I got proper value. When I try to call Sub graj() value of n is alwayes 0.
HTTP is stateless. This means that for every request a new instance of the class WebForm2 gets created. So if you set the value of n inside Button1_Click it will not be preserverd when you access it from Button2_Click since it's a different instance.
To save your data across requests there are several possibilities, to name a few:
Save it in a database
Save it in the Application-object (this is shared across all users:
// setting the value
HttpContext.Current.Application("n") = "somevalue";
// Getting the value
string test = HttpContext.Current.Application("n");
Save it in the session-state (this is shared across all requests for one user):
// setting the value
HttpContext.Current.Session("n") = "somevalue";
// Getting the value
string test = HttpContext.Current.Session("n");

RiseEvent from usercontrol or communicate between usercontrols

I have problem with bubbleup event from my dynamic loaded ascx control (katalogbooklist.ascx) to it´s parent control (ViewAJBarnboksKatalog.ascx)
i need to fire/run sub uppdateraAndraModuler in the parent control when the event addMultiVotes_command fires in the chiled control.
is there any one who knows or have an idea on how to do this?
/
Andreas
(the code lives in a dotnetnuke cms modules, if that's any help)
Partial Class ViewAJBarnboksKatalog '<--------Partial codebehind file for ViewAJBarnboksKatalog.ascx
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
.. code for adding for loading katalogbooklist.ascx dynamic...
Me.Controls.Add(..code.. add katalogbooklist.ascx ..) '
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Execkoden = New Execkoden()
AddHandler t.onAjeventtest, AddressOf Uppdateramod
End Sub
Public Sub Uppdateramod(sender As Object, e As ajeventArgs)
uppdateraAndraModuler()
End Sub
Public Sub uppdateraAndraModuler()
'..do some code
End Sub
End Class
Partial Class katalogenBookList '<--------Partial codebehind file for katalogbooklist.ascx
Protected Sub addMultiVotes_command(ByVal sender As Object, ByVal e As System.EventArgs)
'..more code...
Dim te As New Execkoden ' <----- i want to use the constructor to raise the event in class Execkoden can´t raiseevent directly it wont´t fire
'... more code...
End sub
End Class
Public Class ajeventArgs : Inherits EventArgs
Public Sub New()
End Sub
End Class
Public Delegate Sub Uppdatera(sender As Object, e As ajeventArgs)
Public Class Execkoden
Public Event onAjeventtest As Uppdatera
Public Sub New()
RaiseEvent onAjeventtest(Me, New ajeventArgs)
End Sub
End Class
Create an event handler in the child control, like this:
public event EventHandler DeleteButtonClick;
When a button is clicked in the child control, do this:
protected void DeleteClick(object sender, EventArgs e)
{
if (this.DeleteButtonClick != null)
this.DeleteButtonClick(sender, e);
}
And in your parent control, in the markup:
<UC:SomeUserControl ID="UserControl1" runat="server" OnDeleteButtonClick="UserControl1_DeleteClick" ...>
And in the code behind of the parent control:
protected void UserControl1_DeleteClick(object sender, EventArgs e)
{
//do something
}
I would suggest using the IMC or Inter Module Communication feature that's built in to DotNetNuke.

Page.Load issue in ASP.NET 2.0

I am trying to aid another programmer with a page called Default.aspx with a code-behind section, and unfortunately I am at a bit of a loss.
Partial Class _Default
Inherits OverheadClass
'A bunch of global variables here'
Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load
'Function goes here'
And in the OverheadClass we have
Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e as System.EventArgs) Handles MyClass.Load
The desired effect is when the OverheadClass is inherited, we want its load to run before the load event on the page runs. There is probably a very simple answer to this that I am missing.
Edit: I forgot to note that we write in VB, and not C# as many of you are used to for ASP.
You should be able to override the OnLoad and call the base class's OnLoad first, then your class, for example:
C# Version
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Do some stuff here
}
VB Version
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
' Do some stuff here
End Sub
In VB it would be:
Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load
Mybase.Sub_OverheadClass_Load(e)
End Sub
Your default page should inherit OverheadClass
Partial Public Class _Default
Inherits OverheadClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Do some page stuff'
End Sub
End Class
And OverheadClass should inherit System.Web.UI.Page
Public Class OverheadClass
Inherits System.Web.UI.Page
Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyClass.Load
'Do some base stuff'
End Sub
End Class
Partial Class OverheadClass
Inherits System.Web.UI.Page
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
End Class
Partial Class _Default
Inherits OverheadClass
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
End Class

Resources