Need a 'RaiseEvent' statment to raise an event - asp.net

I have a page on the website that display a client details in text boxes.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not page.IsPostBack Then
Dim dh As New Datahandler
Dim CI As ClientInfomation = dh.GETClientInfomationWithClientID(Session("ClientID"))
txtClientID.Text = Session("ClientID")
txtCompany.Text = CI.Company
txtEmail.Text = CI.Email
txtPassword.text = CI.Password
dtSubcriptionEndDate.Text = CI.SubscriptionEndDate
txtContactName.Text = CI.ContactName
txtTelephoneNum.Text = CI.Telephone.ToString()
End If
The above code works,the code gets the client data from the database in the GETClientInfomationWithClientID(Session("ClientID")) and loads the data into a class, and from the class I load the data into the text boxes.
Here is where my problem starts. A user will now change his details in the text boxes and click the 'Save changes' button invoking the butSaveChanges_Click Event.
Protected Sub butSaveChanges_Click(sender As Object, e As EventArgs) Handles butSaveChanges.Click
Dim dh As New Datahandler
Dim CI As New ClientInfomation With {.ClientID = txtClientID.Text,
.Company = txtCompany.Text,
.ContactName = txtContactName.Text,
.Email = txtEmail.Text,
.Password = txtPassword.Text,
.SubscriptionEndDate = dtSubcriptionEndDate.Text,
.Telephone = txtTelephoneNum.Text}
If dh.SaveUserProfileChanegs(CI) = True Then
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Changes has been saved')", True)
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Changes could not be saved')", True)
End If
End Sub
I just wanted to load the changed text values into the class, and load the class in the SaveUserProfileChanegs(CI) function that updates the new values in the database.
When the butSaveChanges_Click event is invoked I get a "(1) : error BC32022: 'Public Event Click As EventHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."

Related

session saved data not fetching in vb.net

I am trying to get data in textbox if previously saved in session. When user enters page it will be blank. If he fills data & submit then I want If page loads again then it should take it from session values
Private Sub Online_Medicines_order_online_Default_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("patientName") Is Nothing Then
patientName.Text = ""
Else
patientName.Text = Session("patientName").ToString
End If
End Sub
Private Sub orderButton_Click(sender As Object, e As EventArgs) Handles orderButton.Click
Session("patientName") = patientName.Text
Session("address1") = address1.Text
Session("address2") = address2.Text
Session("landmark") = landmark.Text
Session("zip") = zip.Text
Session("mobileNumber") = mobileNumber.Text
Session("city") = city.Text
End Sub

Calling function on dynamic page in aspx code behind

I have a function loadList() that loads a dynamic list based on our database.
In element_maint.aspx.vb:
Private Sub loadList()
'load sub-organizations
Dim lstItems As cihElementList = Nothing
Dim thr As TableHeaderRow = Nothing
Dim tr As TableRow = Nothing
Dim tc As TableCell = Nothing
Dim lnk1 As HyperLink = Nothing
Dim lnk2 As HyperLink = Nothing
Dim item As cihCategoryOrgDef = Nothing
Dim rvd As Routing.RouteValueDictionary = Nothing
Dim rvdDeactivate As Routing.RouteValueDictionary = Nothing
Dim vpd As Routing.VirtualPathData = Nothing
Dim vpdDeactivate As Routing.VirtualPathData = Nothing
Dim btnAction As cihBootstrapButtonDropdown = Nothing
'*****************************************
'Build the action drop down
'Build the action button
'*****************************************
btnAction = New cihBootstrapButtonDropdown("Action", cihBootstrapButtonDropdown.buttonTypes.link)
'Existing Items
lstItems = New cihElementList()
lstItems.loadForOrganization(orgId)
For Each ele As cihElementList.elementShort In lstItems.listOfElements
tr = New TableRow
lnk1 = New HyperLink
lnk1.Text = "Edit"
lnk2 = New HyperLink
lnk2.Text = "Deactivate"
rvd = New Routing.RouteValueDictionary(New With {.action = "edit", .elementid = ele.elementId.ToString()})
rvdDeactivate = New Routing.RouteValueDictionary(New With {.action = "deactivate", .elementid = ele.elementId.ToString()})
vpd = Routing.RouteTable.Routes.GetVirtualPath(Nothing, "element_maint", rvd)
vpdDeactivate = Routing.RouteTable.Routes.GetVirtualPath(Nothing, "element_deactivate", rvdDeactivate)
btnAction.addLink("Edit", vpd.VirtualPath, False)
If (ZenCommon.CurrentUser.secLevelId = cihCommonDef.FullSiteAdmin Or ZenCommon.CurrentUser.secLevelId = cihCommonDef.OrganizationAdmin) Then 'if SuperAdmin or Campus Admin
'Need Help here
btnAction.addLink("Deactivate", vpdDeactivate.VirtualPath, False)
End If
ZenCommon.AddCell(tr, ele.eleDescr, , , "200px")
tc = New TableCell
tc.Controls.Add(New LiteralControl(btnAction.buttonHTML))
tc.Style.Add("width", "50px")
tr.Cells.Add(tc)
Me.tblList.Rows.Add(tr)
Next
End Sub
Private Sub deactivateTag()
'Goto Database, flag 'tag' as inactive
End Sub
I want the btnAction.addLink("Deactivate") to call a function in my aspx.vb. The function will go mark the tag as inactive.
Right now the 'Edit" button (just above the 'Deactivate' button, will go to a new page to allow you to edit it. I get that. But is there a way to make it so when I click my 'Deactivate' button it will just call my deactivateTag()
In your codebehind you could change your method signature
Protected Sub deactivateTag(ByRef sender as Object, ByRef e as System.EventArgs) Handles btnDeactivate.Click
and in your markup
<asp:Button ID="btnDeactivate" onClick="deactivateTag" />
Or you can add a click event for the button and call your function from that event.
Protected Sub btnDeactivate_Click(ByRef sender as Object, ByRef e as System.EventArgs) Handles btnDeactivate.Click
deactivateTag()
End Sub
It depends on whether you'll need to call deactivateTag from other controls or not.

Object reference not set to an instance of an object in ASP(vb)

I came across to this code that creates control dynamically. I tried it. However, whenever I run it, Object reference not set to an instance of an object error pops out and points out to a certain line in the code( i put ---> on the line). I'm a newbie in this programming language. I don't know what to do.
Here's the code I got:
Imports System.Collections.Generic
'Imports System.Data.Odbc
Partial Public Class main
Inherits System.Web.UI.Page
Private controlCounter As Integer = 0
Private myControlList As List(Of String)
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
myControlList = DirectCast(ViewState("myControlList"), List(Of String))
For Each ctlID As String In myControlList
controlCounter += 1
Dim hyper As New HyperLink()
hyper.ID = ctlID
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
myControlList = New List(Of String)()
ViewState("myControlList") = myControlList
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
controlCounter += 1
Dim hyper As New HyperLink()
hyper.Text = "a new text Box"
hyper.ID = "hyperlink" + controlCounter.ToString()
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
--> myControlList.Add(hyper.ID)
ViewState("myControlList") = myControlList
End Sub
End Class
please help me out. Thanks.
A button click initiates a postback, and you only set myControlList to a thing if the request is not a post back, so it's nothing.
If you're wanting to create the list on page load, and then only keep adding to it with subsequent clicks, say, then you'll need to shove myControlList into the Session, or something, after creating it, then on button click retrieve it again, add to it, and re-set it in the Session.
To do this add and getting from the Session stuff, do,
Session("someDistinctKey") = myControlList;
myControlList = CType(Session("someDisctintKey"), List(Of String))
respectively.

ASP.NET VB Listbox DataBind Not Working

In my user control, I have a text boxes that I used to create a new membership user. Each listbox has a datasource set to membership.getallusers() to fill the listbox. When the listbox index is changed, it populates the text boxes to allow editing. What I want to accomplish is: have each user listbox refresh to show the new user that gets created. Even though I call databind() after successfully creating the user, it doesn't update. On the form load I check if listbox.items.count < 1, then call databind(), which does work correctly. Any ideas?
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If ListBox3.Items.Count < 1 Then
For Each item As MembershipUser In Membership.GetAllUsers(0, Membership.GetAllUsers.Count + 1, Membership.GetAllUsers.Count)
ListBox3.Items.Add(item.UserName)
Next
Label1.Text = ListBox3.Items.Count
username = ListBox3.Items(0).ToString
Else
ListBox1.DataBind()
ListBox3.DataBind()
End If
user = Membership.GetUser
ListBox1.DataSource = Membership.GetAllUsers
ListBox1.DataBind()
ListBox2.DataSource = Roles.GetAllRoles
ListBox2.DataBind()
' ListBox3.DataSource = Membership.GetAllUsers
'ListBox3.DataBind()
End Sub
Protected Sub ListBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox3.SelectedIndexChanged
txtUsername.Text = user.UserName
txtEmail.Text = user.Email
txtQuestion.Text = user.PasswordQuestion
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try'Creates a new user
Dim status As System.Web.Security.MembershipCreateStatus
Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, True, status)
Label1.Text = "User " & txtUsername.Text & " was created"
'ListBox3.Items.Clear()
ListBox1.DataBind()
'ListBox3.DataBind()
Catch ex As Exception
Label1.Text = "Error:" & ex.Message
End Try
End Sub
You need to set the datasource again before you do the databind, like so:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try'Creates a new user
Dim status As System.Web.Security.MembershipCreateStatus
Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, True, status)
Label1.Text = "User " & txtUsername.Text & " was created"
'ListBox3.Items.Clear()
ListBox1.DataSource = Membership.GetAllUsers() ' Necessary because the DB has changed
ListBox1.DataBind()
'ListBox3.DataBind()
Catch ex As Exception
Label1.Text = "Error:" & ex.Message
End Try
End Sub
When you call databind, you might think that that's the moment the Listbox will call the method you gave it as a datasource, but that's not the case. The moment you assign the datasource, the data is retrieved. When you actually call the Databind-method, the Listbox will create the necessary internal controls

change textbox's value from pageload event, submit new values issue

I have created a form to update an access DB table. My issue is that when the text in the text boxes is changed and the form is submitted, the .text values stay the same as they were when the datareader loaded them on the page load event. How can I submit the values that the user updates, not what is already there from page load.
Code:
Public Property vehicleid As Integer
Public Property connstring As String = "myconnectionstring..."
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vehicleid = Integer.Parse(Request.QueryString("vehicID"))
Dim svEnterdate, stocknum, make, model, color As String
Dim conn As New OleDbConnection(connstring)
Dim sql As String = "select * from vehicle where vehicleid=#vid"
Dim cmd As New OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#vid", vehicleid)
conn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read
svEnterdate = dr("enterdate").ToString()
stocknum = dr("stock_num").ToString()
make = dr("make").ToString()
model = dr("model").ToString()
color = dr("color").ToString()
End While
conn.Close()
EnterDateTXT.Text = svEnterdate
StockNumTXT.Text = stocknum
MakeTxt.Text = make
ModelTXT.Text = model
ColorTxt.Text = color
End Sub
'inbetween these 2 events the user can manipulate all the controls .text values, yet the
' .text values of the submitted controls below are the same as the ones filled by the
'datareader
Protected Sub SubmitBTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitBTN.Click
Dim conn As New OleDbConnection(connstring)
Dim sql As String = "UPDATE Vehicle" & _
" SET stock_num=#stock, make=#make, model=#model, color=#color, enterdate=#enter " & _
"WHERE vehicleid=#vid"
Dim cmd As New OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#vid", vehicleid)
cmd.Parameters.AddWithValue("#stock", StockNumTXT.Text)
cmd.Parameters.AddWithValue("#make", MakeTxt.Text)
cmd.Parameters.AddWithValue("#model", ModelTXT.Text)
cmd.Parameters.AddWithValue("#color", ColorTxt.Text)
cmd.Parameters.AddWithValue("#enter", EnterDateTXT.Text)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Sub
In your page load code, Check For Post back
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Write your code to read data from database here
End
End Sub
If you dont check for postback in your page load event, Everytime when you click the submit button, It is going to excute the code in your page load ( load the content again to the text box) first. So whatever you entered in the textbox will be overwritten by the content form the database and that will be saved back again to the database.
To undestand this. Put a breakpoint in your Page_load event code and another in your button click event code. Now enter some value in textbox and click the button and see whether your code block in pageload is executing or not.
Checking the Postback check in your page_load will fix the problem.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

Resources