刷新文本框并在按钮单击时获取另一个值 [英] Refresh textbox and get another value on button click

查看:70
本文介绍了刷新文本框并在按钮单击时获取另一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,所以我的教授分配了这个asp.net/c#项目,我们必须创建一个密码。所以有两个文本框,左边的文本框应该是只读的,下面是获取按钮。当玩家按下获取按钮时,它应该输出加密文本(有点像凯撒密码),玩家将在右侧的文本框中输入他们的答案,然后单击右侧文本框下方的解决按钮。无论如何,我的问题是当我得到get按钮时,它不会清除以前的值,它只是继续在第一个值的末尾添加更多加密的单词。我希望文本框刷新并获得不同的值,并且每次用户单击获取按钮时仅显示一个加密文本。我怎么做?我在下面包含了我的代码并发布了问题的屏幕截图。我还没有编写解决按钮的逻辑代码所以不要担心它!



链接到图像是: http://i405.photobucket.com/albums/pp138/la_chua15/cryptoquest-game_zpsorgf3frq.jpg [ ^ ]



正如您所看到的,当我反复点击获取按钮时,会在文本框中添加更多单词。



这是我的代码!



Hey guys, so my professor assigned this asp.net/c# project where we have to create a cryptogram. So there are two textboxes, the textbox on the left is supposed to be read-only, and underneath it is the "Get" button. When a player presses the "Get" button, it's supposed to output an encrypted text (kinda like caesar cipher) and players will type in their answers in the textbox on the right and click the "Solve" button underneath the right textbox. Anyway, my problem is when I get the get button, it doesn't clear the previous value and it just continues to add more encrypted words at the end of the first one. I want the textbox to refresh and get a different value and display only one encrypted text everytime a user clicks the "Get" button. How do I do that? I included my code below and posted a screenshot of the problem. I have yet to code the logic for solve button so don't worry about it!

Link to the image is: http://i405.photobucket.com/albums/pp138/la_chua15/cryptoquest-game_zpsorgf3frq.jpg[^]

As you can see, more words get added to the textbox when I click get button repeatedly.

Here's my code!

protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnGet_Click(object sender, EventArgs e)
        {
            {
                //1. build the connection string for our SQL instance
                string connectionString = "Data Source=LA-HPENVY15;Initial Catalog=CryptoText;Integrated Security=True";

                //2. build the connection to our SQL instance

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    //open the connection
                    con.Open();

                    //string variable to hold the ouput
                    string result = String.Empty;

                    //3. Build our SQL query
                    SqlCommand cmd = new SqlCommand("SELECT TOP 1 Cipher FROM CipherText ORDER BY NEWID()", con);

                    //4. Execute the query
                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        //add the results from the SQL query to a textbox
                        result = reader["Cipher"].ToString();
                        txtGet.Text += result;
                    }

                    //5. Always close connections when you're done
                    con.Close();

                }
            }
        }

    }
}

推荐答案

那是因为你将结果连接到TextBox值。

That is because you are concatenation the result to the TextBox value.
txtGet.Text += result;



将此代码更改为...


Change this code to...

txtGet.Text = result;


尝试清除 Postback 块之外的文本框值,如下所示:

Try to clearing out the textbox value outside of Postback block as below:
protected void Page_Load(object sender, EventArgs e)
{
   txtGet.Text = "";
}


这篇关于刷新文本框并在按钮单击时获取另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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