i have three textboxes, one has 'quantity', the second has 'unit price' and the third has 'total'. what i want is to display the product of 'quantity' and 'unit price' into the 'total' textbox using the keyup trigger.
I am just beginning asp.net
Thanks
The focus of your application wont lie on the form itself so I would suggest putting the event in each textbox.
There is for sure a smoother solution but this worked for me:
Private Sub TextBoxQuantity_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxQuantity.KeyUp
If e.KeyCode = Keys.Up Then
TextBoxTotal.Text = TextBoxQuantity.Text & vbTab & TextBoxUnitPrice.Text
End If
End Sub
Private Sub TextBoxUnitPrice_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxUnitPrice.KeyUp
If e.KeyCode = Keys.Up Then
TextBoxTotal.Text = TextBoxQuantity.Text & vbTab & TextBoxUnitPrice.Text
End If
End Sub
Private Sub TextBoxTotal_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxTotal.KeyUp
If e.KeyCode = Keys.Up Then
TextBoxTotal.Text = TextBoxQuantity.Text & vbTab & TextBoxUnitPrice.Text
End If
End Sub
This is a wiki page on VB.net KeyPress-Event. Please read it to avoid further questions on this topic.
Related
I am trying to retrieve the text from a Label within a List View control. The method I am using, below, will only retrieve the first piece of text I want. The text I am looking for is an ID, therefore I am always returning the first ID no matter what item I click on.
Private Sub guidelinesList_SelectedIndexChanged(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles guidelinesList.SelectedIndexChanged
i = guidelinesList.SelectedIndex
End Sub
Private Sub guidelinesList_ItemDataCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles guidelinesList.ItemCommand
Dim theIdLabel As Label = CType(guidelinesList.Items(i).FindControl("lblId"), Label)
guidelinesId = CInt(theIdLabel.Text)
If String.Equals(e.CommandName, "bEdit") Then
Response.Redirect("../EditFile.aspx?FileId=" & guidelinesId & "&FileType=Guidelines" & "&AppType=Payroll")
End If
End Sub
Small code change is all that was needed, see below.
Private Sub guidelinesList_ItemDataCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles guidelinesList.ItemCommand
If String.Equals(e.CommandName, "bEdit") Then
Dim i As Integer = CInt(e.Item.DataItemIndex)
Dim theIdLabel As Label = CType(guidelinesList.Items(i).FindControl("lblId"), Label)
guidelinesId = CInt(theIdLabel.Text)
Response.Redirect("../EditFile.aspx?FileId=" & guidelinesId & "&FileType=Guidelines" & "&AppType=Payroll")
End If
End Sub
I have a CheckBoxList on my page that isn't behaving very well.
The idea is that once the submit button on the form is clicked, the app should clear the database table of all rows pertaining to the specific user and then re-insert new ones based on the user's CheckBoxList selections.
The problem is that regardless of whether or not any (or all) items in the CheckBoxList are selected, the app keeps getting Selected = False.
Here's my code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
loadRegions()
End Sub
Private Sub loadRegions()
Dim db As New Database
Dim sql As String = "select * from regions"
Dim dr As MySqlDataReader = db.execDB(sql, "Text", Nothing, "DataReader", False)
If dr.HasRows Then
cblRegion.DataSource = dr
cblRegion.DataTextField = "regionname"
cblRegion.DataValueField = "regionid"
cblRegion.DataBind()
End If
dr.Close()
End Sub
Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click
' ============================================================
' There's more code in here, but it's irrelevant to this paste
' ============================================================
Dim sql As String = "delete from userregions where userid = " & lblUserID.Text & ";"
For i As Integer = 0 To cblRegion.Items.Count - 1
If cblRegion.Items(i).Selected Then
sql &= "insert into userregions (userid, regionid)" & _
"values(" & UserID & ", " & cblRegion.Items(i).Value & ")"
db.execDB(sql, "Text", Nothing, "None", False)
End If
Next
End Sub
For the record
I'm aware of the potential for SQL Injection here. I'll be going over to using Parameters as soon as I have the loop working.
Thanks for your time.
Any help will be greatly appreciated.
You only need to call loadRegions on the initial load and not on postbacks:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
loadRegions()
End If
End Sub
Otherwise you'll lose changed values and events are not triggered.
Add this line of copde "If IsPostBack Then Return" in Page_Load method.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If IsPostBack Then Return
loadRegions()
End Sub
In page loaded, write the following:
If Not IsPostBack
loadRegions()
End If
How do you set focus to a specific control in a datagridview row after clicking the edit button? I'm able to do it for a new row when the grid is binding, but not for an existing row. The control doesn't seem to exist yet.
'This doesn't work (existing row)
Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
Try
gvDays.EditIndex = e.NewEditIndex
gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
Catch ex As Exception
Helper.WriteException(ex)
End Try
End Sub
'This does work for a newly bound row
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If e.Row.RowState = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub
gvDays_RowDataBound should work, the problem is you are looking at e.Row.RowState using the = operator, but RowState is a bitflag
try this
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub
I have a method named raise_alarm() which, show a message box based on jquery. But when I call this method from an event of a control(such as submit button) which is inside of Updatepanel, it isn't working. Related codes are below. How can I fix it?
Public Sub Raise_Alarm(ByVal p_Page As Page, ByVal p_Message As String, Optional ByVal p_IsError As Boolean = True)
Dim strScript As String
strScript = "$(function() { Mesaj('" & p_Message & "'); });" & ControlChars.NewLine
p_Page.ClientScript.RegisterStartupScript(p_Page.GetType(), "alert", strScript, True)
end sub
Private Sub dtlQuestion_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtlQuestion.ItemCommand
If Not User.Identity.IsAuthenticated Then
Raise_Alarm(Me, "Giriş Yapmadan Oy Veremezsiniz")
Exit Sub
End If
end sub
You have to use ScriptManager instead of p_Page.ClientScript.
EDIT : Example. I replaced p_Page.ClientScript by ScriptManager in your code.
Public Sub Raise_Alarm(ByVal p_Page As Page, ByVal p_Message As String, Optional ByVal p_IsError As Boolean = True)
Dim strScript As String
strScript = "$(function() { Mesaj('" & p_Message & "'); });" & ControlChars.NewLine
ScriptManager.RegisterStartupScript(p_Page.GetType(), "alert", strScript, True)
end sub
Private Sub dtlQuestion_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtlQuestion.ItemCommand
If Not User.Identity.IsAuthenticated Then
Raise_Alarm(Me, "Giriş Yapmadan Oy Veremezsiniz")
Exit Sub
End If
end sub
ClientScript is not ajax enabled, ScriptManager knows how to deal with partial postbacks. Please have a quick look at this article on msdn.
could anybody advice me, are there any approaches to calculate the methods execution time in asp.net page?
ASP.Net Trace doesn't suit me since it ignores ajax partial postbacks though just they are the tight place.
I have the page event which for some unknown reason executes slowly. Since the logic and event execution chain is rather complicated, I still unable to merely determine the culprit in debugger.
I'd like to measure the execution time of each method throughout the page, but I still see no way but insert redundant code lines (like timeBegin = DateTime.Now.... timeEnd=DateTime.Now = timeBegin etc) in the beginning and ending of each method. It seems to be ugly approach, does anybody know how to do it automatically? Are there tools which measure the methods execution time available?
I am not 100% sure the below works in all cases. So far it does though. Note that Context.Session.Item("CURRENT_WEB_USER") is where I store the current user, and is therefore not readily available as part of the asp.net session variables.
The code hereunder is in replacement of Global.aspx that allows both full page timing and timing of developer code.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AcquireRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If HttpContext.Current.Session Is Nothing Then Return
If TypeOf HttpContext.Current.CurrentHandler Is Page Then
If Not Context.Items.Contains("Request_Start_Time") Then
Dim MyPage As Page = HttpContext.Current.CurrentHandler
AddHandler MyPage.Init, AddressOf PageInitEncountered
AddHandler MyPage.Unload, AddressOf PageUnloadEncountered
Context.Items.Add("Request_Start_Time", DateTime.Now)
Dim User As String = Context.Session.Item("CURRENT_WEB_USER")
Context.Items.Add("Request_User", User )
End If
End If
End Sub
Sub Application_ReleaseRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If TypeOf Context.CurrentHandler Is Page Then
Dim Page As Page = Context.CurrentHandler
Dim StartDate As Date = Context.Items("Request_Start_Time") : If StartDate = Nothing Then Return
Dim EndDate As Date = Now
Dim StartProc As Date = Context.Items("Page_Init")
Dim EndProc As Date = Context.Items("Page_Unload")
Dim User As String = Context.Items("Request_User")
LogEntry(StartDate, EndDate, StartProc, EndProc, User, Context.Request.Url.ToString)
End If
End Sub
Public Sub PageInitEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Init", DateTime.Now)
End Sub
Public Sub PageUnloadEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Unload", DateTime.Now)
End Sub
Public Sub LogEntry(StartDate As Date, EndDate As Date, StartProc As Date, EndProc As Date, User As String, PageURL As String)
System.Diagnostics.Debug.WriteLine(" (" & LogEntry.RequestMs & " - " & LogEntry.DurationMs & ") (" & User & ") " & PageURL)
End Sub
This may not be what you are looking for, but RedGate has a product called Performance Profiler that does exactly that. They do have a 15 day trial so you can even download it and try it out on your current issue. IMHO, it's worth every penny.
You can use asp.net performance counters and other things also. But that will not measure a page speed but they will measure a over all application performance.