关于ASP.Net回发的几个问题 [英] Some questions about ASP.Net Postback

查看:178
本文介绍了关于ASP.Net回发的几个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有这应该有一个默认值的文本框....,它是在像这样的初始化函数:

Lets say I have a text box which should have a default value.... and it is in an initializing function like so:

void InitializeControls()
{
    myTextBox.Text = "Default Text";
}

现在,让我们说,我有一个按钮,它不回发...我要过的地方保存文本框的用户输入的值按钮的onclick事件。

Now, lets say that I have a button, which does a postback... I want to save the user entered value of the textbox off somewhere in the button's OnClick event.

我的问题是,当我应该叫初始化控制code以上?我想它应该是在onload功能,但是这似乎是我将覆盖回发数据,每次:

My question is, when should I call the initializing control code above? I'm thinking it should be in the OnLoad function, however this seems like that I will overwrite the postback data everytime:

protected override void OnLoad(EventArgs eventArgs)
{
    base.OnLoad(eventArgs);

    InitializeControls();
}

将回发数据覆盖上面,如果我有初始化code在onload我的默认的文字?

Will the postback data overwrite my default text above if I have the initializing code in the OnLoad?

感谢

推荐答案

呼叫 InitializeControls()的OnInit ,调用 base.OnInit前()。这样,您的默认值不会ViewState中的一部分,你会不会把它传递给客户和背部没有任何理由。 你也可以只设置在加价的默认值(在.aspx文件)。

Call InitializeControls() in the OnInit, before the call to base.OnInit(). In this way your default value will not be part of the ViewState and you will not be passing it to the client and back for no reason. You could also just set the default value in the mark-up (in the .aspx file).

你的没有的通话InitializeControls()在pageLoad的/的OnLoad,因为这将增加的默认值的ViewState,腹胀的ViewState没有理由。

Do not call InitializeControls() in PageLoad / OnLoad since this will add the default value to ViewState, bloating ViewState for no reason.

阅读文章真正了解的ViewState 获得这个东西一个很好的了解。

Read the article TRULY understanding ViewState to get a good understanding of this stuff.

这篇关于关于ASP.Net回发的几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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