ASP.NET Page执行顺序和My97DatePicker控件服务器端输入框设置默认值
当页面进行回发时,如点击按钮,以上事件都会重新执行一次,这时的执行顺序为:
1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad
7. Button_Click
8. OnLoadComplete
9. OnPreRender
public partial class SupportDepartList: System.Web.UI.Page { static int count = 0; protected void Page_Load(object sender, EventArgs e) { Response.Write(count+ "Page_Load <br />"); count++; } protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); Response.Write(count + "OnPreInit <br />"); count++; } protected override void OnInit(EventArgs e) { base.OnInit(e); Response.Write(count + "OnInit <br />"); count++; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Response.Write(count + "OnLoad <br />"); count++; } protected override void OnPreLoad(EventArgs e) { base.OnPreLoad(e); Response.Write(count + "OnPreLoad <br />"); count++; } protected override void OnLoadComplete(EventArgs e) { base.OnLoadComplete(e); Response.Write(count + "OnLoadComplete <br />"); count++; } protected override void OnInitComplete(EventArgs e) { base.OnInitComplete(e); Response.Write(count + "OnInitComplete <br />"); count++; } protected override void OnUnload(EventArgs e) { base.OnUnload(e); } protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); Response.Write(count + "OnDataBinding <br />"); count++; } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); Response.Write(count + "OnPreRender <br />"); count++; } protected void btnGraphics_Click(object sender, EventArgs e) { //Bitmap bmp = new Bitmap(10, 10); //Graphics g = Graphics.FromImage(bmp); Response.Write(count + "btnGraphics_Click <br />"); count++; } }
此资料是从龙果果的博客(http://www.cnblogs.com/yeminglong/archive/2012/10/16/2725664.html)中找到的,经过验证无误,感谢龙果果的博客的分享。
下面说说My97DatePicker控件服务器端输入框设置默认值:
在解决了Page执行顺序问题后就该是设置默认值了,
前端代码:
<table>
<tr>
<th>
专业室:</th>
<td>
<asp:TextBox ID='SpecialtyName' Name="SpecialtyName" runat='server' Width="144px"></asp:TextBox>
</td>
<th>
导入日期:</th>
<td>
<asp:TextBox ID='IntroductionMonth' Name="IntroductionMonth" runat='server' Width="144px" onfocus="WdatePicker({lang:'zh-cn',startDate:'%y-%M',dateFmt:'yyyy-MM'})"></asp:TextBox>
</td>
</tr>
</table>
后台代码:
protected override void OnPreLoad(EventArgs e) { base.OnPreLoad(e); TextBox IntroductionMonth = (TextBox)this.GridSearch.Controls[1].Controls[0].FindControl("IntroductionMonth"); if (string.IsNullOrEmpty(IntroductionMonth.Text)) { IntroductionMonth.Text = DateTime.Now.ToString("yyyy-MM"); //this.SmartDataSource1.SelectParameters.Add(new Parameter("IntroductionMonth", System.Data.DbType.String, DateTime.Now.ToString("yyyy-MM"))); } }
这样就OK了。
js脚本方法设置默认值:
<script type="text/javascript"> jQuery(document).ready(function ($) { var d = new Date(); function addzero(v) { if (v < 10) return '0' + v; return v.toString(); } var s = d.getFullYear().toString() + "-" + addzero(d.getMonth() + 1); document.getElementById('IntroductionMonth').value = s; }); </script>
此前用过js脚本方法设置默认值能成功,但是由于页面用到了公司框架,框架中会在页面加载时自动查询,而在重置按钮事件后js方法无效了没有被执行,所以采用后台方法设置默认值,当然如果没用到框架,重置按钮是自定义的话使用js脚本应该没问题。要是能够解决框架重置按钮事件也能解决这里就不再尝试了。
果断使用后台方法。

浙公网安备 33010602011771号