Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue with my default value for the textboxes on the form. When the page loads the textboxes all have zeros in them. When a user enters the data and clicks submit the data is saved and the page reloads but the textboxes are empty. What did I do wrong? I set the textbox property Text to "0".

ASP.NET
<asp:TextBox ID="TextBoxTNUGSC" runat="server" Width="180px" Text= "0"></asp:TextBox>


Code Behind for the Submit Button:

C#
protected void Submit_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            SqlCommand cmd = new SqlCommand("Insert into Table22 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE, TIME) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE, @TIME)Insert into Table23 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE, TIME) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE, @TIME)", con);

            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
            cmd.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
            cmd.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
            cmd.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
            cmd.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
            cmd.Parameters.AddWithValue("@BTRANSFERS", TextBoxTTOG.Text);
            cmd.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
            cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
            cmd.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
            cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
            cmd.Parameters.AddWithValue("@TIME", lblTime.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();

            if (Page.IsValid)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
            }
            else
            {
                Response.Redirect("Gradrate.aspx"); 
            }

            TextBoxUNITID.Text = string.Empty;
            TextBoxTNUGSC.Text = string.Empty;
            TextBoxTNUGSCD.Text = string.Empty;
            TextBoxTTOUG.Text = string.Empty;
            TextBoxTNGSC.Text = string.Empty;
            TextBoxTNGSCD.Text = string.Empty;
            TextBoxTTOG.Text = string.Empty;
            
        }
Posted
Updated 7-Jan-14 7:38am
v3
Comments
phil.o 7-Jan-14 10:48am    
'What did I do wrong?'
To answer this question, we would need to see your code. Please show it to us.
Computer Wiz99 7-Jan-14 10:53am    
Sorry about that. I thought I did.
Sibasisjena 7-Jan-14 10:51am    
No need to do any thing. The values will remain same after the post back
Computer Wiz99 7-Jan-14 10:53am    
No the values are gone after post back.
Sibasisjena 7-Jan-14 10:52am    
I do agree with Phil

Problem

The TextBoxes are blanked out because you are making them by the following code inside Button Click Event.
C#
TextBoxUNITID.Text = string.Empty;
TextBoxTNUGSC.Text = string.Empty;
TextBoxTNUGSCD.Text = string.Empty;
TextBoxTTOUG.Text = string.Empty;
TextBoxTNGSC.Text = string.Empty;
TextBoxTNGSCD.Text = string.Empty;
TextBoxTTOG.Text = string.Empty;

Please understand that when you click on Button, it Posts back and hits the Page Load Event and loads all the controls by default. So, at this moment, your TextBox value would be 0. But after that it hits the Button Click Event and inside this Event you are making them blank.

So, they are blanked out.

I hope if you remove these lines, those TextBoxes would load default values as 0. :)
 
Share this answer
 
Comments
Computer Wiz99 7-Jan-14 16:58pm    
Ok. What if I write the same code to have a default value? What I mean is...look at my update.
Yes, I can see you have updated the code in your answer. That would work.

Good Work. Keep coding. :)
 
Share this answer
 
v2
Please try to add EnableViewState as true in page registration tag in the top of the page. If u had use Update panel pls put all controls in a single update panel like TextBox and Save Button also.
 
Share this answer
 
v3
I would take this:

C#
TextBoxUNITID.Text = string.Empty;
TextBoxTNUGSC.Text = string.Empty;
TextBoxTNUGSCD.Text = string.Empty;
TextBoxTTOUG.Text = string.Empty;
TextBoxTNGSC.Text = string.Empty;
TextBoxTNGSCD.Text = string.Empty;
TextBoxTTOG.Text = string.Empty;


And change it to this:
C#
TextBoxUNITID.Text = "0";
TextBoxTNUGSC.Text = "0";
TextBoxTNUGSCD.Text = "0";
TextBoxTTOUG.Text = "0";
TextBoxTNGSC.Text = "0";
TextBoxTNGSCD.Text = "0";
TextBoxTTOG.Text = "0";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900