Click here to Skip to main content
Click here to Skip to main content

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.

DataGridRowValue

screenshot2.GIF

Introduction

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

Background

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 ==
                        ListItemType.AlternatingItem)
  {
    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+"','" + 
       HireDate+"','"+Job+"','"+Publisher+"','"+City+"');";
  }
}

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();
    array[0]=first_name;
    array[1]=last_name;
    array[2]=hire_date;
    array[3]=job;
    array[4]=publisher;
    array[5]=city;
    window.returnValue=array;
    window.close();    
}

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!

License

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

Share

About the Author

murtaza dhari
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir
 
http://murtazadharis.blogspot.com/

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAmir Mehrabi-Jorshary22-Jun-12 19:40 
GeneralThanks a lot dude PinmemberFIFI526-Feb-10 4:56 
GeneralRe: Thanks a lot dude Pinmembermurtaza dhari2-Mar-10 14:31 
GeneralAbout DataGrid View Pinmembermarifdu27-Oct-08 9:17 
GeneralRe: About DataGrid View Pinmembermurtaza dhari27-Oct-08 17:07 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 30 Mar 2008
Article Copyright 2008 by murtaza dhari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid