0
votes

I have a form with textboxes and 2 buttons ‘Save’ and ‘Cancel’.

Save - stores the data to the database and clears all the fields. Cancel - clears all the fields. (I am clearing all the fields by doing a server.transfer to the same page).

I want the label to display “Course Added” after I click the Save Button and clear the form.

I have looked into View State and Session Variables but havn’t found a concrete solution to my problem as yet. Any help or advice is much appreciated.

Below is the code for my aspx and code behind. Thanks.

Admin_Course_Add.aspx

<asp:Content ID="Content3" ContentPlaceHolderID="rightNavigation" runat="server">

<script type="text/javascript">
`/`/JS for validations
  `enter code here`      $(document).ready(function () {
            $("#form1").validate({

                rules: { '<%=tbCourseName.UniqueID %>': { required: true, maxlength: 25 },
                    '<%=tbShortName.UniqueID %>': { maxlength: 10 },
                    '<%=tbPointScale.UniqueID %>': { required: true, digits: true },
                    '<%=tbDescription.UniqueID %>': { maxlength: 50 }
                },
                messages: {}
            });




            $("#imgBtn_A_add").click(function (evt) {
                // Validate the form and retain the result.
                var isValid = $("#form1").valid();

                // If the form didn't validate, prevent the
                //  form submission.

                // If the form didn't validate, prevent the
                //  form submission.
                if (!isValid)
                    evt.preventDefault();
            });

            $("#imgBtn_A_cancel").click(function () {
                $("#form1").validate().cancelSubmit = true;
                $("#form1").submit();
                return false;

                });


    </script>


    <div class="Admin_rightNavtop">


        <table style="margin: 0 auto">
            <tr>
                <td>
                    <asp:TextBox ID="tbCourseName" runat="server" class="tbSize_large"  />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="tbShortName" runat="server" class="tbSize_large" />
                </td>
            </tr>

        </table>
    </div>

    <center>
        <div class="Admin_action">
            <asp:ImageButton ID="imgBtn_A_add" ImageUrl="../Images/Add.png" runat="server" class="Admin_action_imgSize_small"
                OnClick="add_Click"  />
            <asp:ImageButton ID="imgBtn_A_cancel" ImageUrl="../Images/Cancel.png" OnClick="cancel_Click"
                 runat="server" class="Admin_action_imgSize_small"/>
        </div>
    </center>
</asp:Content>

Admin_Course_Add.aspx.cs (Code Behind)

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { dbConnection dbConn = new dbConnection(); DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection("Data Source = VC-SQL2008; Integrated Security=True; database = CORP");

                dt = Admin_Course_WebService.PopulateCourseLevel();

                ddlCourseLevel.DataSource = dt;
                ddlCourseLevel.DataTextField = "Name";
                ddlCourseLevel.DataValueField = "Id";
                ddlCourseLevel.DataBind();
            }


        }

    protected void add_Click(object sender, ImageClickEventArgs e)
    {

        DataTable dt = new DataTable();
        string name = tbCourseName.Text;
        string short_name = tbShortName.Text;

        int id = Convert.ToInt32(ddlCourseLevel.SelectedItem.Value);
        bool isDeleted = false;
        Admin_Course_WebService.AddCourse(name, short_name, graded, point_scale, id, description, isDeleted);

        Server.Transfer("Admin_Course_Add.aspx");


    }

    protected void cancel_Click(object sender, ImageClickEventArgs e)
    {
        Server.Transfer("Admin_Course_Add.aspx");
    }
1
Is this a job for a cookie? Redirecting takes viewstate out of the picture, and I don't care much for session variables. Other than that you've got cookies or querystrings...Tim
Query Strings does sound like something I need to look into. Thanks!user1288906

1 Answers

0
votes

First Why You Clear the Field By ServerTransfer??

Second You can Send Parameter in the QueryString

for example:

 Server.Transfer("Admin_Course_Add.aspx?ClearParams=Yes");

and in the page load you would add

if(Request["ClearParams"].ToString() == "Yes")
{
  Show My Lable
}