I am in a very big pickle. I am trying to add page content - as well as
a submit button - programatically to a web form that is supposed to
submit to DB and then refresh.
That is, a user goes to the web page, which draws the current content
out of the db and inserts into a "preview" area as well as the form
itself. User makes changes, hits submit button. The page *should*
refresh, with the changes saved to the db, and the new content in the
preview area and in the form.
What happens is that the entire "content" section of the page vanishes.
Everything. Boom. Poof. Bye-bye. It's as if the sub that catches the
button action simply doesn't fire. And when I refresh the page, the
original content - not the changed content - comes back up, *proving*
that the database insert of the changed content FAILED TO FIRE.
Below is the code-behind that I am using:
Imports System
'Imports System.Configuration
Imports System.Data
Imports System.Data.OleDb
'Imports System.Drawing
'Imports System.Drawing.Imaging
'Imports System.IO
'Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Public Class TeamMetz
Inherits Page
Dim vbCrLf As String = Microsoft.VisualBasic.vbCrLf
Dim strYear As String = DateTime.Now.Year.ToString()
Dim strWarning As String = vbCrLf & " <p>Have you <a
href=""/rules.aspx"">read the rules</a> for adding content? Do you know
how to <a href=""/formatting.aspx"">properly format text</a>?</p>" & vbCrLf
Dim myForm As New HTMLForm
Dim myConn As New
OleDbConnection(System.Configuration.Configuration Settings.AppSettings("strConn"))
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim strURL as String = Request.RawURL.ToString()
Dim strDO as String = Request.QueryString("do")
Dim intID as Integer = Request.QueryString("id")
Dim strP as String = Request.QueryString("parent")
Dim strC as String = Request.QueryString("child")
Dim strG as String = Request.QueryString("gchild")
Dim intE as Integer = Request.QueryString("entry")
Controls.Add(New LiteralControl(" ... ")) 'sets up the opening
HTML, the meta tags and the page header.
If Not IsPostBack Then
Select Case strDo
Case "add"
Case "edit"
Case "delete"
Case "intro"
LoadIntro()
Case Else
Controls.Add(New LiteralControl(" <h2>Welcome</h2>" & strWarning & "
<p>Please choose a task from the following list:</p>" & vbCrLf & " <ul>"
& vbCrLf & " <li><a href=""/default.aspx?do=add"">Add Data</a> to the
database</li>" & vbCrLf & " <li><a href=""/default.aspx?do=edit"">Edit
Data</a> in the database</li>" & vbCrLf & " <li><a
href=""/default.aspx?do=intro"">Edit Intro</a> on the front page</li>" &
vbCrLf & " </ul>"))
End Select
End If
Controls.Add(New LiteralControl(" ... ")) 'sets up the page footer
and the closing HTML
End Sub
Sub LoadIntro()
Controls.Add(New LiteralControl(" <h2>Edit Intro</h2>" & strWarning))
myForm.ID = "myForm"
Controls.Add(myForm)
Dim myCmd as New OleDbCommand("SELECT [Comment] FROM [tblIntro]",
myConn)
myConn.Open()
myForm.Controls.Add(New LiteralControl(" <p>This is what is
currently in the Database:</p>" & vbCrLf & " <blockquote>" & vbCrLf & "
<p>" & FormatData(myCmd.ExecuteScalar()) & "</p>" & vbCrLf & "
</blockquote>" & vbCrLf & " <p>Use the form below to edit the contents
of the database and submit the changes:</p>" & vbCrLf & " <div
class=""center"">" & vbCrLf & " "))
Dim textarea As New TextBox
textarea.id = "IntroComment"
textarea.Text = myCmd.ExecuteScalar()
textarea.Rows = "8"
textarea.Columns = "80"
textarea.TextMode = TextBoxMode.MultiLine
textarea.Wrap = true
myForm.Controls.Add(textarea)
myForm.Controls.Add(New LiteralControl("<br />" & vbCrLf & " "))
Dim submit As New Button
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
myForm.Controls.Add(submit)
myForm.Controls.Add(New LiteralControl(vbCrLf & " </div>" & vbCrLf))
myConn.Close()
End Sub
Sub UpdateIntro(sender As Object, e As System.EventArgs)
Dim myCmd as New OleDbCommand("UPDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(Request.Form("IntroComment"))
myCmd.ExecuteNonQuery()
myConn.Close()
LoadIntro()
End Sub
Function FormatDate(sItem as Object) as String
Return String.Format("<p class=""small"">Posted: <i>{0:dddd, dd
MMMMM, yyyy}</i></p>", sItem)
End Function
Function FormatData(sItem) as String
FormatData=sItem.Replace(vbcrlf,"</p>" & vbCrLf & "
<p>").Replace("","<b>").Replace("","</b>").Replace("","<i>").Replace("","</i>").Replace("</p>"
& vbCrLf & " <p>
- </p>" & vbCrLf & " <p>[item]","</p>" & vbCrLf & "
<ul>" & vbCrLf & " <li>").Replace("</p>" & vbCrLf & "
<p>[item]","</li>" & vbCrLf & " <li>").Replace("</p>" & vbCrLf & "
<p>
End Function
Function RepChar(sItem) as String
RepChar=sItem.Replace("'","’").Replace(" & ", " &
").Replace("<","<").Replace(">",">")
End Function
End Class
As you can see, the *logical* progression of events is that the page is
loaded, the user clicks on "edit intro", the edit intro page comes up
(complete with preview area and pre-filled form). The user then makes
changes, and the button gets clicked.
The button *should* call UpdateIntro(), but it clearly does not, since
the DB never gets updated. WHY? WHY? WHY? Why does the DB not update???
The LoadIntro() clearly has the button referencing the UpdateIntro()
sub, but that sub fails to fire when the page gets submitted. It's like
that sub is completely ignored.
WHY?
TIA.
...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.” *
* - Stephen F. Roberts *
************************************************** *********************
* “Anyone who believes in Intelligent Design (“creationism”) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.” - 99.99+% of Scientists *
************************************************** *********************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
************************************************** *********************