JavaScript Add.Addtribute to an ASP.NET Button

  • Thread starter Thread starter IntraRELY
  • Start date Start date
I

IntraRELY

I need to attach the following 2 little scripts to an ASP.NET Button. I am
having an issue with running both of the Attributes.Add. I can only get on
to work at once. I have also tried to put them both/either in the page_load,
but cannot get it to work.

TIA,

Steve Wofford
www.IntraRELY.com


SUEDO Code:
----------------------
User clicks btnPrint
Sc. 1
btnPrint.Attributes.Add("OnClick", "javascript:return confirm('Are you sure
you want to print these checks?');")
removed records from the datagrid and redraws it w/o the checks records.
Sc. 2
btnPrint.Attributes.Add("onclick", "window.open('printCheckClient.exe',
1);")
Local vb.net client queries the db for the records that where removed from
the above datagrid.


ACTUAL CODE:
-----------------------
' Sc. 1
btnPrint.Attributes.Add("OnClick", "javascript:return confirm('Are you sure
you want to print these checks?');")
Dim checkIdItem As DataGridItem
sqlChecks.Open()
For Each checkIdItem In dgChecks.Items
Dim dgCheckbox As CheckBox = CType(checkIdItem.Cells(0).Controls(1),
CheckBox)
If dgCheckbox.Checked Then
Dim selectedCheckId = dgChecks.DataKeys(checkIdItem.ItemIndex)
With cmdChecksPrintUpdate
..Parameters("@checkId").Value = selectedCheckId
..Parameters("@printed").Value = 2
End With
Try
Dim eNonQuery = cmdChecksPrintUpdate.ExecuteNonQuery
With cmdChecksCount
..Parameters("@printed").Value = globalPrinted
End With
dgChecks.VirtualItemCount = cmdChecksCount.ExecuteScalar()
Catch ex As SqlClient.SqlException
lblMessage.Text = "Unexplected error." + ex.Message
Finally
End Try
End If
Next
If dgChecks.CurrentPageIndex = 0 Then
dgChecks_fill(globalPrinted)
Else
If Math.Ceiling(dgChecks.VirtualItemCount / dgChecks.PageSize) <
dgChecks.PageCount Then
Dim currentPageIndex = dgChecks.CurrentPageIndex
Dim pageCount = dgChecks.PageCount
If currentPageIndex + 1 = pageCount Then
dgChecks.CurrentPageIndex = Math.Ceiling(dgChecks.VirtualItemCount /
dgChecks.PageSize) - 1
Dim topExclude = (dgChecks.CurrentPageIndex) * dgChecks.PageSize
dgChecks_fill(topExclude)
Else
Dim topExclude = (dgChecks.CurrentPageIndex) * dgChecks.PageSize
dgChecks_fill(topExclude)
End If
Else
Dim topExclude = (dgChecks.CurrentPageIndex) * dgChecks.PageSize
dgChecks_fill(topExclude)
End If
End If
' Sc. 2
btnPrint.Attributes.Add("onclick", "window.open('printCheckClient.exe',
1);")
End Sub
 
Hi Steve,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you've a web page which has a button named
"btnPrint" and you'd like to add two "onclick" client side event for this
button, the first is to popup a messagebox to confirm the operation. Then
you want the page posted back to the server and do some Database
manipuliation operation, after that, you want to add the
"window.open('printCheckClient.exe)" to the btnPrint's "onclick" event.
If there is anything I misunderstood, please feel free to let me know.

As for the client side script and event. Each html control such as button
could only have one handler for a certain event. For example, for a button,
you could only set one function or script block for its "onclick" event one
time. As for your situation, I think you may set the btnPrint's "onclick"
event as "javascript:return confirm('Are you sure
you want to print these checks?');" Then, put the database operation in the
btnPrint's serverside click event, and use the "Page.RegisterStartupScript"
method to add a script block which will be executed right after the page is
loaded in the client again. For example:


private void btnPrint_Click(object sender, System.EventArgs e)
{
//add the database operation here


Page.RegisterStartupScript("OpenPrintWindow","<script
language='javascript'>window.open('printpage','_blank')</script>");

}


Also, to make it clearly, I've made a sample page to show the code logic.
You may try it out on your side to see whether it can help you on this
problem.

---------------------------------------aspx page
code--------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="600" align="center">
<tr>
<td><FONT face="ËÎÌå">
<asp:Label id="lblMessage" runat="server"></asp:Label></FONT></td>
<td>
<asp:Button id="btnPrint" runat="server"
Text="Print"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

-----------------------------------------code-behind page class
code-----------------------------
public class Show : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Button btnPrint;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
btnPrint.Attributes.Add("onclick","javascript:return confirm('Are you
sure you want to print these checks?');");


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPrint_Click(object sender, System.EventArgs e)
{
//add the database operation here
//simulate the data operation
System.Threading.Thread.Sleep(1000);
lblMessage.Text = "Database operation finish!";

Page.RegisterStartupScript("OpenPrintWindow","<script
language='javascript'>window.open('printpage','_blank')</script>");

}
}



If you feel the suggestion not quite suitable for your situation. Please
feel free to let me know and provide me more information on your
requirement so that I can make more effort on it.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I am having a problem with this sample using VB.NET that maybe you can
help me out with. I'm new to this so it's probably something obvious
I'm missing, please don't shoot me if it is! I've posted my code
below, when the Print button is clicked, a query box opens prompting
the user 'Are you sure you really want to?'. Clicking Cancel does
nothing but close the box as it should. However Clicking 'OK' also
does nothing, when it should open another window. (at least that is
what I want it to do). Any suggestions would be appreciated! Thanks.


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="confirmtest.aspx.vb" Inherits="ESR.WebForm4"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm4</title>
<meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR>
<meta content="Visual Basic 7.0" name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5
name=vs_targetSchema>
</HEAD>
<body>
<form id=Form1 method=post runat="server">
<table width=600 align=center>
<tr>
<td><FONT face=ËÎÌå><asp:label id=lblMessage
runat="server"></asp:label></FONT></td>
<td><asp:button id=btnPrint runat="server"
Text="Print"></asp:button></td></tr></table></form>

</body>
</HTML>


Public Class WebForm4
Inherits System.Web.UI.Page
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents btnPrint As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
btnPrint.Attributes.Add("onclick", "javascript:return
confirm('Are you sure ya wanna?');")

End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
'add the database operation here
'simulate the data operation
System.Threading.Thread.Sleep(1000)
lblMessage.Text = "Database operation finish!"

Page.RegisterStartupScript("OpenPrintWindow", "<script
language='javascript'>window.open('dothis.aspx','ppp,'width=800,height=600');</script>")

End Sub
End Class
 
Hi jean,


Thanks for your followup, as for your problem in VB.NET code, would you
please try the below code, this is a test page
I created using VB.NET which provide the same function as the C# one:

---------------------------------aspx page--------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ButtonEvent</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:Button id="btnConfirm" runat="server" Text="Confirm
PostBack"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>


------------------------code behind page class---------------------
Public Class ButtonEvent
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnConfirm As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
btnConfirm.Attributes.Add("onclick", "javascript:return
confirm('Are you sure?')")
End Sub

Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnConfirm.Click
Response.Write("<br>You have clicked Yes!")
Page.RegisterStartupScript("PopUpWindow", "<script
language='javascript'>window.open('http://www.asp.net')</script>")
End Sub
End Class

----------------------------------------------------------------------------
------------



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top