开发者

C#.NET form submit unable to get new/modified values for form elements

I am creating a ASP.NET page in C# to add/modify details and then save those details in MySQL database. For this I have created a master page "Main.Master" a web content form "modify.aspx" and its code behind. Code for "Main.Master" is as given below. Master contains form tag declaration.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.master.cs" Inherits="WebApplication.Main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="mainmasterform" method="post" runat="server">
        <div id="container">
            <div id="maincontent">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </div>
        </div>
    </form>
</body>
</html>

Code for "modify.aspx" is as given below. Only form elements are present in this page.

<%@ Page Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="modify.aspx.cs" Inherits="WebApplication.modify" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table align="center">
    <tr>
        <td><label for="ownfname">First Name</label></td>
        <td><asp:TextBox id="ownfname" runat="server" TextMode="SingleLine"/></td>
    </tr>
    <tr>
        <td><label for="ownlname">Last Name</label></td>
        <td><asp:TextBox id="ownlname" runat="server" TextMode="SingleLine"/>&l开发者_运维技巧t;/td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="submitowner" runat="server" Text="Submit" onclick="modifyDetails" />
        </td>
    </tr>
</table>
</asp:Content>

On click of "submitowner" button submits "modify.aspx" to a controller method "modifyDetails" present in "modify.aspx.cs" code behind file. "modifyDetails" code is as follows.

protected void modifyDetails(object sender, EventArgs e) {
        string cmdstr = "UPDATE owners SET first_name=?, last_name=? WHERE userid=?";
        OdbcConnection odbccon = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQL55"].ConnectionString);
        OdbcCommand odbccmd = new OdbcCommand(cmdstr, odbccon);
        string fname = ownfname.Text;
        string lname = ownlname.Text;
        odbccmd.Parameters.Add("@fname", OdbcType.NVarChar, 50).Value = fname;
        odbccmd.Parameters.Add("@lname", OdbcType.NVarChar, 50).Value = lname;
        odbccmd.Parameters.Add("@userid", OdbcType.Int).Value = userid;
        odbccon.Open();
        odbccmd.ExecuteScalar();
    }

Now I am facing trouble in getting new values of form elements. As for example when the web page is rendered text within "ownfname" is "Robert" and then you modify it to "David" then click on submit button. After submitting accessing the "ownfname.Text" property still gives "Robert" and not "David". I tried searching for a solution but no luck.

I am not able to understand what is the exact problem here. Is the code wrong? Can anyone help me figure out the issue/problem ? I apologize for such a long question.


If you are loading the initial values into your form fields on page load you need to use

protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
        //Populate form fields
     }
}

This will ensure that the fields are not overwritten on post back.

If you're not overly familiar with the ASP.net page life cycle this article is well worth the read. It explains the order of events when an asp.net page is loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜