How to assign theme in base class using master page? - asp.net

I am trying to assign a theme based on browser type. I would like to do this in a base class so it would only need to be in one place (I am using a master page). I coded the following but the "OnLoad" here is performed before the "Page_PreInit". This needs to go in Page_PreInit, but why isn't it firing?
Imports Microsoft.VisualBasic
Public Class MyBaseClass
Inherits System.Web.UI.Page
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
'Assign the CSS Theme based on the Browser Type
If (Request.Browser.Type = "IE8") Then
Page.Theme = "Standard-IE8"
Else
Page.Theme = "Standard"
End If
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
End Class
Then, I have my Login page coded to inherit the base class:
Partial Class Login
'Inherits System.Web.UI.Page
Inherits MyBaseClass
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Thank you,
James

You need to override OnPreInit in the base class.
Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
'Assign the CSS Theme based on the Browser Type
If (Request.Browser.Type = "IE8") Then
Page.Theme = "Standard-IE8"
Else
Page.Theme = "Standard"
End If
MyBase.OnPreInit(e)
End Sub
See here for more information on using a custom base class.

Related

Run after Init, before Page_Load

After a PostBack caused by ddlPlant_SelectedIndexChanged, I need to set set HttpContext.Current.Session("PlantNumber"). This needs to happen after ddlPlant loads in Site.Master, but before the code in Default.aspx needs the value.
Public Class SiteMaster Inherits MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
If (Not Page.IsPostBack) Then
ddlPlant.DataSource = myDataSource
ddlPlant.DataBind()
ddlPlant.SelectedValue = "1"
End If
HttpContext.Current.Session("PlantNumber") = ddlPlant.SelectedValue
End Sub
Protected Sub ddlPlant_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPlant.SelectedIndexChanged
End Sub
End Class
Public Class _Default Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim units As New List(Of EquipmentModel)
For Each unit As EquipmentModel In getUnits.Out.Results
If (CStr(unit.Plant_ID) = HttpContext.Current.Session("PlantNumber")) Then
units.Add(unit)
End If
Next
gvEquipmentUnit.DataSource = units.OrderBy(Function(n) n.Equipment_ID)
gvEquipmentUnit.DataBind()
End Sub
With the code above, when Session("PlantNumber") is set after PostBack, ddlPlant.SelectedIndex = Nothing, and ddlPlant.SelectedValue is an empty string.
I've tried moving the Session("PlantNumber") = ddlPlant.SelectedValue line to Site.Master's Page_Load instead, but that runs after it is needed in Default.aspx.vb
I looked up PreLoad, but apparently it doesn't work for the Master page.
Ultimately, I decided not to use a Session variable, and instead call the control directly from any page that needs that value on load (almost all of them):
DirectCast(Master.FindControl("ddlPlant"), DropDownList).SelectedValue

Reading a QR code

I downloaded Refinery.Barcodes.Reader.dll, and am using this:
Imports BusinessRefinery.Barcodes.Reader
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnread_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnread.Click
Dim barcodes As String() = BarCodeReader.scanBarCode("D://QRSAMPLE.png", BusinessRefinery.Barcodes.Reader.BarCodeType.QRCODE)
End Sub
End Class
It runs fine; no errors are thrown. But how do I see the result of the decoding process?
Try adding myTextBox.Text = barcodes before the End Sub.
edit:
After seeing comments, I think what you're running into is that scanBarCode returns an array, not a String. Check that, and try something like this:
Imports BusinessRefinery.Barcodes.Reader
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnread_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnread.Click
Dim barcodes As String() = BarCodeReader.scanBarCode("D://QRSAMPLE.png", BusinessRefinery.Barcodes.Reader.BarCodeType.QRCODE)
myTextbox.Text = barcodes(0)
End Sub
End Class

Using parameters in a code behind vb.net file on a DataSource to prevent injection attacks

We would like to change the following coding so it will use parameters to prevent injection attacks.
Here is the code in the Public Class area:
Public Class Parents1
Inherits System.Web.UI.Page
Dim theTableAdapter As New DataSetParentsSummaryTableAdapters.ParentsSummaryTableAdapter
Here is what we have in the page_load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
GridViewParentsSummary.DataSource = theTableAdapter.GetData("ALL")
End Sub
Here is the code used to load a GridView with data from a Search button click:
Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
GridViewParentsSummary.DataSource = theTableAdapter.GetData(TextBoxSearch.Text)
End Sub
Can you show the needed code required to use parameters?

ASP.NET - Accessing web page twice from client

What happens if one user tries to access an ASP.NET page twice before the first page is returned to the client? Have a look at the code below:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("ID") = 1
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("Default3.aspx")
End Sub End Class
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("ID") = 2
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("Default3.aspx")
End Sub
End Class
Imports System.Threading
Partial Class Default3
Inherits System.Web.UI.Page
Dim intTest As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For intTest = 0 To 10
Response.Write(Session("ID") & " " & intTest & "<br>")
Thread.Sleep(1000)
Next
End Sub
End Class
Accessing default3.axpx from the same client (PC) concurrently from default.aspx (by clicking button) and default2.aspx (by clicking button) causes the session variable to be the same on both requests (though I set the variable to 1 on the first request and 2 on the second). Is it possible to replicate this behaviour without threading? I believe I have this bug in an asp.net application that does not use threading.
Your question is not about multi-threading; it is about SessionState.
ASP.NET run time uses lock to avoid overriding same session variables although it can handles multiple requests.
It's why you are do not see miss-matching results.
Please also look at -
ASP.NET Application and Page Life Cycle
ASP.NET Application Life Cycle Overview
It's important to understand that session information is stored at the application level, and really has nothing directly to do with a page.
You could compare it to a global variable in a class. If all your methods are reading and writing to this variable, it will always contain the information from the last time it was updated. It doesn't matter how many times, or which methods, updated it.
To help see this in your code, create a new class with one property. Change your session variable to be an object of this class. Modify your session logic to refer to this object instead of a string, and update the property.
Finally, put a break point on the setter of your property.
This will let you see whenever your session variable is updated, and with what value. You can also view your stack trace to see what's setting it.
-note, this is all assuming you aren't introducing a farm into this and are accessing session from different machines--

custom ASP.NET TextBox on textchanged event

I want to let TextBox control TextChanged event fire only when there are more than one character in the TextBox.
Thank you.
Public Class ZTextBox
Inherits System.Windows.Forms.TextBox
Public Event ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
'Add your custom paint code here
End Sub
Private Sub ZTextBox_TextChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
If Me.Text.Length > 3 Then
RaiseEvent ZTextChanged(sender, e)
End If
End Sub End Class
and you can use the following in your form
Private Sub ZTextBox1_ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZTextBox1.ZTextChanged
MsgBox(1)
End Sub
I think you'll need a custom control that inherits the textbox control with a custom event, do your checks at the internal event, and fire your custom event when you see appropriate.

Resources