soft_coder 0 Light Poster

Hi! I have created an asp.net page with codebehind in Sharepoint. The code is as follows:

The ASPX page:

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddToListv2.aspx.cs" Inherits="TestAppPage.Layouts.TestAppPage.AddToListv2,TestAppPage,Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef990c799011c0a9" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>

<form id="form1" runat="server">
<table id="table1">
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Name :"></asp:Label></td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Cannot be Empty" ControlToValidate="txtName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="Email ID :"></asp:Label></td>
<td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Cannot be Empty" ControlToValidate="txtEmail"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><asp:Label ID="Label3" runat="server" Text="Message :"></asp:Label></td>
<td><asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Cannot be Empty" ControlToValidate="txtMessage"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/></td>
</tr>
</table>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<asp:label ID="lblError" runat="server"></asp:label>
</form>
</body>

</html>

The ASPX.CS page:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace TestAppPage.Layouts.TestAppPage
{
    public partial class AddToListv2 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Visible = false;
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Feedback"];
                SPListItem lstItem = list.AddItem();
                lstItem["Username"] = txtName.Text;
                lstItem["EmailID"] = txtEmail.Text;
                lstItem["Message"] = txtMessage.Text;
                lstItem.Update();
                list.Update();
                txtName.Text = "";
                txtMessage.Text = "";
                txtEmail.Text = "";
                lblMessage.Visible = true;
                lblMessage.Text = list.Title.ToString();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }
    }
}

Currently I am facing two problems:
1. On page refresh the form is retaining the data.
2. On page refresh the event btnSubmit_Click() is getting fired automatically.

Please Help!:S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.