forum

Home / DeveloperSection / Forums / Why Data is not posted in asp.net using postback?

Why Data is not posted in asp.net using postback?

Anonymous User150205-Oct-2014
I'm trying to post data to the same page after postback with the help of ViewState. I fill out the contents of the page and post the form and display the input on the same page but the validator says that the firstname cannot be null while I just filled out the textbox before I submitted the form.
<asp:ContentContentPlaceHolderID="Main"runat="server"EnableViewState="true">
    <formid="form1"runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:LabelID="lbl1"Text="First Name"runat="server"/>
                </td>
                <td>
 
                <asp:TextBoxID="tFirstName"runat="server"/>
            </td>
            <td>
                <asp:RequiredFieldValidatorControlToValidate="tFirstName"ErrorMessage="This field cannot be empty."
                    runat="server"/>
            </td>
        </tr>
        <tr>
            <td>
                <asp:LabelID="lbl2"Text="Last Name"runat="server"/>
            </td>
            <td>
                <asp:TextBoxID="tLastName"runat="server"/>
            </td>
        </tr>           
    </table>
</div>
<asp:ButtonID="id"Text="Submit"runat="server"OnClick="Submit"/>
<div>
    <p>
        <asp:LabelID="lTest"runat="server"/></p>
</div>
<asp:LabelID="lSubmit"runat="server"/>
</form>


.cs code:

protectedvoid Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                tFirstName.Text = (string)ViewState["tFirstName"];
                tLastName.Text = (string)ViewState["tLastName"];
            }
            else
            {
                ViewState["tFirstName"] = tFirstName.Text;
                ViewState["tLastName"] = tLastName.Text;
            }
        }
 
        protectedvoid Submit(object sender, System.EventArgs args)
        {
            try
            {
                lTest.Text = tFirstName.Text + " " tLastName.Text;
            }
            catch (Exception ex)
            {
 
                throw;
            }
        }


Updated on 05-Oct-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By