1

I have a JavaScript function as below.

function callServer(gridView) 
{
    // Other Codes  
}

There is a GridView with id PartAvailability_GV. I need to pass id of this GridView to my JavaScript function. I tried to do it as below.

callServer(PartAvailability_GV);

But I got an error as below.

Uncaught ReferenceError: PartAvailability_GV is not defined

What could be the issue? How can I achieve this?

Bishan
  • 15,211
  • 52
  • 164
  • 258

1 Answers1

1

I think you're mixing client side JavaScript with Server side ASP.NET - So you'll need to pass the GridView's client ID to the function:

callServer('<%= PartAvailability_GV.ClientID %>');
RemarkLima
  • 11,639
  • 7
  • 37
  • 56
  • This worked. Is there any way to pass `GridView` object to `JavaScript` function ? – Bishan Jul 09 '13 at 09:54
  • @Bishan Not directly, as the `GridView` control renders HTML (usually a table) and JavaScript references the HTML DOM. However, if you look at `serialization` then it's possible, see http://james.newtonking.com/projects/json-net.aspx for a .NET library. You can then "serialize" a .NET object and parse that in JavaScript - It's can get pretty complicated and most of the time you'll just want to serialize the data, or setting and parse them rather than the whole object. – RemarkLima Jul 09 '13 at 09:59
  • Thanks for the tip. actually i need to bind data into a `GridView` in a `WebMethod` in `asmx` file. is there any better way to do it than passing `GridView` object to it ? – Bishan Jul 09 '13 at 10:02
  • Think of a `GridView` as the server rendering HTML with the data you spec. So, you could in your `asmx` override the `render` method and just chuck all the HTML back via the `WebMethod`. However, I'd look at using a jQuery type grid, and just pass a serialized datatable back to it: http://stackoverflow.com/questions/5622716/choosing-a-jquery-datagrid-plugin This way, you have seperate layers: The client has the grid, the webmethod just sends the raw data. This data can then be pumped to different target who all render in whichever way they feel best. – RemarkLima Jul 09 '13 at 10:22