Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
221
get gridcell value on cellbutton click +javascript
posted

Hi,

I have an ultrawebgrid in my page.

I used a templated column, an aspx button is placed in it.

I want to get the Id from a cell when clicking on the corresponding cell button in the temoplated column.

How can I get it using javascript function.

Thanks in advance.

Parents
No Data
Reply
  • 12333
    posted

    Hi,

    I would address this functionality using the following strategy:

    In the WebGrid control’s PreRender event, iterate through the Rows and access each Button control in your Template Column. Use the Attributes.Add method on the Button Control to attach a reference to a client side function that will access a specific WebGrid row. The JavaScript function should be designed to accept a parameter, in this case the index of the Row you wish to access. The client side JavaScript function will then get a reference to the WebGrid Row and from there you can do whatever you need.

    Consider this JavaScript code:

    var _grid = null;

    function UltraWebGrid1_InitializeLayoutHandler(gridName){

    _grid = igtbl_getGridById(gridName);

    }

    function ActivateGridRow(theRowIndex) {

    if (!_grid || theRowIndex == NaN || theRowIndex < 0 ) return;

    var r = _grid.Rows.getRow(theRowIndex);

    if (!r) return;

    r.activate();

    alert("CustomerID: " + r.getCellFromKey("CustomerID").getValue());

    }

    In the above code, we have a reference to the client side WebGrid object which is obtained in the UltraWebGrid1_InitializeLayoutHandler JavaScript function. Each time we click a Button or CheckBox that exists in any of the WebGrid control’s Template Column, the ActivateGridRow client side function is called and passed the index of the Row that the Button or CheckBox belongs to. The ActivateGridRow function simply activates the WebGrid Row and also displays an Alert with the Customer ID.

    Consider this method:

    private void AttachEvents(UltraWebGrid g)

    {

    TemplatedColumn theTemplatedCol = g.DisplayLayout.Bands[0].Columns.FromKey("TEMPLATE") as TemplatedColumn;

    foreach (UltraGridRow r in g.Rows)

    {

    Control theControl = theTemplatedCol.CellItems[r.Index] as Control;

    Button theButton = theControl.FindControl("btnOK") as Button;

    CheckBox theCheckBox = theControl.FindControl("chkSelect") as CheckBox;

    if (theButton != null)

    {

    theButton.Attributes.Add("onClick", "ActivateGridRow(" + r.Index.ToString() + ");");

    }

    if (theCheckBox != null)

    {

    theCheckBox.Attributes.Add("onClick", "ActivateGridRow(" + r.Index.ToString() + ");");

    }

    }

    }

    This method is designed to iterate through each WebGrid Row and extract the Button and CheckBox control contained within the Template Column’s Cell Item for that row. Here you can interact with the control’s object model as if you would any other control such as setting properties, adding event handlers and so forth. In this case all we do is attach the client side ActivateGridRow function to the Button and CheckBox instances that reside in each Cell Item for this Column.

    Since the WebGrid Row Count is 0 until late in the rendering cycle, the AttachEvents server side method should be called from within the WebGrid PreRender event:

    protected void UltraWebGrid1_PreRender(object sender, EventArgs e)

    {

    this.AttachEvents(this.UltraWebGrid1);

    }

Children