[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

ASP.Net form error - Invalid postback or callback argument.

I found a simple contact form in asp.net on the web and I am trying to implement on my site... things are not going well. I have the form in an aspx file and the sub in a code behind file. Please see the code below:

I get the following error message when I hit submit:
Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Form code:
        <form id="contact">
          <h3>Name:</h3>
          <asp:textbox id="strName" runat="server" />
          <br><br>
 
          <h3>Email Address:</h3>
          <asp:textbox id="strEmail" runat="server" />
           <br><br>
 
          <h3>Subject:</h3>
          <asp:textbox id="strSubject" runat="server" Columns="55"/>
           <br><br>
 
           <h3>Your Message</h3>
          <asp:textbox id="strYourMsg" runat="server" Columns="45" Rows="10" TextMode="MultiLine" />
            <br /><br />
          <asp:button runat="server" id="func" class="btn" Text="Send Message" OnClick="Send2Mail" />
          <br /><br />
        </form>
 
Code in the page.aspx.vb file:
Imports System.Web.Mail
 
Partial Class contact_Default
    Inherits System.Web.UI.Page
 
    Sub Send2Mail(ByVal sender As Object, ByVal e As EventArgs)
 
        Dim objMail As New MailMessage()
 
        objMail.To = "testmail@gmail.com"
        objMail.From = strEmail.Text
 
        objMail.BodyFormat = MailFormat.Text
        objMail.Priority = MailPriority.Normal
        objMail.Subject = strSubject.Text
 
        objMail.Body = "Name : " + strName.Text + vbNewLine + "Email : " + strEmail.text + vbnewLine + "Message : " + strYourMsg.text
 
        SmtpMail.SmtpServer = "mail.test.com"
        SmtpMail.Send(objMail)
 
 
        strMessage.Visible = True
 
    End Sub
End Class

Open in new window

0
eadam-uk
Asked:
eadam-uk
  • 2
1 Solution
 
sunithnairCommented:
Try this add EnableEventValidation="false" in your page header or try declaring form as runat="server"
Form code:
<%@ Page EnableEventValidation="true" %>
        <form id="contact">
          <h3>Name:</h3>
          <asp:textbox id="strName" runat="server" />
          <br><br>
 
          <h3>Email Address:</h3>
          <asp:textbox id="strEmail" runat="server" />
           <br><br>
 
          <h3>Subject:</h3>
          <asp:textbox id="strSubject" runat="server" Columns="55"/>
           <br><br>
 
           <h3>Your Message</h3>
          <asp:textbox id="strYourMsg" runat="server" Columns="45" Rows="10" TextMode="MultiLine" />
            <br /><br />
          <asp:button runat="server" id="func" class="btn" Text="Send Message" OnClick="Send2Mail" />
          <br /><br />
        </form>
 
Code in the page.aspx.vb file:
Imports System.Web.Mail
 
Partial Class contact_Default
    Inherits System.Web.UI.Page
 
    Sub Send2Mail(ByVal sender As Object, ByVal e As EventArgs)
 
        Dim objMail As New MailMessage()
 
        objMail.To = "testmail@gmail.com"
        objMail.From = strEmail.Text
 
        objMail.BodyFormat = MailFormat.Text
        objMail.Priority = MailPriority.Normal
        objMail.Subject = strSubject.Text
 
        objMail.Body = "Name : " + strName.Text + vbNewLine + "Email : " + strEmail.text + vbnewLine + "Message : " + strYourMsg.text
 
        SmtpMail.SmtpServer = "mail.test.com"
        SmtpMail.Send(objMail)
 
 
        strMessage.Visible = True
 
    End Sub
End Class

Open in new window

0
 
sunithnairCommented:
Or form like this
Form code:
        <form id="contact" runat="server">
          <h3>Name:</h3>
          <asp:textbox id="strName" runat="server" />
          <br><br>
 
          <h3>Email Address:</h3>
          <asp:textbox id="strEmail" runat="server" />
           <br><br>
 
          <h3>Subject:</h3>
          <asp:textbox id="strSubject" runat="server" Columns="55"/>
           <br><br>
 
           <h3>Your Message</h3>
          <asp:textbox id="strYourMsg" runat="server" Columns="45" Rows="10" TextMode="MultiLine" />
            <br /><br />
          <asp:button runat="server" id="func" class="btn" Text="Send Message" OnClick="Send2Mail" />
          <br /><br />
        </form>
 
Code in the page.aspx.vb file:
Imports System.Web.Mail
 
Partial Class contact_Default
    Inherits System.Web.UI.Page
 
    Sub Send2Mail(ByVal sender As Object, ByVal e As EventArgs)
 
        Dim objMail As New MailMessage()
 
        objMail.To = "testmail@gmail.com"
        objMail.From = strEmail.Text
 
        objMail.BodyFormat = MailFormat.Text
        objMail.Priority = MailPriority.Normal
        objMail.Subject = strSubject.Text
 
        objMail.Body = "Name : " + strName.Text + vbNewLine + "Email : " + strEmail.text + vbnewLine + "Message : " + strYourMsg.text
 
        SmtpMail.SmtpServer = "mail.test.com"
        SmtpMail.Send(objMail)
 
 
        strMessage.Visible = True
 
    End Sub
End Class

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now