2

I want to get the value of column[0] of row[0] from a telerik mvc grid in a Javascript function. I wonder how to do that?

   @{
    Html.Telerik().Grid<StationEvaluation>().Name("ManagementGrid")
    .DataKeys(dataKeys => dataKeys.Add(o => o.StationEvaluationID)).Groupable().
    Filterable().Pageable().Sortable().Localizable("fa-IR").
    DataBinding(dataBinding => dataBinding.Ajax()

            ...
            columns.Bound(o => o.StationEvaluationId);
            ...
    }

<script type="text/javascript">
function MyFunction()
{
// I want to get the value here!
}
</script>
6
  • @BahmanNihhhahah, Telerik uses the fact that any content (HTML, JavaScript, etc) inside ASP.NET Web Pages is in fact translated into a Response.Write-like call by the page compiler. Try to 'View page source' for your rendered page. What are the rendered control id's? Commented Feb 3, 2013 at 17:40
  • Hi Dave, thanks for the reply. The control's id is the same with grid's name as it must be. You are right, I must find the value in the rendered HTML, but I can't find it! Commented Feb 4, 2013 at 6:13
  • that's strange. can you find anything that resembles controls? possibly jscript enabled? maybe if you do a search for id's. I will ask some ppl in my office about their experience with telerik. see if i can get more recent insights. Commented Feb 4, 2013 at 6:33
  • 1
    as a completely unrelated matter, i recomend you make a stackoverflow meta account. You can make suggestions about feature changes there and interact with moderators hearing their ideas about where stack should go. Also, you get a 100pnt bonus to your stack rep score the first time u create another stack-exchange account and link them. Commented Feb 4, 2013 at 6:37
  • Thanks Dave, OK, I'll do it ASAP. Commented Feb 4, 2013 at 6:40

2 Answers 2

3

there is even much more simpler way of doing this.

All of the Telerik MVC widgets have a clear cut client site API. Here is the link to the Grd-Client Side API documentation:

http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-client-api-and-events.html

What you are looking for is a way to access the underlying data item bound to a row. The Grid client side API exposes dataItem property of the grid.

Lets say you wanted to get the underlying data object bound to first row - you will write the following code to access it:

var grid = $("#<ID of the Grid").data("tGrid");
// get the first table row
var tr = $("#Grid tbody tr:eq(1)"); // use eq(1) if the grid is scrollable or eq(0) if not to get the first row
// get the associated data item
var dataItem = grid.dataItem(tr);

Now the dataItem represent the underlying data object.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I think your solution is better than mine.
1

I found the answer:

var mastergrid = $('#ManagementGrid');
var mypreciousvalue = mastergrid[0].childNodes[2].childNodes[2].childNodes[0].childNodes[0].innerText;

The rendered HTML of the grid is something like this:

<div class="t-widget t-grid" id="ManagementGrid">
  <div class=t-toolbar t-grid-toolbar t-grid-top">
       ...</div>
  <div class="t-grouping-header">
       ...</div>
  <table cellspacing="0">
      <colgroup>...</colgroup>
      <thead class="t-grid-header">...</thead>
      <tbody>
         <tr>
            <td> **MY PRECIOUS VALUE** <td>
            ...(other cells)
         </tr>
         ... (other rows)
      </tbody>
  <div class="t-grid-pager t-grid-bottom">
       ...</div>
 </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.