Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在webform asp.net中多次更新文本框值_C#_Asp.net_Textbox_Concatenation_String Concatenation - Fatal编程技术网

C# 在webform asp.net中多次更新文本框值

C# 在webform asp.net中多次更新文本框值,c#,asp.net,textbox,concatenation,string-concatenation,C#,Asp.net,Textbox,Concatenation,String Concatenation,我正在尝试用字符串更新文本框。字符串将被连接,然后将使用单击事件更新文本框。我可以在windows应用程序中执行此操作,但当我尝试在asp.net应用程序中执行此操作时,我无法获得想要的结果 public partial class WebForm3 : System.Web.UI.Page { public string string_punch; protected void Page_Load(object sender, EventArgs e) {

我正在尝试用字符串更新文本框。字符串将被连接,然后将使用单击事件更新文本框。我可以在windows应用程序中执行此操作,但当我尝试在asp.net应用程序中执行此操作时,我无法获得想要的结果

public partial class WebForm3 : System.Web.UI.Page
{
    public string string_punch;
    protected void Page_Load(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View1);            
        txt_punch.MaxLength = 4;
        txt_punch.Attributes.Add("OnChange", string_punch);            
    }

    protected void btn_punch_7_Click(object sender, EventArgs e)
    {
        const string string_punch_Number_7 = "7";
        string_punch = string_punch + string_punch_Number_7;
        txt_punch.Attributes.Add("Value", string_punch);
    }

    protected void btn_punch_8_Click(object sender, EventArgs e)
    {
        const string string_punch_Number_8 = "8";
        string_punch = string_punch + string_punch_Number_8;
        txt_punch.Attributes.Add("Value", string_punch);
    }

我希望能够单击btn_punch_7,然后单击btn_punch_8,连接字符串,并用这两个数字更新文本框。每次单击按钮时,字符串都会设置为null。感谢advance的帮助。

string\u punch
在每次
PostBack
之间都会丢失,这就是为什么它总是等于
null
,因为ASP.NET是无状态的,这意味着它不会将状态从post back保留到post back。还可以使用
TextBox
Text
属性来分配/检索TextBox的值

根据以下代码相应地更改事件:

protected void btn_punch_7_Click(object sender, EventArgs e)
{
   const string string_punch_Number_7 = "7";
   var text = txt_punch.Text;
   text += string_punch_Number_7

   txt_punch.Text = text;
}

会话中存储
字符串\u punch
,因此将此代码添加到加载:

if (Session["string_punch"] == null)
{
    // this will only happen ONE TIME per session
    Session["string_punch"] = "your INITIAL static value";
}
string_punch = Session["string_punch"];
现在,
string\u punch
的值将在回邮之间保留。代码中发生的事情是在回发过程中重建页面时,
publicstring\u punch正在重新定义此变量

现在,每次单击后,只需添加此行:

Session["string_punch"] = string_punch;

每次都将属性值添加到元素中。与其那样做,不如这样做

txt_punch.Text = string_punch;

成功了!谢谢,我正要把头发拔出来。我想这就是导致它的原因,但我不知道如何摆脱它。我有一种感觉,这不是asp.net最后一次让我困惑。我对此也很好奇。我也要试试这个。谢谢