Click here to Skip to main content
14,030,350 members
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 29 Mar 2008
Licenced CPOL

Get DataGrid Row Value Without PostBack or AJAX

, 29 Mar 2008
Rate this:
Please Sign up or sign in to vote.
How to get DataGrid row value without postback or AJAX.




This article basically describes how to get a DataGrid row value without postback or calling AJAX.


There are scenarios when we have to populate fields from a child window to a parent window after selecting a particular row from a DataGrid. The idea is to pass the DataGrid value to the parent window without postback or calling AJAX. I did ask this question in different forums, but everyone ended up suggesting to use AJAX. After some searching and exploring, I found some trick to do this otherwise.

Using the code

I have created a scenario in which the parent window has some fields that can be populated from the child window. After getting the search results on the DataGrid, we select the result and the parent window fields will be populated with the selected result.

This can be done by using the OnItemDataBound event. In the event handler function, we register the OnMouseOver, OnMouseOut functions for decoration purposes, and the OnClick function registers a SetMouseClick function, passing the parameters to the parent window.

private void dgEmployee_ItemDataBound(object sender, 
        System.Web.UI.WebControls.DataGridItemEventArgs e)
  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    e.Item.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
    e.Item.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
    string FirstName = e.Item.Cells[0].Text.ToString();// first column value
    string LastName = e.Item.Cells[1].Text.ToString() ;// Second 
    string HireDate = e.Item.Cells[2].Text.ToString() ;// third
    string Job = e.Item.Cells[3].Text.ToString() ;// fourth
    string Publisher = e.Item.Cells[4].Text.ToString() ; // fifth 
    string City = e.Item.Cells[5].Text.ToString() ; // sixth
    e.Item.Attributes["onclick"] = 
       "javascript:setMouseClick('"+FirstName+"','"+LastName+"','" + 

Now, we have a JavaScript function that passes the parameters to parent window on the DataGrid Click event. Since this is a modal popup, we can return the selected row value to the parent window through window.returnValue.

//JavaScript Function 
function setMouseClick(first_name,last_name,hire_date,job,publisher,city ) {
    var array = new Array();

On the parent window side, we have a JavaScript function that accepts the return value from the child window and shows up the return value on text boxes. The OnSearch function fires on clicking the search hyperlink.

//JavaScript Function 
function OnSearch()
    var value = window.showModalDialog('\Popup.aspx','','');
    if(value != null)
        document.getElementById('txtFirstName').value = value[0];
        document.getElementById('txtLastName').value = value[1];
        document.getElementById('txtHireDate').value = value[2];
        document.getElementById('txtJob').value = value[3];
        document.getElementById('txtPublisher').value = value[4];
        document.getElementById('txtCity').value = value[5];

That's all. Thanks!


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


About the Author

murtaza dhari
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Amir Mehrabi-Jorshari22-Jun-12 19:40
memberAmir Mehrabi-Jorshari22-Jun-12 19:40 
GeneralThanks a lot dude Pin
FIFI526-Feb-10 4:56
memberFIFI526-Feb-10 4:56 
GeneralRe: Thanks a lot dude Pin
murtaza dhari2-Mar-10 14:31
membermurtaza dhari2-Mar-10 14:31 
GeneralAbout DataGrid View Pin
marifdu27-Oct-08 9:17
membermarifdu27-Oct-08 9:17 
GeneralRe: About DataGrid View Pin
murtaza dhari27-Oct-08 17:07
membermurtaza dhari27-Oct-08 17:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190419.4 | Last Updated 30 Mar 2008
Article Copyright 2008 by murtaza dhari
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid