Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am developing a system using asp.net(c#) , ajax and java scripts. I want to bind a dataset to a grid-view using a JavaScript function. the java script function is written in a different .js file and linked to the aspx page. I am calling that function in "onclientclick" method in a button because I want to stop post back. I am not using an update panel. I am calling code behind function to get the required dataset. I have successfully taken the dataset to the function but the problem is how to set the data source & call the databind method of a gridview in javascript?
Posted 27-Jan-11 2:48am
Edited 27-Jan-11 4:15am
Henry Minute223.8K
v2
Comments
Pravin Patil, Mumbai at 27-Jan-11 7:56am
   
Hi Saurabh,
You have already executed the postback while fetching the data in the dataset.
And there is no sense in binding the datagrid in javascript. Because you will have to execute the postback to get the data from database.
Sandeep Mewara at 27-Jan-11 9:25am
   
Not exactly. Have a look at my answer.
Christian Graus at 27-Jan-11 9:18am
   
I would agree with the answer you were given and add that you desperately need to read a book on ASP.NET, it's clear you don't understand the server/client division at all.
Sandeep Mewara at 27-Jan-11 9:27am
   
Can I have something like I mentioned in answer as a resolution?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Actually you can't. I think you are new in asp.net. Else you can not do it. If you u use ajax than why you want to do using javascript. By javascript you can do only one thing. Bind a html and assign to div. But in that case there is difficult to sorting and paging and other formation like gridview.
  Permalink  
Comments
Sandeep Mewara at 27-Jan-11 9:25am
   
I would not fully agree. Have a look at my answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You cannot bind the grid on client side but you can surely get the new data displayed using Javascript.
 
Have a look at this article: ClientCallback custom control for web applications[^]
 
Something similar can be done. Once you have the new dataset binded to the grid on server side, get the Grid into html writer text and pass it on via Response to Javascript. Replace the html of Grid. Done!
 
Something like:
protected void RebindDataGrid()
{
    DataTable dt = RetrieveDataTable();
    gvTest.DataSource = dt;
    gvTest.DataBind();
 
    //Setting of the response output for callback
    using (System.IO.StringWriter sw = new System.IO.StringWriter())
    {
        gvTest.RenderControl(new HtmlTextWriter(sw));
        Response.Write(sw.ToString());
    }
}
function ResponseRecieved(responseText)
{
    // Based on responseText, action taken on client side
    document.getElementById("gvTest").outerHTML = responseText;
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 219
4 Mathew Soji 160
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100