RegisterArrayDeclaration Help Needed with Grid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xenophon

    RegisterArrayDeclaration Help Needed with Grid


    I have a grid with checkboxes in it. When a checkbox is un/checked, I
    want to set a true-false value in an array. Then on PostBack I can
    work with that array.

    I know I need to use RegisterArrayDe claration, but I don't see a quick
    tutorial how. Can anyone post sample C# code-behind-only code on how
    to do this?

    Thanks.


  • Scott Allen

    #2
    Re: RegisterArrayDe claration Help Needed with Grid

    Just as an alternate solution: you could loop through the grid on
    postback and pick out which rows have the checkbox checked.

    there is some sample code in this article:


    --
    Scott


    On Tue, 17 May 2005 21:44:25 -0400, xenophon <xenophon@onlin e.nospam>
    wrote:
    [color=blue]
    >
    >I have a grid with checkboxes in it. When a checkbox is un/checked, I
    >want to set a true-false value in an array. Then on PostBack I can
    >work with that array.
    >
    >I know I need to use RegisterArrayDe claration, but I don't see a quick
    >tutorial how. Can anyone post sample C# code-behind-only code on how
    >to do this?
    >
    >Thanks.
    >[/color]

    Comment

    • Steven Cheng[MSFT]

      #3
      Re: RegisterArrayDe claration Help Needed with Grid

      Thanks for Scott's informative suggestion.

      Hi Xenophon ,

      As Scott has mentioned, we can query the select status in each datagrid Row
      at serverside when post back rather than use clientsdie script to store
      these info. Since the serverside ASP.NET provide a convenient object model
      for us to access the datagrid's item template, I think it'll be much
      better. What's your oponion on this?

      Thanks,

      Steven Cheng
      Microsoft Online Support

      Get Secure! www.microsoft.com/security
      (This posting is provided "AS IS", with no warranties, and confers no
      rights.)

      Comment

      • xenophon

        #4
        Re: RegisterArrayDe claration Help Needed with Grid

        Thanks, but I won't be able to do that because I am not keeping the
        ViewState contents because the grid is complex and large. Pushing a
        couple of hundred kbytes back upstream just to know a checkbox state
        is too much for this app.



        On Tue, 17 May 2005 22:47:59 -0400, Scott Allen
        <scott@nospam.o detocode.com> wrote:
        [color=blue]
        >Just as an alternate solution: you could loop through the grid on
        >postback and pick out which rows have the checkbox checked.
        >
        >there is some sample code in this article:
        >http://odetocode.com/Articles/372.aspx[/color]

        Comment

        • xenophon

          #5
          Re: RegisterArrayDe claration Help Needed with Grid




          I cannot use this solution. The rendered grid is very large and
          complex, so EnableViewState is set to false for the grid. I just need
          to know the state of the checkboxes, with just the checkbox state (and
          grid row id) going back upstream.

          Please show an example of how I can do this, using only C# (my grids
          are not created in the .aspx page tempalte at all).

          Thanks.




          On Wed, 18 May 2005 08:20:54 GMT, v-schang@online.m icrosoft.com
          (Steven Cheng[MSFT]) wrote:
          [color=blue]
          >Thanks for Scott's informative suggestion.
          >
          >Hi Xenophon ,
          >
          >As Scott has mentioned, we can query the select status in each datagrid Row
          >at serverside when post back rather than use clientsdie script to store
          >these info. Since the serverside ASP.NET provide a convenient object model
          >for us to access the datagrid's item template, I think it'll be much
          >better. What's your oponion on this?
          >
          >Thanks,
          >
          >Steven Cheng
          >Microsoft Online Support
          >
          >Get Secure! www.microsoft.com/security
          >(This posting is provided "AS IS", with no warranties, and confers no
          >rights.)[/color]

          Comment

          • Steven Cheng[MSFT]

            #6
            Re: RegisterArrayDe claration Help Needed with Grid

            Hi Xenophon,

            Thanks for your followup.
            If what you worried about is the DataGrid being disabled for Viewstate,
            then that's doesn't matter since we can still retrieve the controls from
            DataGrid.Items collection when posting back as long as we bind data with
            datagrid everytime in the Page_Load. Anyway, I've made a simple demo page
            which have used both the two means :
            1. Use serverside datagrid obejct model to query the checkbox controls and
            get the checked state.

            2. Use clientside script to save all the checkboxes's checked states before
            post back to server.

            The page's code are pasted in the bottom of the message, you can have a
            test on your side to see whether it helps.

            Thanks,

            Steven Cheng
            Microsoft Online Support

            Get Secure! www.microsoft.com/security
            (This posting is provided "AS IS", with no warranties, and confers no
            rights.)

            =============== aspx=========== =========
            <HTML>
            <HEAD>
            <title>checkbox grid</title>
            <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
            <meta name="CODE_LANG UAGE" Content="C#">
            <meta name="vs_defaul tClientScript" content="JavaSc ript">
            <meta name="vs_target Schema"
            content="http://schemas.microso ft.com/intellisense/ie5">
            <script language="jscri pt">
            function saveCheckArray( )
            {
            var i=0;
            var array = new Array();
            for(i=0;i<docum ent.all.length; i++)
            {
            if(document.all[i].type == "checkbox" &&
            document.all[i].id.indexOf("dg Check")> -1)
            {
            var chk = document.all[i];
            array.push(chk. checked);

            }

            }

            document.getEle mentById("check Array").value = array.join("|") ;
            }
            </script>
            </HEAD>
            <body>
            <form id="Form1" method="post" runat="server">
            <asp:DataGrid id="dgCheck" runat="server" AutoGenerateCol umns="False"
            EnableViewState ="False">
            <Columns>
            <asp:BoundColum n HeaderText="Nam e" DataField="name "></asp:BoundColumn >
            <asp:TemplateCo lumn HeaderText="Sel ected">
            <ItemTemplate >
            <asp:CheckBox ID="chkSelect" Runat="server" Checked="<%#
            ((System.Data.D ataRowView)Cont ainer.DataItem)[1] %>">
            </asp:CheckBox>
            </ItemTemplate>
            </asp:TemplateCol umn>
            </Columns>
            </asp:DataGrid>
            <input type="hidden" id="checkArray " runat="server">

            <P>
            <asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>
            </P>
            </form>
            </body>
            </HTML>

            =========code behind========= ============
            public class checkboxgrid : System.Web.UI.P age
            {
            protected System.Web.UI.W ebControls.Butt on btnSubmit;
            protected System.Web.UI.H tmlControls.Htm lInputHidden checkArray;

            protected System.Web.UI.W ebControls.Data Grid dgCheck;

            private void Page_Load(objec t sender, System.EventArg s e)
            {
            dgCheck.DataSou rce = GetDataSource() ;
            dgCheck.DataBin d();

            btnSubmit.Attri butes["onclick"] ="saveCheckArra y();";
            }

            private DataTable GetDataSource()
            {
            DataTable dt = new DataTable();

            dt.Columns.Add( "name",typeof(s tring));
            dt.Columns.Add( "selected",type of(bool));

            for(int i=0;i<10;i++)
            {
            DataRow dr = dt.NewRow();
            dr[0] = "Name_" + i;
            dr[1] = i%3 == 0? true:false;

            dt.Rows.Add(dr) ;
            }

            return dt;

            }


            #region Web Form Designer generated code
            override protected void OnInit(EventArg s e)
            {
            InitializeCompo nent();
            base.OnInit(e);
            }

            private void InitializeCompo nent()
            {
            this.btnSubmit. Click += new System.EventHan dler(this.btnSu bmit_Click);
            this.Load += new System.EventHan dler(this.Page_ Load);

            }
            #endregion

            private void btnSubmit_Click (object sender, System.EventArg s e)
            {
            CheckBox chk = null;
            int i = 0;


            Response.Write( "<br>Values from DataGrid server object:");
            for(i=0;i<dgChe ck.Items.Count; i++)
            {
            chk = dgCheck.Items[i].FindControl("c hkSelect") as CheckBox;

            Response.Write( string.Format(" <br/>Item {0} Selected: {1}", i,
            chk.Checked));
            }

            Response.Write( "<br>Values from clientside input hidden field:");

            string[] items = checkArray.Valu e.Split("|"[0]);

            for(i=0;i<items .Length;i++)
            {
            Response.Write( string.Format(" <br/>Item {0} Selected: {1}", i,
            items[i]));
            }
            }
            }
            =============== =======


            Comment

            • Steven Cheng[MSFT]

              #7
              Re: RegisterArrayDe claration Help Needed with Grid

              Hi Xenophon,

              Any progress on this issue? Does the test page I pasted in the former
              message help? If anything else we can help, please feel free to post here.

              Thanks & Regards,

              Steven Cheng
              Microsoft Online Support

              Get Secure! www.microsoft.com/security
              (This posting is provided "AS IS", with no warranties, and confers no
              rights.)

              Comment

              Working...