Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 27-Jan-11 3:15am
v2
Comments
Pravin Patil, Mumbai 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 27-Jan-11 9:25am    
Not exactly. Have a look at my answer.
Christian Graus 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 27-Jan-11 9:27am    
Can I have something like I mentioned in answer as a resolution?

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.
 
Share this answer
 
Comments
Sandeep Mewara 27-Jan-11 9:25am    
I would not fully agree. Have a look at my answer.
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:
C#
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());
    }
}

JavaScript
function ResponseRecieved(responseText)
{
    // Based on responseText, action taken on client side
    document.getElementById("gvTest").outerHTML = responseText;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900