Assign the vaule of property of the user control get error - asp.net

Hi I have website which has usercontrol, I can load the control. However if I assign the value of property of the user control, I get error" Type "WebUserControl) is not defined. Can someone told me how to solve it. Thanks in advance.
There is the code to use my Usercontrol
Partial Class Test
Inherits System.Web.UI.Page
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim ctrl As Control
ctrl = Page.LoadControl("Management/Profile/WebUserControl.ascx")
ctrl.ID = "ctrl_1"
CType(ctrl, WebUserControl).Level = 6 //this line cause error
PlaceHolder1.Controls.Add(ctrl)
End Sub
End Class
Here is my WebUserControl
Partial Class Management_Profile_WebUserControl
Inherits System.Web.UI.UserControl
Private m_Level As Integer
Public Property Level() As Integer
Get
Return m_Level
End Get
Set(ByVal Value As Integer)
m_Level = Value
End Set
End Property
Protected Sub Management_Profile_WebUserControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = m_Level
End Sub
End 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

Write a form in VB.NET that will be the same state when you return to it

I'm trying to make a form that when it is return to, it will have the same state for the controls
I have 3 .net pages, testa,testb,testc
testa, sets up the session and has a button to go to testb. testb has a check box and a button to go to testc. testc has a button to return to page testb.
I want it so when we leave testc, and then come back the check mark will be in the same state, but it is not. it stays as uncheck when I go from page testc back to page testb
This is what i did,
On the page load event, I set the check box equal to the "button" Session variable. On the button event I save the button checked state in the Session variable and call page testc
Code
Page A
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
Page b
Partial Class testb
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim t As Boolean
t = Session.Item("button")
CheckBox1.Checked = t
End Sub
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim t As Boolean
t = CheckBox1.Checked
Session.Item("button") = t
Server.Transfer("testc.aspx")
End Sub
End Class
Page c
Partial Class testc
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
End Class
Data in the Session is stored as an Object. What you'll need to do is get the value from the Session and then convert it to a Boolean if it's not null (Nothing in VB). We've used this technique before:
Dim rawValue As Object = Session.Item("button")
Dim realValue as Boolean
If rawValue IsNot Nothing Then
realValue = Boolean.Parse(rawValue.ToString())
End If
It sounds like you need to check the IsPostBack property in the Page_Load method to decide if the values should be loaded or not. When it's a postback, don't load from the Session.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim t As Boolean = Session.Item("button")
CheckBox1.Checked = t
End If
End Sub

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

Pass GridView Cell Value to Sub Routine in ASP.NET / VB.NET

I have a GridView with Cell 0 containing the ID that I need to pass to a Public Sub.
I cannot figure out how to pick the value from Cell 0 in order to pass it to the Sub. I have tried experimenting (see the Dimmed EventID below) but have failed. Here is my code:
Protected Sub gvAppointmentsCalls_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAppointmentsCalls.RowCommand
Dim EventID As String = gvAppointmentsCalls.Rows(e.RowIndex).Cells(0).Text
If e.CommandName = "gvAdd2Outlook" Then
Send_iCal_Call(EventID)
End If
End Sub
If I type the value directly e.g. Send_iCal_Call(123) then it works perfectly.
Public Sub Send_iCal_Call(ByRef Event_ID As Integer)
' My code in here
End Sub
Use the CommandArgument to pass the ID to the RowCommand-Handler.
For example:
CommandName="gvAdd2Outlook" CommandArgument='<%# Bind("EventID")%>'
In my opinion you should obtain your ID value within GridView's DataKeyNames property. You should define it in your grid markup this property like here
<asp:GridView DataKeyNames="EvenID">
and then will access it in code behind:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "gvAdd2Outlook" Then
Dim EventIDString As String = GridView1.DataKeys.Item(e.RowIndex).Value.ToString()
Dim EventID As Integer
If Integer.TryParse(EventIDString, EventID) = False Then Throw New ArgumentException("Wrong EventID=" & EventID)
Send_iCal_Call(EventID)
End If
End Sub

Resources