Problem: Dynamic TemplateFields in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ananthu
    New Member
    • Sep 2007
    • 87

    Problem: Dynamic TemplateFields in GridView

    I have created a templatefield at runtime for checkboxcolumn in gridview using Itemplate Interface and InstanceIn methods.

    I have given the id for checkbox also.

    I get the checkbox column in gridview successfully.

    Now i want to check all the checkbox in gridview.

    But when i find checkbox controls in gridview with respect to the id i have given, it returns me nothing.

    Following is the code in InstanceIn method of class GridViewTemplat e which Implements Itemplate.

    Code:
    Select case ListItemType.Item
    
     Case ListItemType.Item
    
      Dim chk as checkbox
      chk=new checkbox
      chk.id="chkSelRoom"
      Container.items.add(chk)
    
    End Select
    Code for Finding Checkbox Control in GridView.
    Code:
    For each row as GridViewRow in dgvRoomDetails.rows
    
     dim ch as checkbox
     ch=new checkbox
     ch=CType(row.FindControl("chkSelRoom"),checkbox)  // returns nothing
     ch.checked=true  // object reference not set to an instance of the object
    
    End For
    Error are shown as comments in above code.
    Last edited by Frinavale; Mar 17 '10, 01:29 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags. Removed Bold from question as well.
  • Ananthu
    New Member
    • Sep 2007
    • 87

    #2
    Problem: GridView TemplateFields Disappearing after Postback

    Hi,

    I have created a dynamic templatefields (checkbox and textbox) for gridview successfully by writing code using ITemplate Interface.

    I have loaded the gridview dynamic columns in Form_Load event and also inside the Page.IsPostBack condition only.

    I am having checkbox column and textbox column in my gridview when loading a page.

    But when I click the button control in that page, the templatefields checkbox and textbox column gets removed and only empty columns are displayed after that. But other columns Bounfields are displayed properly.

    What's the problem?

    Give me solution for it.

    Thanks in advance.

    Comment

    • Ananthu
      New Member
      • Sep 2007
      • 87

      #3
      I found the answer by myself. The reason is that I should load the dynamic columns of gridview in Page_PreInit event and not in Page_load event. That's it. It worked fine. Now i am able to check all checkbox in gridview by a button click event.

      Comment

      • Ananthu
        New Member
        • Sep 2007
        • 87

        #4
        Hi,

        I found the answer by myself. The reason is that I should load the dynamic columns of gridview in Page_PreInit event and not in Page_load event. That's it. It worked fine. Now the template columns are not disappearing.

        Thanks if you have also tried with my posted question.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm really happy that you've solved your problem.
          I looked at my code that is similar to yours (except that I use link buttons instead of check boxes) and could not think of anything that would explain the problem that you were having.

          I should have known it had something to do with where you were dynamically instantiating these controls.

          Thanks for sharing your solution!

          -Frinny

          Comment

          Working...