This is a migrated thread and some comments may be shown as answers.

GridItem read attribute from client

4 Answers 334 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Baal
Top achievements
Rank 1
Baal asked on 19 Oct 2011, 10:29 PM
If we add a value to a RadComboBoxItem attribute, we can always read it from the client side by saying
var value = combo.get_selectedItem().get_attributes().getAttribute('MyAttribute');


Can we do something similar for grid? During the ItemDataBound, I want to add attribute to each DataItem and read it when a row is selected and deselected clientside.

Thanks.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Oct 2011, 06:20 AM
Hello Baal,
I am not sure from where you are trying to access the attributes value. Here is the sample code for setting the custom attributes in ItemDataBound event.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
  RadComboBox combo = (RadComboBox)item.FindControl("combo");
  foreach (RadComboBoxItem item1 in combo.Items)
  {
      item1.Attributes["Asia"] = "4";
  }
 }
}
If you are using any built-in client events of RadComboBox, then you can access the attribute value with the sender and the arguments ( the default parameters). Here is a sample code.
Javascript:
function OnClientSelectedIndexChanged(sender,args)
{
 var item = args.get_item();
 alert(item.get_attributes().getAttribute("Asia"));
}
If you are accessing form any other control event you need to try the following approach.
Custom Attributes.

Thanks,
Princy.
0
Baal
Top achievements
Rank 1
answered on 20 Oct 2011, 04:44 PM
I'm sorry, I didn't explain it clearly. Combobox attributes aren't an issue. I put that in as an example. I need to read gridItem attribute similar to how we read comboboxItem attribute.

Here's what I have in my grid ItemDataBound.
protected void rg_ItemDataBound (object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var obj = (MyObject) e.Item.DataItem;
         
        if (obj.IsActive)
            e.Item.Attributes["Active"] = "1";
        else
        {
            e.Item.ForeColor = Color.Red;
            e.Item.Attributes["Active"] = "0";
        }
    }
}


On Grid->ClientSettings->ClientEvents->OnRowSelecting, I have a client function RowSelecting. This is what I want to do.
function RowSelecting (sender, e)
{
    // Pseudo-code. This is what I want to do.
    var isActive = e.get_Attributes().getAttribute('Active');
     
    if (isActive == '0')
    {
        var isGood = confirm ('This record is inactive. Select anyway?');
         
        if (!isGood)
            e.set_cancel (true);
    }
}

Also, another extension will be to check from some other control's event handler.
function ibEdit_ClientClick ()
{
    var masterTableView = $find('<%= rg.ClientID %>').get_masterTableView();
    var items = masterTableView.get_selectedItems();
     
    // Pseudo-code. This is what I want to do. 
    var isGood = true;
    for (var i=0; i<items.length; i++)
    {
        var item = items[i];
        var isActive = item.get_Attributes().getAttribute('IsActive');
         
        if (isActive == '0')
            isGood = false;
    }
     
    if (!isGood)
        isGood = confirm ('Inactive records are selected. Proceed anyway?');
         
    return isGood; // now server-side code can continue if isGood==true
}

I checked the RadGrid documentation at
http://www.telerik.com/help/aspnet-ajax/grid-getting-familiar-with-client-side-api.html
and didn't find anything under client-side programming that shows how to read the gridItem attribute.

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 21 Oct 2011, 07:52 AM
Hello Baal,

Here is the sample code to access the attribute value from client side.
javascript:
<script type="text/javascript">
 function RowSelecting(sender, e)
    {
        var isActive = e.get_gridDataItem().get_element().getAttribute("Active")
         . . . . .
    }

Thanks,
Shinu.
0
Baal
Top achievements
Rank 1
answered on 24 Oct 2011, 05:15 PM
Thank you Shinu. This code works beautifully.

Can you please add this getAttribute to the RadGrid documentation.

Thanks.
Tags
Grid
Asked by
Baal
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Baal
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or