在文本框问题2中显示默认文本。 [英] Display default text in textbox issue 2.

查看:64
本文介绍了在文本框问题2中显示默认文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上的文本框的默认值存在问题。页面加载文本框时,所有文本框中都包含零。当用户输入数据并单击提交时,将保存数据并重新加载页面,但文本框为空。我做错了什么?我将文本框属性Text设置为0。



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:TextBox ID="TextBoxTNUGSC" runat="server" Width="180px" Text= "0"></asp:TextBox>





代码背后提交按钮:





Code Behind for the Submit Button:

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;
            
        }

推荐答案

问题



TextBoxes 被删除,因为你是通过 Button Click Event 中的以下代码制作的。

Problem

The TextBoxes are blanked out because you are making them by the following code inside Button Click Event.
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;



当你点击按钮,它回发并点击页面加载事件并默认加载所有控件。所以,此时,您的 TextBox 值将为0.但之后它会点击按钮单击事件并在此事件中您正在制作它们空白。



所以,他们被淘汰了。



我希望你删除这些行,那些 TextBoxes 会将默认值加载为0. :)(


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. :)


得到电流 - 文本框文本值-IN-A-按钮的onclick事件-ASP-净 [ ^ ]

get-text -from-asptextbox [ ^ ]


请尝试添加在页面顶部的页面注册标记中将EnableViewState设置为true。如果您使用了更新面板,请将所有控件放在单个更新面板中,如TextBox和Save Button。
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.


这篇关于在文本框问题2中显示默认文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆