and thank you for viewing my post. I have a project in vb completed in Visual Studio 2010. Once the user clicks the submit button, I want a popup message that has certain text. I also want an okay and cancel button. If the user clicks okay, the page goes as normal and submits. It cancel is clicked, the page goes back for the user to edit info.
Now, I've tried using msgbox, but it doesn't work server side. From research, I found I prob need to use Jquery. I don't have much experience with that, so any help is appreciated!
Thanks,
Josh
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If (Not Page.IsPostBack) Then
Me.btnSubmit.Attributes.Add("onclick", "return confirm('Please note that what you have entered will be used to auto-generate the flyer. Also, submissions less than 30 days from the date entered may be denied.');")
End If
Dim objRequestID As Object = Nothing
Dim intRequestID As Integer = 0
Try
If ValidateForm() Then
Using objConnection As SqlConnection = util.GetConnection()
Using objTransaction As SqlTransaction = objConnection.BeginTransaction
Using objCommand As SqlCommand = New SqlCommand("Request_Insert", objConnection)
With objCommand
.CommandType = Data.CommandType.StoredProcedure
.Transaction = objTransaction
.Parameters.Add("#CourseTypeID", Data.SqlDbType.Int).Value = util.CourseRequestType.PIE
.Parameters.Add("#SponsorName", Data.SqlDbType.NVarChar, 50).Value = SponsorName.Text.Trim
.Parameters.Add("#SponsorPhone", Data.SqlDbType.NVarChar, 20).Value = SponsorPhone.Text.Trimenter code here
You need to put client button onclick event at server Page_Load event.
When you reach to btnSubmit_Click, it is too late.
Protected Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
If (Not Page.IsPostBack) Then
Me.btnSubmit.Attributes.Add("onclick", "return confirm('Please...');")
End If
End Sub
you can call this javascript on button click
<script>
function myFunction()
{
alert("I am an alert box!");
}
</script>
Related
i have some different buttons in a page after clicking them i want them to redirect me to a page that has a gridview , but for each button it gives a different gridview which are generated by datatables . may you help to find ,how can i do that in asp.net, vb ??
i did two pages at the gridview page i made methods that are binding different gridviews for each button , and in the first page where i have the button click events i did the redirecting to this gridview page and called the corresponding methods. it redirects me but doen't give me any gridview at all :(
Call in button action
Response.Redirect("Your Page Name.aspx")
and call in that page a BindGrid Function that you create to bind the gridview
You can redirect by using
Response.Redirect("GridView.aspx")
At the same time you can use only one aspx by using session
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session.Add("CheckButton", Button1Clicked)
Response.Redirect("GridView.aspx")
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Session.Add("CheckButton", Button2Clicked)
Response.Redirect("GridView.aspx")
End Sub
After that at your GridView.aspx on your pageload
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CheckButton As String = Session("CheckButton")
Dim query As String
If (CheckButton = "Button1Clicked") Then
query = "SELECT * FROM Table1"
Else If (CheckButton = "Button2Clicked") Then
query = "SELECT * FROM Table2"
Else
Response.Write("<script>alert('Error!')</script>")
End If
SqlDataSource1.SelectCommand = query
SqlDataSource1.DataBind()
End Sub
I'm inserting record into the DB after the button has been clicked then on page load I'm checking if the user attended today and display the right label. Problem is it keeps displaying the wrong label after postback, but if I do one refresh i get the correct label displayed.
Protected Sub AttendBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AttendBtn.Click
Dim con As New SqlConnection
Dim conString As String
conString = ConfigurationManager.ConnectionStrings("attConnection").ConnectionString
con = New SqlConnection(conString)
con.Open()
Dim cmd As New SqlCommand("INSERT INTO Attendance (UserID, Date, Attendance_Cycle, Comments)VALUES (#UserID, #Date, #Attendance_Cycle, #Comments)", con)
cmd.Parameters.AddWithValue("#Date", DateTime.Now)
cmd.Parameters.AddWithValue("#Attendance_Cycle", GlobalVarAndFunc.CurrentCycle(2))
cmd.Parameters.AddWithValue("#Comments", "")
cmd.Parameters.AddWithValue("#UserID", GlobalVarAndFunc.UserID)
cmd.ExecuteNonQuery()
con.Close()
End Sub
and on page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If GlobalVarAndFunc.HasAttended Then
attend_LB.Text = "You already attended today"
Else
attend_LB.Text = "Attend now"
End If
End Sub
GlobalVarAndFunc.HasAttended function checks if the User has pressed on the attended button today or not
Please understand the flow of asp page. System will execute your page load before the button click event. So in your case, the moment page_load was called at that time you get the value that is already there in DB because system hasn't executed click event yet. When click event gets execute then your value gets into DB (before this event your DB wasn't updated). So on next refresh you gets the correct value.
Solution : Place page_load code in btn_click event to get the result in one go.
I have a relatively simple ASP.NET problem (I should think) that regrettably I am unable to solve by myself. What I am trying to do is the following:
On a page I load a number of controls (Text Boxes) programmatically;
following this load the user should be able to select a value to load into the Textbox from a panel control that is added to the page following the click of a button
Once the panel is closed, the selected text from the panel should be loaded into the textbox
However, in the vb.net statements below when run the "test" string never makes it to the textbox - any help with resolving this would be greatly appreciated.
Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Controls_Load()
End Sub
Public Sub Controls_Load()
Dim ttf_tb As New TextBox With {.ID = "ttf_tb"}
Master_Panel.Controls.Add(ttf_tb)
Dim ttf_button As New Button
Master_Panel.Controls.Add(ttf_button)
AddHandler ttf_button.Click, AddressOf TTF_BUTTON_CLICK
End Sub
Public Sub TTF_BUTTON_CLICK(sender As Object, e As EventArgs)
Dim str As String = sender.id
Dim panel As New Panel
panel.ID = "TTF_Panel"
panel.Width = 300
panel.Height = 300
Master_Panel.Controls.Add(panel)
panel.BackColor = Drawing.Color.Black
panel.Style.Add(HtmlTextWriterStyle.Position, "absolute")
panel.Style.Add(HtmlTextWriterStyle.Left, "200px")
panel.Style.Add(HtmlTextWriterStyle.Top, "100px")
panel.Style.Add(HtmlTextWriterStyle.ZIndex, "100")
Dim CL_Button As New Button
CL_Button.ID = "TTF_Close_" & Replace(str, "TTF_Button_", "")
panel.Controls.Add(CL_Button)
AddHandler CL_Button.Click, AddressOf TTF_Close_Button_Click
End Sub
Public Sub TTF_Close_Button_Click(sender As Object, e As EventArgs)
Dim ttf_tb As TextBox = Master_Panel.FindControl("ttf_tb")
ttf_tb.Text = "Test"
Dim panel As Panel = FindControl("TTF_Panel")
Master_Panel.Controls.Remove(panel)
End Sub
End Class
I think you need to re-create your controls in the Page_Init method. It's been a while since I've done web forms but I think it's something like:
When a user clicks the button a post back is fired. This re-creates a new instance of your class, creates the controls on the page, assigns any form values then calls your Page_Load event.
The problem is you are creating your controls too late, so the forms values are never assigned correctly.
You should create / recreate your dynamic controls in the Init event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Controls_Load()
End Sub
This should allow you to maintain their state across PostBacks.
For more information about this topic, see the MSDN article on the ASP.NET Page Life Cycle.
On my page I have this button, which calls this method, it works. However, when I set that same method, to be called during Page Load it works, but not completely. The method has a few readers, they read from the database and show the information, there's no issue when the method is ran reguarly, but on Page Load the readers never read.
This is one my select statements that fuel the reader:
sql.CommandText = "select temp from forecast where zone='" + myzone + "' and date=TO_DATE('" + mydate + "','dd/mm/yyyy') order by zone,date,time"
sql.CommandType = CommandType.Text
reader = sql.ExecuteReader()
Dim x As Integer
While (reader.Read())
data(0, x) = reader.Item("temp").ToString()
x += 1
End While
Again, when ran as a button click once the page is loaded it reads, when ran during Page Load it doesn't read.
It cannot be the select statement since the same exact statement works when the button is clicked. Also both variables are working and have the values they should when the statement is executed during Page Load so it can't be that either. Also tried with both Load and Init and none will work.
My Button Click:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.myMethod()
End Sub
And the Page Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.myMethod()
End Sub
Any ideas of what I may be doing wrong? Thanks!
Can I get some help posting across different pages from a custom control?
I've created a custom button that raises it's own click event through the following code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Const EventName As String = "button_click"
Const ArgName As String = "__EVENTARGUMENT"
If Page.IsPostBack _
AndAlso Request.Params IsNot Nothing _
AndAlso Request.Params(ArgName).Trim = EventName Then
Me.OnClick(Me.this_button, New EventArgs)
Else
Me.this_button.Attributes.Add("OnClick", Page.ClientScript.GetPostBackEventReference(Me.this_button, EventName))
End If
End Sub
How would I go about modifying this to let me post to a different page?
I'd like it to act as close as possible to the System.Web.UI.WebControls.Button property PostBackUrl.
You can use Cross Page Postbacks.
You can also use the WebForm_DoPostBackWithOptions js method to postback the current page to another page.