Click here to Skip to main content
6,629,885 members and growing! (21,113 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

Get DataGrid Row Value Without PostBack or AJAX

By murtaza dhari

How to get DataGrid row value without postback or AJAX.
C# (C# 1.0, C# 2.0, C# 3.0), Javascript, CSS, HTML, Windows (Win2K, WinXP, Win2003, Vista), .NET, ASP, ASP.NET, ADO.NET, WinForms, Dev
Posted:29 Mar 2008
Views:22,185
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 3.23 Rating: 4.63 out of 5

1

2

3
3 votes, 60.0%
4
2 votes, 40.0%
5

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)

About the Author

murtaza dhari


Member
Murtaza Tahir Ali Dhari
Student of FAST-NU karachi,pakistan

If all the tree become pen and all ocean become ink and many more other ocean become ink even then the ALLAH praise could not be written completely.
(AL-Quran)

Yes, I Love ALLAH .ALLAH is the fountain of my life and my savior ALLAH keeps me going day and night without ALLAH i am no one but with ALLAH i can do every thing ALLAH is my strength.
Location: Pakistan Pakistan

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralAbout DataGrid View Pinmembermarifdu10:17 27 Oct '08  
GeneralRe: About DataGrid View Pinmembermurtaza dhari18:07 27 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Mar 2008
Editor: Smitha Vijayan
Copyright 2008 by murtaza dhari
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project