Page.IsPostBack 属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cabxyz/article/details/558908

Page.IsPostBack 属性

获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问。

[Visual Basic]
Public ReadOnly Property IsPostBack As Boolean
[C#]
public bool IsPostBack {get;}
[C++]
public: __property bool get_IsPostBack();
[JScript]
public function get IsPostBack() : Boolean;

属性值

如果是为响应客户端回发而加载该页,则为 true;否则为 false

Page.EnableViewState 属性

获取或设置一个值,该值指示当前页请求结束时该页是否保持其视图状态以及它包含的任何服务器控件的视图状态。

[Visual Basic]
Overrides Public Property EnableViewState As Boolean
[C#]
public override bool EnableViewState {get; set;}
[C++]
public: __property bool get_EnableViewState();
public: __property void set_EnableViewState(bool);
[JScript]
public override function get EnableViewState() : Boolean;
public override function set EnableViewState(Boolean);

属性值

如果该页保持其视图状态,则为 true;否则为 false。默认值为 true

Page.ErrorPage 属性

获取或设置错误页,在发生未处理的页异常的事件时请求浏览器将被重定向到该页。

[Visual Basic]
Public Property ErrorPage As 
[C#]
public  ErrorPage {get; set;}
[C++]
public: __property * get_ErrorPage();
public: __property void set_ErrorPage(*);
[JScript]
public function get ErrorPage() : ;
public function set ErrorPage();

属性值

浏览器被重定向到的错误页。

[C#]
void Page_Load(Object sender, EventArgs e)
{
   // Note: This property can also be set in <%@ Page ...> tag.
   if(!IsPostBack)
      this.ErrorPage = "Error_Page.aspx";
}

Page.IsValid 属性

获取一个值,该值指示页验证是否成功。

[Visual Basic]
Public ReadOnly Property IsValid As 
[C#]
public  IsValid {get;}
[C++]
public: __property  get_IsValid();
[JScript]
public function get IsValid() : ;

属性值

如果页验证成功,则为 true;否则为 false

备注

若要使该属性返回 truePage.Validators 属性中的所有验证服务器控件必须都验证成功。只有在已调用 Page.Validate 方法,或已在开始窗体处理的 ASP.NET 服务器控件的 OnServerClick 处理程序中将 CausesValidation 属性设置为 true 后才可以选中该属性。这些服务器控件包括 ButtonHtmlButtonHtmlInputButtonHtmlInputImageImageButton 以及 LinkButton 类。

Page.Visible 属性

获取或设置指示是否呈现 Page 对象的值。

属性值

如果将呈现 Page,则为 true;否则为 false。默认值为 true

示例

[Visual Basic, C#] 下面的示例将 Page.Visible 属性设置为假,以隐藏页的呈现内容。在首次请求该页时,此页中将填充并将显示数据源。当用户单击按钮时,HideButton_Click 事件处理程序将会隐藏页的全部呈现内容。

[Visual Basic] 
Sub HideButton_Click(Sender As Object, er As EventArgs)

   Me.Visible = false

End Sub

[C#] 
void HideButton_Click(Object sender, EventArgs er)
{      
   this.Visible = false;
}
阅读更多

If Not Page.IsPostBack 如果没有Page.IsPostBack

04-01

If Not Page.IsPostBackrnUncovering the Mysteryrn揭开他的神秘面纱rn________________________________________rnMany of you have seen this mysterious if/then statement in a lot of the online and book code samples, and you probably have, at one time or another, wondered just what exactly this was all about. This tutorial plans to answer all your questions. There is one basic question that gets asked on ASP.Net Forums and ASPFriends.com ListServes, over and over, in a several different ways. It all boils down to one answer concerning the IsPostBack Property of the page. rn很多朋友在网上和参考书中见过这个有点神秘的if/then的判断,也许有那么一次,有人会想知道它究竟讲些什么,本教程会为我们解开疑云。rn在asp.net论坛和asp之友的网站上,有一个问题很常见,他以各种形式出现,归结起来就是页面的IsPostBack属性值rn"Why doesn't my DropDownList keep it's Selection?"rn"Why is the selectedindex for my (ASP.Net control) always turning up a -1?" rn “为什么页面的下拉菜单不能保留选项值?”rn“为什么页面的控件值总是指向-1?”rnFortunately, today, you've come to the right place. You questions will be answered. rn幸运的是,今天你找对的地方。这里有所有问题的答案。rnScenario:rnYou put a DropDownList or a ListBox (or just about any ASP.Net control which has multiple items assigned to it) on your web page (inside a form, naturally). Then, at some point, you either populate the list items manually, or bind it to a database table. However, you do it, you get a list of items from which, at some point, the end user can make a choice. Based on that choice, the end user gets more data in return. Most of the basic item population of these controls is done during the initial loading of the page (Page_Load event). That way, the list items are available for choosing once the page is finished loading. rn摘要:rn我们在web页面上(当然是在form中)放了一个DropDownList或者ListBox(或者是任何具有多重值的ASP.NET控件).接下来,或者手动添加值,或者绑定数据库表,反正你得到了可供用户选择的选项。基于上述选择,终端用户可以得到更多的数据。多数时候,我们在初始化页面时(即Page_Load事件)就完成了选项值的填充。这样,一旦页面下载完成时,列表项即可供选择。rnrnLet's say, then, you also put a button on the page. That way, the end user can choose an item in the list, click the button and get the extended data, based on the selection made. The button's click event would then take the item which was selected and use it in a click event that could then, possibly connect to a database and use the selected item's data to filter a query against a database.rn 比方说,我们在网页上再放上一个按钮。这样,终端用户可以选择列表项上的一个选项值,点击按钮然后基于已有的选择得到扩展数据。点击按钮事件会捕捉到我们勾选的选项值,然后或许连接数据库,用这个选项值做出对数据库的筛选操作。rn"Of course", you say - "Simple", you say - "No Problem!", you say - even "Why are you bothering me with this drivel?" This is where it all boils down to the mystery for which this tutorial was created. Here it is - your DropDownList was populated with the last names of people at your company. You create a sub procedure for the onclick event of the button to connect to the database and give the end user back all the available data in the table, based on that last name. The SQL statement goes something like this:rn或者你会说:“这当然”,或者你会说“这简单”,或者你会说“有什么难的”甚至你也许会说:“不要对我一派胡言!”创作这篇教程正是归结于这种神秘感。接下来,你的DropDownlist将会填上你同事的姓氏。我们写下来单击按钮事件的主程序,连接数据库得到基于姓氏的所有相关数据,SQL语句如下:rnSQL = "Select * from Employees where lastname = '" & DropDownList.SelectedItem.Text & "'"rnHowever, when you do this, you either get an error, or you get absolutely no results back from the query. You're sure the query is correct - but you can't figure out why this isn't returning any records. rn但一旦当我们这样做了,我们或者得到了一个错误的数据,或者我们压根儿啥也没查到,我们可以确定查询方法是对的,却解释不了为什么没有任何的查询结果。rnrnThat's because you didn't surround the data population of your control with the basic statement this tutorial is all about:rn 那是因为我们没有用这篇教程当中所讲的基本声明语句将我们控件的初始化语句包裹起来rnIf not Page.IsPostback then rnEnd IfrnrnWhat's happening is that your page is re-loading the data into your server control - it's not responding in any way, to the click event from the button click. First of all, you would never need the server control to re-populate EVERY time the page loaded. That would be terribly inefficient and as you have seen here - it makes making a choice from the control pretty much unuseable. ASrn所发生的事情是,页面把服务器端控件的数据重新加载了一遍,他没有任何包括按钮点击事件反应。第一,我们不需要每次加载页面的时候都重新载入服务器端控件的值,那将低效至极,而且我们也看到了,这样的话我们没法子做出选择。rnAs I said at the beginning, there are a couple of ways to populate the data items of a server control (DropDownList, ListBox, RadioButtonList, CheckBoxList, etc) - manually and binding the results from a database query to it. It doesn't matter how you populate the data. What DOES matter is that you include the population of the control within the boundaries of this If/Then Statement. rn就像我一开始说过的,有很多种方式为我们的服务器端控件赋值(DropDownList, ListBox, RadioButtonList, CheckBoxList, 等等),或为手动,或通过数据库绑定。但怎样为控件赋值并不重要,真正重要的是你是否把控件放入这个If/Then声明之内。rnTwo things will happen then. The first thing that will happen, is that, on each subsequent loading of the page, the control doesn't go through the redundant action of re-loading the control. It loads it only once, at the initial page load. Once the page is loaded, the click event from the button (or however you post back) is considered to be a postback. Yes, the page gets reloaded, but, by surrounding your data population with this statement, the page knows that the initial data loading was already done, so it doesn't happen any more, and it gets maintained throughout any subsequent post back. The second, and most rewarding thing that happens is that the selection which was made by the end user is maintained throughout the post back. This is the pay-off - by doing it with the If/Then Page.IsPostBack statement, your selection carries through to your query and the query returns the results you wanted in the first place. rnrn接下来发生两件事情。首先将要发生的是,每一次页面载入,控件只需在初始化时加载一次,不需要多余的重复加载。一旦页面载入操作完成,由按钮触发的事件(或者其他的需要postback的事件)就会当做是一个postback。是的,页面被重新加载了,但是通过把数据包含在if/then即判断ispostback语句中,页面便知道了控件的数据加载已经完成了,所以加载事件不会再发生,这种状态将会一直贯穿任何的重复的回发事件。第二件事,也是最有意义的事情就是终端用户做出的任何选择都会一直维持在选择的状态。这一切都归功于我们应用了If/Then Page.IsPostBack声明,我们通过选项进行查询,而查询返回的结果我们想置于首位。rnExamples:rnIf Not Page.IsPostBack thenrn Dim strConn as string = "server=(local);uid=UID;pwd=PWD;database=Northwind"rn Dim MySQL as string = "Select LastName from Employees"rn Dim MyConn as New SQLConnection(strConn)rn Dim objDR as SQLDataReaderrn Dim Cmd as New SQLCommand(MySQL, MyConn)rn MyConn.Open()rn objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)rn DropDownList.DataSource = objDRrn DropDownList.DataBind()rnEnd Ifrnrn例子:rnIf Not Page.IsPostBack thenrn Dim strConn as string = "server=(local);uid=UID;pwd=PWD;database=Northwind"rn Dim MySQL as string = "Select LastName from Employees"rn Dim MyConn as New SQLConnection(strConn)rn Dim objDR as SQLDataReaderrn Dim Cmd as New SQLCommand(MySQL, MyConn)rn MyConn.Open()rn objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)rn DropDownList.DataSource = objDRrn DropDownList.DataBind()rnEnd IfrnrnrnBy doing it this way, you will always be able to maintain the selection from your DropDownList (or other ASP.Net Control) through the postback events of your pages.rnrn通过这样做,贯穿整个页面的postback事件始终,你都可以一直继续保持DropDownList的选项。

-------诚心求帮助----if (!Page.IsPostBack)

08-18

从网上下载了DropDownList 省市区无刷新三级联动源码,可以正常显示,但增加提交数据按钮后就出现提示:rn回发或回调参数无效。在配置中使用 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。rnrn按照网上的方法,想采用在Page_Load中加rn if (!Page.IsPostBack)rn rn 代码rn rn的方法解决,[b][color=#0000FF]请教这个绑定代码怎么写?[/color] [/b]rn附源码部分(还有dll等,不知道怎么上传)rnrnrnDefault.aspx文件里面:rn<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %> rn rn rn rn 51aspx省市区县(含完整数据库)无刷新联动 rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrnrnDefault.aspx.cs文件:rnrnnamespace WebApplication1rnrn public partial class WebForm1 : System.Web.UI.Pagern rn protected void Page_Load(object sender, EventArgs e)rn rnrn rn rn protected void Button1_Click(object sender, EventArgs e)rn rn if (TextBox2.Text == string.Empty)rn rn Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('请把……!');", true);rn rn elsern ……rn rn protected void Button2_Click(object sender, EventArgs e)rn rn Response.Write(" ");rn this.Dispose();rn rnrnrnSNWebService.cs文件:rnusing System;rnusing System.Web;rnusing System.Collections;rnusing System.Web.Services;rnusing System.Web.Services.Protocols;rnusing System.Web.Script.Services;rnusing System.Collections.Generic;rnusing System.Collections.Specialized;rnusing AjaxControlToolkit;rnusing System.Data.SqlClient;rnusing System.Data;rnrn/// rn/// SNWebService 的摘要说明rn/// rn[WebService(Namespace = "http://tempuri.org/")]rn[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]rn[ScriptService]rn//该源码rnrnpublic class SNWebService : System.Web.Services.WebServicernrn public SNWebService()rn rn //InitializeComponent(); rn rn /// rn /// 获取1级数据rn /// rn /// rn /// rn /// rn ////-------------------地址-------------------rn [WebMethod(EnableSession = true)]rn public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category)rn rn List provinceList = new List();rn string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];rn SqlConnection sqlConn = new SqlConnection(connectionString);rn sqlConn.Open();rn string strSql = "Select * From Province";rn SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);rn SqlDataReader dtrProvince = sqlCmd.ExecuteReader();rn while (dtrProvince.Read())rn rn provinceList.Add(new CascadingDropDownNameValue(dtrProvince["Name"].ToString(), dtrProvince["Code"].ToString()));rn rnrn dtrProvince.Close();rnrn sqlConn.Close();rnrn return provinceList.ToArray();rn rn /// rn /// 获取2级数据rn /// rn /// rn /// rn /// rn [WebMethod]rn public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category)rn rn StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);rn string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];rn SqlConnection sqlConn = new SqlConnection(connectionString);rn sqlConn.Open();rn string strSql = "Select * From city Where ProvinceId='" + provinceList["Province"] + "'";rn SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);rn SqlDataReader dtrCity = sqlCmd.ExecuteReader();rn List cityList = new List();rn while (dtrCity.Read())rn rn cityList.Add(new CascadingDropDownNameValue(dtrCity["Name"].ToString(), dtrCity["code"].ToString()));rn rn dtrCity.Close();rn return cityList.ToArray();rn rn /// rn /// 获取3级数据rn /// rn /// rn /// rn /// rn [WebMethod]rn public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category)rn rn StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);rn string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];rn SqlConnection sqlConn = new SqlConnection(connectionString);rn sqlConn.Open();rn string strSql = "Select * From area Where CityId='" + cityList["City"] + "'";rn SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);rn SqlDataReader dtrViliage = sqlCmd.ExecuteReader();rn List viliageList = new List();rn while (dtrViliage.Read())rn rn viliageList.Add(new CascadingDropDownNameValue(dtrViliage["Name"].ToString(), dtrViliage["id"].ToString()));rn rn dtrViliage.Close();rn return viliageList.ToArray();rn rnrnrnrnrn

没有更多推荐了,返回首页