单击事件后,全局变量返回null [英] Global variable returns null after click event

查看:98
本文介绍了单击事件后,全局变量返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将点击状态存储为点击后使用它,如在编辑行中阻止,但值设置为null



i have to store click status as "clicked" after use it as in if block in edit row but the value setting null

public string btnStatus;

private void bindSearchResult()
        {
 btnStatus = "clicked";
lblMessage.Text = btnStatus; // lable is displaying fine 
//code
    }
protected void gvMainCategory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
if (e.CommandName == "EditRow")
            {
                int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                gvMainCategory.EditIndex = rowIndex;
                if (btnStatus == "clicked") // if btnstatus is null due to this it goes to else
                {
                    bindSearchResult();
                }
                else
                {
                    bindMC();
                }
            }

推荐答案

只要请求得到满足,ASP.NET页面就会存在...之后它会被破坏并为下一个请求创建一个非常新的页面,所有变量都被初始化为默认值...

所以每次回发(点击事件)你都在处理一个新的页面的实例,所以所有的变量都初始化为默认值... btnStatus为你的情况为空...

这种行为是网络断开性质的结果...

你当然有一些选择来处理'真正'的全局变量...

1.将它存储在会话/应用程序级别

2将它附加到发起click事件的对象(你的案例中的网格行)并将其存储在View State ...
An ASP.NET page lives as long as the request fulfilled...After that it destroyed and a very new page will be created for the next request, with all the variables initialized to default values...
So on every postback (on click event) you are dealing with a new instance of the page and so all the variables are initialize to default values... btnStatus to null fro your case...
This behavior is a result of the disconnected nature of the web...
You have some options of course to handle 'real' global variables...
1. Store it on the session/application level
2. Attach it to the object initiated the click event (the grid row in your case) and store it in the View State...


这篇关于单击事件后,全局变量返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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