用户控制在回发时松散的proerty值 [英] User Control Loose proerty Value on Postback

查看:68
本文介绍了用户控制在回发时松散的proerty值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



以下是我的问题:

1)我有一个dataView,其中项目模板代码中的绑定usercontrol为as关注

Hello ,

following is my problem :
1) I have one dataView where am binding usercontrol in item template code are as follow

  <ItemTemplate>
<Chart:LineGraph  runat="server" ID="uc1" A='<%# Eval("A")%>' Data='<%# Eval("Data") %>' />
  </ItemTemplate>





我在databind上传递A和Data



然后我在usercontrol页面上检查这个数据加载

使用以下代码



where I passed "A" and "Data" on databind

then I check this data on usercontrol page load
using following code

 public  string A {
        get;
        set;
    }
    public  string Data
    {
        get;
        set;
    }
 if (!string.IsNullOrEmpty(Data))
        {
            string ValueOfA = A;
            string DataValue = Data;
}



第一次运行得非常好。

但是当页面获得回发时我在数据和A中得到空值



我尝试过相同的地方放在updatepanel中,然后我也得到了相同的结果。





任何人都可以告诉解决方案



在此先感谢

Chetan V


on first time it work very well.
but when page get postback I get null value in Data And "A"

I was tried same to place in updatepanel then also I got same result.


Can any one tell the solution

Thanks in Advance
Chetan V

推荐答案

检查你的页面加载方法:每次创建页面时都会调用它,包括回发 - 如果你没有检查这是否是回发,那么你很可能会覆盖这些值您的初始默认值:

Check your Page Load method: it gets called every time the page is created, and that includes postbacks - if you are not checking if this is a postback, then you are likely to be overwriting the values with your initial defaults:
if (IsPostback)
   {
   ...
   }

应该有帮助。


完成页面生命周期后,值将会丢失。因此,建议在属性中使用VIewstate来保留回发值。



类似



On completion of Page life cycle the values will be lost. So a suggestion is to use a VIewstate in properties to persist the values across postback.

Something like

public string A
{
    get
    {
        return ViewState["a"] != null ?Convert.Tostring( ViewState["a"]): string.Empty;//to handle null
    }
    set
    {
        ViewState["a"] = value;
    }
}





希望这会有所帮助......



Hope this helps...


这篇关于用户控制在回发时松散的proerty值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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