How to set cell value on client?

5 posts, 0 answers
  1. Rusty
    Rusty avatar
    3 posts
    Member since:
    Sep 2010

    Posted 23 Jan 2013 Link to this post

    Hello,
    I need to set some radgrid cell values client-side, via javascript.  Then at postback I need to access those values.  Currently I'm setting the cell's innerHTML property, which displays correctly in the grid.  However, at postback all the cell values revert to " ".  At postback, I am not re-binding the grid.  
    Thanks!
    -rusty

    Here's how I set values on the client-side:

    // For all selected rows, set the cell value.
    function rgSetSelectedRowValue(rg, columnName, val) {
        var table = rg.get_masterTableView();
        var selectedRows = table.get_selectedItems();
        for (var i = 0; i < selectedRows.length; i++) {
            var row = selectedRows[i];
            var cell = table.getCellByColumnUniqueName(row, columnName);
            cell.innerHTML = val;
        }
    }


    Here's how I access values server-side:

    // loop on grid rows
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items) {
    string foo = item["ReplacementID"].Text;
    string bar = item["comment"].Text;
    ...
    }




  2. Jayesh Goyani
    Jayesh Goyani avatar
    2640 posts
    Member since:
    May 2010

    Posted 23 Jan 2013 Link to this post

    Hello,

    function AccessOnclient() { 
                    var grid = $find("<%= RadGrid1.ClientID %>"); 
                    if (grid) { 
                        var MasterTable = grid.get_masterTableView(); 
                        var Rows = MasterTable.get_dataItems(); 
                        for (var i = 0; i < Rows.length; i++) { 
                            var row = Rows[i]; 
                            // Method1 
                            var getCellText_1 = row.get_element().cells[0].innerHTML; 
                            // Method2 
                            var getCellText_2 = row.get_cell("ReplacementID").innerHTML; 
                            // Method3 
                            var getCellText_2 = row.get_cell("ReplacementID").getElementsByTagName("span")[0].innerHTML; //this code also work for Checkboxcolunm, hyperlinkcolumn...etc 
                        
                    
                   
                }

    For more info please check below link
    http://jayeshgoyani.blogspot.in/2012/07/access-radgrid-on-client.html

    Thanks,
    Jayesh Goyani

  3. Rusty
    Rusty avatar
    3 posts
    Member since:
    Sep 2010

    Posted 23 Jan 2013 Link to this post

    Thanks Jayesh, but my problem lies with SET values, not GET values.  I want to SET a grid cell value on the client, and then have that value available on the server at postback.  When I set innerHTML, the value is lost at postback.
    -r
     

  4. Shinu
    Shinu avatar
    17747 posts
    Member since:
    Mar 2007

    Posted 24 Jan 2013 Link to this post

    Hi,

    You cannot get the values that are changed in the client side from server side because of postback. One suggestion is that you can set the value to a hiddenfield as the cell value in client side and access it in server side as shown below.
    JS:

    function rgSetSelectedRowValue(rg, columnName, val) {
        var table = rg.get_masterTableView();
        var selectedRows = table.get_selectedItems();
        for (var i = 0; i < selectedRows.length; i++) {
            var row = selectedRows[i];
            var cell = table.getCellByColumnUniqueName(row, columnName);
            cell.innerHTML = val;
           var hidden = document.getElementById("HiddenField1");
                hidden.value = cell.innerHTML;
        }
    }
    C#:
    protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGrid1.SelectedItems)
            {
            string cellvalue= HiddenField1.Value;
            }
    }

    Thanks,
    Shinu

  5. Rusty
    Rusty avatar
    3 posts
    Member since:
    Sep 2010

    Posted 25 Jan 2013 Link to this post

    Thanks for your reply, Shinu.  I am aware of the possibility of using hidden fields, however I am storing a LOT of stuff in the grid cells, so the hidden field method is rather messy.  

    In the past, I've used another 3rd-party grid that supported "unbound columns", which could be populated on the client, then accessed server-side after a postback.  It looks like this is not possible with RadGrids.  

    My current solution is to use the grid to store my data, and then handle it all client-side without doing a postback.  Took a bit of refactoring but it's working now.

    -rd

Back to Top