×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

请问做过或者正在做UI的大侠,当需要必须用动态使用templateField的时候,你们是怎么解决TemplateField在GridView里不能保存state的问题的,在Page_Load里每次动态Add/Insert总会自动增加空columns,还要remove这些columns。。。。

我目前在用GridView做普通UI的数据显示,稍微特殊点的要求就要用TemplateField(implement ITemplate, Override InstantiateIn()),遗憾的是,目前为止微软也没有解决TemplateField不能像BoundField那样保存state的问题,具体看链接。这样导致只能在Page_Load里动态add/insert这些TemplateField,但同时又有自动带来空白columns的问题(每次postback一次,多一个空白的column。。。),只能再手动删除一次空白columns,这样Page_Load显得太heavy了,特别是TemplateField过多的情况下,大家有无更好的方法解决这个问题呢?
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 请问做过或者正在做UI的大侠,当需要必须用动态使用templateField的时候,你们是怎么解决TemplateField在GridView里不能保存state的问题的,在Page_Load里每次动态Add/Insert总会自动增加空columns,还要remove这些columns。。。。
    我目前在用GridView做普通UI的数据显示,稍微特殊点的要求就要用TemplateField(implement ITemplate, Override InstantiateIn()),遗憾的是,目前为止微软也没有解决TemplateField不能像BoundField那样保存state的问题,具体看链接。这样导致只能在Page_Load里动态add/insert这些TemplateField,但同时又有自动带来空白columns的问题(每次postback一次,多一个空白的column。。。),只能再手动删除一次空白columns,这样Page_Load显得太heavy了,特别是TemplateField过多的情况下,大家有无更好的方法解决这个问题呢?
    • 你好像没入门,多看看书
      • 你好像什么也没说,一点诚意也没有。
        • 不少IT人就这德行,你过时了,你不行,你水平低,我知道答案就是不告诉你,其实潜台词就是告诉你了我怎么混啊!
    • Not very clear your question. One purpose of ViewState is to keep control’s value to be shown automatically after postback. If it’s your only requirement, you can rebind data source after postback.
      • If you have tried dynamically adding TemplateField into GridView control, you would know what I was talking about. check this out.
        • we a using template columns for all columns and works ok. before we add column, we just check if the column count is 0. we save status when postback and set new status in rowdatabound.
          • that's what I am doing now. we have to delete the empty columns before or after we add/insert TemplateField in every postback. that's painful sometimes.
        • I don’t understand why after postback, it generates a blank column. Since the dynamically added columns are not saved in viewstate, they will disappear after postback. How the blank column comes?
          BTW, what kind special requirement lets you look for some template other than those built-in GV fields? The HyperLinkField is much more powerful than invented GridViewHyperlinkTemplate
          • It seems you don't have so many experience with GridView control. you probably are back-end developer?
            If you have some experiences with playing around GridView, you should know :
            1. There is buggy thing in HyperLinkField to stop javascript code to execute(you simply can't successfully call window.open("xx","xx","width=xx,height=xx....")), you can google this.
            2. For built-in CheckBoxField, there must be a bool type field in database table. What if I need checkbox on right hand side for choosing specific row, basically the only thing I can do is with Custom TemplateField.
            3. the reason adding custom TemplateField generates blank column, I have no clue so far. You can try to do yourself, see if there is something happening.
            • See my comments inside
              1. There is buggy thing in HyperLinkField to stop javascript code to execute(you simply can't successfully call window.open("xx","xx","width=xx,height=xx....")), you can google this.

              Of course, you cannot directly call window.open in HyperLinkField, neither Hyperlink cannot. Because the method returns window object, after calling, caller windrow shows [object] (in IE) or [object Window] (in FF). Solution is you create a simple javascript function like
              function newWin()
              { var win = window.open(URL,...);
              win.focus();
              } Then set HyperLinkField’s NavigateUrl="javascript:newWin();"

              2. For built-in CheckBoxField, there must be a bool type field in database table. What if I need checkbox on right hand side for choosing specific row, basically the only thing I can do is with Custom TemplateField.

              Basically CheckBoxField is also a bound field. The major purpose is to show data rather than operation purpose. However, it’s possible to use it as operation purpose.

              3. the reason adding custom TemplateField generates blank column, I have no clue so far. You can try to do yourself, see if there is something happening.

              No blank column when I try it. If I were you, I would check my code (in both .cs and .aspx files) very carefully.
              • thanks.
                本文发表在 rolia.net 枫下论坛for question1, I think u just misunderstood, of course I know how to popup a window with HyperLinkField......what I said is, HyperLinkField won't let you call window.open(don't get me wrong,this is javascript) with changing window's properties. for example, following code is in my current code:
                window.open('CustomersEdit.aspx?Status=New','CustomersEdit','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=319,height=177,left = 490,top = 362')
                if I use this in HyperLinkField.NavigateUrl="", the popup window's size doesn't change.

                the reason I googled:

                This issue with hyperlink field unable to run a javascript function has been around for a while. Microsoft confirmed it's a bug. So currently there's NO WAY to make a hyperlink field running a script. Embed the script function and it won't generate a link. As for the work-around using ItemTemplate, there's no need for all that code.

                for question 3, I will try to reproduce the problem today in a new project. I will let you know the result. It shouldn't happen like that.

                anyway, thanks for input.更多精彩文章及讨论,请光临枫下论坛 rolia.net
              • For question3:
                I don't think in your code, sometimes you just don't postback enough:)
                set a Watch for your gridview.columns.count,you will see everytime you postback the count will increase. because the column we added for TemplateField are still there ,just lost state when posting back. that's why i saw empty columns.