
November 19th, 2000, 08:14 PM
|
|
Contributing User
|
|
Join Date: Dec 2002
Posts: 14,578
  
Time spent in forums: < 1 sec
Reputation Power: 22
|
|
|
Retrieving Form data from an Button_Onclick event.? Can it be done??
<i><b>Originally posted by : steve schofield (steve@aspfree.com)</b></i><br /><br />Hi All,<br /><br />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....<br /><br />This doesn't works<br /><br /><%@ Page Trace="true"%> <br /><%@ Import Namespace="System.Data" %><br /><%@ Import Namespace="System.Data.SQL" %><br /><br /><script language="VB" runat="server"><br /> <br /> Sub steve_click(Sender As Object, e As EventArgs) <br /><br /> Dim Idiot as string = Request.form("text1")<br /> response.write(request("text1"))<br /> Dim myConnection As SQLConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=aspfree")<br /> Dim insertString As String<br /> Dim MyCommand As SQLCommand<br /><br /> InsertString = "Insert into Guestbook (Name) VALUES (@Idiot)"<br /><br /> MyCommand = New SQLCommand(InsertString, MyConnection)<br /><br /> MyCommand.Parameters.Add(New SQLParameter("@Idiot", SQLDataType.VarChar, 50))<br /> MyCommand.Parameters("@Idiot").Value = request.form("text1")<br /> myConnection.Open()<br /> myCommand.Execute()<br /> myConnection.Close()<br /> <br /> <br /> End Sub<br /><br /></script><br /><br /><br /><html><br /><br /><head><br /><title>New Page 1</title><br /></head><br /><body><br /><p align="center"><br /><b><br /><font face="Arial"><strong><big>Guest Book Entry ASP.NET style!</big></strong></font></b></p><br /><form method="Post"><br /><div align="center"><br /> <center><br /><table border="0" cellpadding="2" cellspacing="2"><br /> <tr><br /> <td bgcolor="#800000"><asp:Label id="Label1" tooltip="Enter Your Name" Font-Name="Arial" Forecolor="#ffffff" Text="Name" runat="server"/></td><br /> <td bgcolor="#C0C0C0"><asp:textbox id="text1" name="Name" size="30" runat="server"/></td><br /> </tr><br /> <tr><br /> <td align="center" colspan="2" bgcolor="#800000"><asp:Button id="Button1" onclick="steve_click" Name="btnSubmit" type="Submit" text="Submit Message" runat="server"/></td><br /> </tr><br /></table><br /> </center><br /></div><br /> <br /></form><br /></body><br /><br /><br />This page submits to another page and uses the Page Onload event<br /><br /><%@ Page Trace="true"%><br /><html><br /><head><br /><title>New Page 1</title><br /></head><br /><body><br /><p align="center"><br /><b><br /><font face="Arial"><strong><big>Guest Book Entry ASP.NET style!</big></strong></font></b></p><br /><form method="POST" action="default3.aspx"><br /><div align="center"><br /> <center><br /><table border="0" cellpadding="2" cellspacing="2"><br /> <tr><br /> <td bgcolor="#800000"><asp:Label id="Label1" tooltip="Enter Your Name" Font-Name="Arial" Forecolor="#ffffff" Text="Name" runat="server"/></td><br /> <td bgcolor="#C0C0C0"><asp:textbox id="text1" Name="Idiot" size="30" runat="server"/></td><br /> </tr><br /> <tr><br /> <td align="center" colspan="2" bgcolor="#800000"><asp:Button type="Submit" text="Submit Message" runat="server"/></td><br /> </tr><br /></table><br /> </center><br /></div><br /></form><br /></body><br /></html><br /><br />Second page has the database logic in a compiled dll and it works fine.<br /><br /><%@ Page Trace="true"%><br /><%@ Import Namespace="System.Data" %><br /><%@ Import Namespace="System.Data.SQL" %><br /><br /><script language="VB" runat="server"><br /> <br /><br /> Sub Page_Load(sender As Object, e As EventArgs) <br /> <br /><br /> Idiot = Request.form("text1")<br /><br /> Dim PostMsg As New ASPFree.MessageText<br /> PostMsg.GetNewHeadLines(Idiot)<br /> <br /> End Sub<br /><br /></script><br /><html><br /><head><br /></head><br /><body><br />Body written<br /></body><br /></html><br /><br />.VB file that has the db logic in it.<br /><br />Imports System<br />Imports System.Data<br />Imports System.Data.SQL<br />Namespace ASPFree<br /><br /><br /> Public Class PostDetails<br /> <br /> Public Idiot As String<br /><br /> End Class<br /><br /> Public Class MessageText<br /><br /><br /> Public Sub GetNewHeadlines(Idiot as String)<br /><br /> ' Create Instance of Connection and Command Object<br /> Dim myConnection As SQLConnection = new SQLConnection(aspfreedb.ConnectionString)<br /> Dim insertString As String<br /> Dim MyCommand As SQLCommand<br /><br /> InsertString = "Insert into Guestbook (Name) VALUES (@Idiot)"<br /><br /> MyCommand = New SQLCommand(InsertString, MyConnection)<br /><br /> MyCommand.Parameters.Add(New SQLParameter("@Idiot", SQLDataType.VarChar, 50))<br /> MyCommand.Parameters("@Idiot").Value = Idiot<br /><br /> <br /> Try <br /> ' Open the connection and execute the Command<br /> myConnection.Open()<br /> myCommand.Execute()<br /> Catch e As Exception<br /> ' An error occurred, pass the exception up<br /> throw e<br /> Finally <br /> ' Close the Connection<br /> If myConnection.State = DBObjectState.Open then<br /> myConnection.Close()<br /> End If<br /> End Try<br /> <br /> <br /> End Sub<br /> <br /> End Class<br /><br />End Namespace<br /><br />Helper db connection .VB file<br /><br />Imports System<br />Imports System.Web<br />Imports System.Collections<br /><br />Namespace ASPFree<br /><br /><br /> Public Class ASPFreedb<br /> <br /> Shared m_ConnectionString As String<br /><br /> '************************************************* ******<br /> '<br /> ' ASPFreeDB.ConnectionString Property<br /> '<br /> ' The ASPFreeDB.ConnectionString property encapsulates<br /> ' a callout to the ASP+ Config System to obtain the<br /> ' database connection string for the application.<br /> '<br /> '************************************************* ******<br /><br /> Shared ReadOnly Property ConnectionString As String <br /> <br /> Get <br /><br /> ' Pull the ConnectionString from the ASP+ AppSettings section.<br /> ' Cache in static field for faster repeat access.<br /><br /> If m_ConnectionString = "" Then<br /><br /> Dim appsetting As Hashtable = CType(HttpContext.Current.GetConfig("appsettings"), Hashtable) <br /> m_ConnectionString = CStr(appsetting("DSN"))<br /><br /> If m_ConnectionString = "" Then<br /> throw new Exception("ASPFree DSN Value not set in Config.web")<br /> End if<br /><br /> End If<br /><br /> ' Return the Connection String<br /> return m_connectionString<br /><br /> End Get<br /> <br /> End Property<br /> <br /> End Class<br /> <br />End Namespace<br /><br />-- <br />* ----------------------------------------- *<br />* Steve Schofield steve@aspfree.com<br />* Webmaster<br />* http://www.aspfree.com<br />* http://www.abc2xml.com<br />* ----------------------------------------- *
|