Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I wanted to find a way to show details of a datagrid row in the same row as opposed to binding a new query to a separate datagrid.  To do this I had to bind my query results to a datagrid, hide the results that I did not want displayed behind a style tag, and then toggle the style as needed to show or hide the results.

Overview

 Using a simple Page_Load event to bind Xml data to a datagrid and display the results.

public void Page_Load(Object sender, EventArgs e)
{
  string books = Server.MapPath("aspnetbooks.xml");
  DataSet dataSet = new DataSet();
  dataSet.ReadXml(books);
  dgBooks.DataSource = dataSet;
  dgBooks.DataBind();
}        

Once the data is displayed, use the JavaScript below in combination with the datagrid to manipulate the unique Ids and show or hide the details.

Using the code

Javascript Code

This script first determines the browser version being used in order to then ascertain which method it should use to manipulate the elements in the HTML.  Next, it loops through the arguments and sets up an element object to change the visibility to hidden or visible.  The following code illustration shows the DataGrid itself and then explains the details behind calling the showDetails function.

<script type="text/javascript">
var action;
IE4 = (document.all && !document.getElementById) ? true : false;
NS4 = (document.layers) ? true : false;
IE5 = (document.all && document.getElementById) ? true : false;
NS6 = (!document.all && document.getElementById) ? true : false;

function showDetails(action)
{
   x = 0;
   changeText(arguments[x]);
    if (IE4) { prefix = document.all(arguments[x]).style; }
    if (NS6 || IE5) { prefix = document.getElementById(arguments[x]).style }
    if (NS4){ prefix = document.layers(arguments[x]); }

    if (prefix.visibility == "visible")
    {
       prefix.visibility = "hidden";
       prefix.fontSize = "0";
    }
    else
    {
       prefix.visibility = "visible";
       prefix.fontSize = "16";
    }
}

function changeText(link)
{
  link += "link";
  linkText = document.getElementById(link);
  linkText.firstChild.nodeValue;

  if (linkText.firstChild.nodeValue == "Show Details")
  {
    linkText.firstChild.nodeValue = "Hide Details";
  }
  else
  {
    linkText.firstChild.nodeValue = "Show Details";
  }
}
</script>
        

DataGrid Code

This datagrid is pretty simple.  autoGenerateColumns is set to False in order to manipulate the columns manually.  First, notice line #9.  This is where the showDetails function is called.  Using the DataBinder.Eval to bind a unique database record ID to each function; that is the same record ID used on line #20-#22 to set the id of a cell in the table and the same unique ID assigned to the "a href" tag except it has "link" appended to the ID to keep it unique. 

Notice that Row #14 has a style that sets the font-size to "0" and the visibility to hidden.  This is what the JavaScript above will toggle.  The Url is set to call "showDetails('UniqueID of the row')".  Once the function has performed browser detection, a call is made to another function: changeText('Unique ID of the row').  This function appends the "link" to the ID and changes the text to either "Show Details" or "Hide Details" by checking the innerText of the href tag.  The showDetails function then resumes by changing the visibility and font-size style according to it current state.

1.  <asp:DataGrid id="dgBooks" runat="server" Height="35px"
       autoGenerateColumns="False" BorderWidth="2px">
2.  <AlternatingItemStyle  forecolor="#99CC99"
       backcolor= "#FFEADE"></AlternatingItemStyle>
3.  <ItemStyle forecolor="#FFCC99" backcolor= "#99AA99"></ItemStyle>
4.  <Columns>
5.  <asp:TemplateColumn>
6.  <ItemTemplate>
7.  <table border="0" id="Table1">
8.  <tr>
9.  <td>
10. <a id='<%# DataBinder.Eval(
       Container.DataItem, "bookID") %>link' 
       href="javascript:showDetails('<%# DataBinder.Eval(
       Container.DataItem, "bookID") %>')">
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralEditable Detail Fields ?
TheDonSansone
18:04 19 Jan '09  
Would it be possible to take this one step further and introduce bound editable detail fields for easy update ?
GeneralProblem with DataGrid inside web user control
Chuck Lane
10:07 5 Aug '04  
Dear Readers,

I have several Web Forms pages and a user control that chooses Code/Value pairs for the fields on the Web Forms Pages. On the pages where I predefine a panel in Visual Studio designer and drag an instance of the user control onto that panel, and then use this.FindControl in my Web form to find the user control, the user control works well including the event/delegates combos to talk back to the Web Form and update the new values that are chosen on the user control. However on pages where I attempt to dynamically create the user control and panel using LoadControl and put the panel on the page where I want at runtime, I am encountering a variety of problems. First of all these dynamically created user controls do not seem to call the OnInit function so the handlers that I set these user controls in visual studio d don't ever seem to actually stick once it's on the page unless I set them from the Web form that's creating the user control explicitly in code (for example from the OnInit function of the Web Forms Page). The Other problem is a little more cryptic... in my Datagrids ( which is basically what the user control consists of ) I had a Select Linkbutton set as the first column in each row and then in the itembound callback i set the "onclick" attribute as e.Item.Attributes["onclick"] = Page.GetPostBackClientHyperlink(button, "");.
As was stated before this all works fine and dandy on web forms pages where I have dragged a panel and user control onto them from vs designer, however if I set the select link button to Visible, the dynamically created (LoadControl) user control page won't load at all giving me the error user control 'DataGridLinkButton' must be placed inside a form tag with runat=server, if the select button isn't set to visible however, then the click simply refreshes the page and takes me back to just the web form with now user control at all. I know this may seem a little confusing, and it is, if there is anythign I can do to clear things up someone please let me know. I'm pulling my hair out here. Smile

thanks
chuck

GeneralRe: Problem with DataGrid inside web user control
Chuck Lane
10:45 6 Aug '04  
Well I finally managed to get it working, but not particularly the way I'd like. I switched back to a declaritive user control and panel and override the OnPreRender and set the Panel's Style attributes that way. This way i am able to move the panel around the screen with the user control to the places I want while maintaining the event handlers, etc inside the user control. I'm having a problem now with a textbox on the web form. I have an event set up for the Text Changed and the AutoPostBack property is set to true, however if you change the text inside the box and then hit enter instead of firing the textchange event it clicks on the button further down on the page. I know this is an easy fix, but what is it? thanks. Chuck

GeneralWhy didn't use a DataList???
c_marius
23:23 2 Mar '04  
I'm wonderring why don't you used a DataList for this type of view....
There are also other possibilities, but by example u could have defined the ItemTemplate of DataList as a 2 row Table...and just hide it with pretty much the same JS code....or...

Even more...u could have been defined a SelectedItemTemplate for the DataList..and nest within a DataGrid, either directly bound, or just on Select Command......
GeneralShow first row details
gadgetfbi
8:41 22 Jul '03  
Thanks, this has been realy usefull.......but (hehe) can you think of a way to expand (show) the first row of the dataGrid on pageLoad.


tia
GeneralShow Embeded DataGrid?
GKFlash
5:58 17 Jul '03  
Can you use this to show/hide an embeded DataGrid?
GeneralThanks! :cool:
mememe24
15:36 2 Jul '03  
Big Grin
FINALLY someone teaches me how to do this! Ic ouldn't find out how anywhere!


Last Updated 8 Jul 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010