从视图状态读取数据时延迟 [英] Delay when reading data from viewstate

查看:173
本文介绍了从视图状态读取数据时延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图挽救一个变量视图状态和使用后右边的按钮是pressed。
问题是,你必须preSS一个钮2次写东西了。
这code是问题,因为我看到它boilde下来的基本知识。

 <%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;
<!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
    <身体GT;
        <表ID =form1的=服务器>
            < ASP:按钮的ID =Button1的=服务器文本=按钮1的OnClick =的button1_Click/>
            < ASP:按钮的ID =Button2的=服务器的OnClick =Button2_Click文本=按钮2/>
        < /表及GT;
    < /身体GT;
< / HTML>

在code背后
使用系统;

 公共部分类_Default:System.Web.UI.Page {    保护无效的Page_Load(对象发件人,EventArgs的发送){
        回复于(的ViewState [按钮]);
    }    保护无效的button1_Click(对象发件人,EventArgs的发送){
        的ViewState [按钮] =按钮1;
    }    保护无效Button2_Click(对象发件人,EventArgs的发送){
        的ViewState [按钮] =按钮2;
   }
}


解决方案

您的页面加载事件之前,你的按钮点击事件,以便在第一页加载您查看的状态是空白的发生。

在第一个按钮,单击它仍然是空的,因为页面加载先打电话那么你按一下按钮。

现在对第二次按键点击视图状态有值,它会显示在页面上。

在这里,你必须使用页面pre渲染事件显示类似下面的视图状态值

 保护覆盖无效在preRender(EventArgs的发送)
        {
            base.On preRender(E);            回复于(的ViewState [按钮]);
        }

将这个你页面类,它会在一个按钮的点击工作

So i am trying to save a variable in the viewstate and use right after the button is pressed. The problem is that you have to press a botton 2 times before something is written. This code is the problem as i see it boilde down to the basics.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
    <body>
        <form id="form1" runat="server">
            <asp:Button ID="Button1" runat="server" Text="Button 1" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button 2" />
        </form>
    </body>
</html>

in the code behind using System;

public partial class _Default:System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        Response.Write(ViewState["Button"]);
    }

    protected void Button1_Click(object sender, EventArgs e) {
        ViewState["Button"] = "Button 1";
    }

    protected void Button2_Click(object sender, EventArgs e) {
        ViewState["Button"] = "Button 2";
   }
}

解决方案

Your page load event occur before your button click events so in first page load you view state is blank.

On first button click it is still blank because page load is call first then your button click.

Now on second time button click view state have value so it will show on your page

Here you have to use page Pre Render event to show view state values like following

protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Response.Write(ViewState["Button"]);
        }

Put this in you page class and it will work in single button click

这篇关于从视图状态读取数据时延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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