Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java script function

Status
Not open for further replies.

nelco

Programmer
Apr 4, 2006
93
US
I want to add java script function.

The following code works - if I put that on page load event.
If I put the same code in Button_click event - the message does not pop up.

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "myAlert", "alert('Record added');", True)

If this helps, I am also using Ajax controls on my page and update panel.

I will appreciate any help.
 
javascript is a client side scripting language where webforms and asp.net are server side frameworks. the 2 don't directly interact with each other. therefore it doesn't make sense to register javascript from a button click event. and if your introducing ajax into the mix [tt]ClientScript.RegisterClientScriptBlock[/tt] will never render the script to the client.

typically you keep your js scripts on the client (in js files) and the .net code on the server. you don't mix the 2 together.

with some more details about what you are trying to do, we may be able to guide to you a better solution.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the quick reply.
What I am trying to do is have a pop up message 'Record Added" in my button click event.

Currently in my code, I am showing that message in Label. I was asked to put that message as a pop up and I was wondering how to do that.

My current code is:
Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

If cmbAppName.SelectedValue <> " " And
CmbDistUsers.SelectedValue <> " " And
CmbSecTran.SelectedValue <> " " Then
' con = New SqlConnection(get_dbConnString("SQL:dba"))
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("DBAConnectionString").ConnectionString.ToString())

Dim SQLCmd As New SqlCommand
With SQLCmd
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandText = "UserSecurityTransact"
.Parameters.AddWithValue("boxName", "ADD")
.Parameters.AddWithValue("appName", cmbAppName.SelectedItem.Text.ToString())
.Parameters.AddWithValue("userName", CmbDistUsers.SelectedItem.Text.ToString())
.Parameters.AddWithValue("scrty_trn_nme", CmbSecTran.SelectedItem.Text.ToString())
Try
con.Open()
SQLCmd.ExecuteNonQuery()
Label2.Text = "Record Added Successfully"
Label1.Text = ""
clearForm()
Catch tmpE As Exception
Label1.Text = " * ERROR: Record not inserted. Duplicate record"
End Try
End With
Else
Label1.Text = " * ERROR: To Add: Please make sure that Application name,Security Transaction and Available user's are selected or filled"
Label2.Text = ""
End If

End Sub
 
are you using ajax with the button click? if so then this definitely won't work because the script block will never get injected into the response markup.

the other thing to change is you want to register a startup script
Code:
page.clientscript.registerstartupscript(gettype(), "alert('record added');");
this will execute as soon as the page is loaded into the browser and you will need to test this across different browsers to ensure there are no quirks.

in all honesty though, the label is the better approach. it's inline with common web UX and can be styled however it's required using CSS.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top