-
Reading Form collection data when posting back and reading data from button_onclick event.
Hi All,
I have a form that posts back to itself. I have a button_onclick event called
after the form is submitted. Anyone ever had luck passing form data either
through a post or get method to a Button_OnClick event. I've gotten the
same code to work when using the Page_Onload event. List below is sample
code of what i'm talking about. When i turn page tracing on, the form and
querystring values are getting passed but its like the Onclick event when
a form posts to itself doesn't work. Please either contact me via email
or reply to this....
This doesn't works
<%@ Page Trace="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<script language="VB" runat="server">
Sub steve_click(Sender As Object, e As EventArgs)
Dim Idiot as string = Request.form("text1")
response.write(request("text1"))
Dim myConnection As SQLConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=aspfree")
Dim insertString As String
Dim MyCommand As SQLCommand
InsertString = "Insert into Guestbook (Name) VALUES (@Idiot)"
MyCommand = New SQLCommand(InsertString, MyConnection)
MyCommand.Parameters.Add(New SQLParameter("@Idiot", SQLDataType.VarChar,
50))
MyCommand.Parameters("@Idiot").Value = request.form("text1")
myConnection.Open()
myCommand.Execute()
myConnection.Close()
End Sub
</script>
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<p align="center">
<b>
<font face="Arial"><strong><big>Guest Book Entry ASP.NET style!</big></strong></font></b></p>
<form method="Post">
<div align="center">
<center>
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td bgcolor="#800000"><asp:Label id="Label1" tooltip="Enter Your Name"
Font-Name="Arial" Forecolor="#ffffff" Text="Name" runat="server"/></td>
<td bgcolor="#C0C0C0"><asp:textbox id="text1" name="Name" size="30" runat="server"/></td>
</tr>
<tr>
<td align="center" colspan="2" bgcolor="#800000"><asp:Button id="Button1"
onclick="steve_click" Name="btnSubmit" type="Submit" text="Submit Message"
runat="server"/></td>
</tr>
</table>
</center>
</div>
</form>
</body>
This page submits to another page and uses the Page Onload event
<%@ Page Trace="true"%>
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<p align="center">
<b>
<font face="Arial"><strong><big>Guest Book Entry ASP.NET style!</big></strong></font></b></p>
<form method="POST" action="default3.aspx">
<div align="center">
<center>
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td bgcolor="#800000"><asp:Label id="Label1" tooltip="Enter Your Name"
Font-Name="Arial" Forecolor="#ffffff" Text="Name" runat="server"/></td>
<td bgcolor="#C0C0C0"><asp:textbox id="text1" Name="Idiot" size="30"
runat="server"/></td>
</tr>
<tr>
<td align="center" colspan="2" bgcolor="#800000"><asp:Button type="Submit"
text="Submit Message" runat="server"/></td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
Second page has the database logic in a compiled dll and it works fine.
<%@ Page Trace="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Idiot = Request.form("text1")
Dim PostMsg As New ASPFree.MessageText
PostMsg.GetNewHeadLines(Idiot)
End Sub
</script>
<html>
<head>
</head>
<body>
Body written
</body>
</html>
.VB file that has the db logic in it.
Imports System
Imports System.Data
Imports System.Data.SQL
Namespace ASPFree
Public Class PostDetails
Public Idiot As String
End Class
Public Class MessageText
Public Sub GetNewHeadlines(Idiot as String)
' Create Instance of Connection and Command Object
Dim myConnection As SQLConnection = new SQLConnection(aspfreedb.ConnectionString)
Dim insertString As String
Dim MyCommand As SQLCommand
InsertString = "Insert into Guestbook (Name) VALUES (@Idiot)"
MyCommand = New SQLCommand(InsertString, MyConnection)
MyCommand.Parameters.Add(New SQLParameter("@Idiot", SQLDataType.VarChar,
50))
MyCommand.Parameters("@Idiot").Value = Idiot
Try
' Open the connection and execute the Command
myConnection.Open()
myCommand.Execute()
Catch e As Exception
' An error occurred, pass the exception up
throw e
Finally
' Close the Connection
If myConnection.State = DBObjectState.Open then
myConnection.Close()
End If
End Try
End Sub
End Class
End Namespace
Helper db connection .VB file
Imports System
Imports System.Web
Imports System.Collections
Namespace ASPFree
Public Class ASPFreedb
Shared m_ConnectionString As String
'*******************************************************
'
' ASPFreeDB.ConnectionString Property
'
' The ASPFreeDB.ConnectionString property encapsulates
' a callout to the ASP+ Config System to obtain the
' database connection string for the application.
'
'*******************************************************
Shared ReadOnly Property ConnectionString As String
Get
' Pull the ConnectionString from the ASP+ AppSettings section.
' Cache in static field for faster repeat access.
If m_ConnectionString = "" Then
Dim appsetting As Hashtable = CType(HttpContext.Current.GetConfig("appsettings"),
Hashtable)
m_ConnectionString = CStr(appsetting("DSN"))
If m_ConnectionString = "" Then
throw new Exception("ASPFree DSN Value not set in
Config.web")
End if
End If
' Return the Connection String
return m_connectionString
End Get
End Property
End Class
End Namespace
--
* ----------------------------------------- *
* Steve Schofield steve@aspfree.com
* Webmaster
* http://www.aspfree.com
* http://www.abc2xml.com
* ----------------------------------------- *
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks