Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

Display gridview data on click

I have a gridview that becomes quite long with some information that could be somewhat hidden.

Here's my asp:

<asp:GridView ID="gvLogBody" runat="server" CssClass="Grid" AllowPaging="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="true">
</asp:GridView>

And here's my code behind:

DataSet ds = new DataSet();
ds.Tables.Add("LogBody");
ds.Tables["LogBody"].Columns.Add("timeStamp");
ds.Tables["LogBody"].Columns.Add("name");
ds.Tables["LogBody"].Columns.Add("message");
foreach (LogObject l in logLines)
{
    ds.Tables["LogBody"].Rows.Add(l.TimeStamp, l.Name, l.Message);
}
gvLogBody.DataSource = ds.Tables["LogBody"].DefaultView;
gvLogBody.DataBind();

This gives me a gridview that looks like this:

____________________________________________________________________________________________
|timeStamp|                      name                     |            message             |
+---------+-----------------------------------------------+--------------------------------+
|01-01-01 | someLongAndQuiteUnnecesaryNameThatIWishToHide | someMessageThatIsMoreImportant |
+---------+-----------------------------------------------+--------------------------------+

What I want is something like this:

_________________________________________________________
|timeStamp|    name     |            message             |
+---------+-------------+--------------------------------+
|01-01-01 | clickToShow | someMessageThatIsMoreImportant |
+---------+-------------+--------------------------------+

And once the user clicks the text, it expands / opens a popup or something.

How can this be done?

Upvotes: 0

Views: 65

Answers (2)

Sid M
Sid M

Reputation: 4354

Why not use jQueryUI Dialog for good look and feel of popup

<head runat="server">
     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>

  <script>
      function openPopup(name) {

          $('#<%= lblName.ClientID %>').text(name);
          $("#dialog").dialog();
          return false;
      };
  </script>
</head>

then in body

<asp:GridView ID="gvLogBody" runat="server" CssClass="Grid" AllowPaging="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="timeStamp" HeaderText="timeStamp" />
                        <asp:TemplateField HeaderText="name">
                            <ItemTemplate>
                                <a href="#" onclick='javascript:return openPopup("<%#Eval("name") %>");'>
                                <%#Eval("name")%>
                            </a>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="message" HeaderText="message" />
                    </Columns>
                </asp:GridView>
 <div id="dialog" title="Basic dialog">
        <asp:Label ID="lblName" runat="server" ></asp:Label>
</div>

Upvotes: 1

Ondipuli
Ondipuli

Reputation: 468

This piece of code will help you out.

<asp:GridView ID="gvLogBody" runat="server" CssClass="Grid" AllowPaging="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="timeStamp" HeaderText="timeStamp" />
                        <asp:TemplateField HeaderText="name">
                            <ItemTemplate>
                                <input type="button" value="clickToShow" onclick="alert('<%#Eval("name") %>')" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="message" HeaderText="message" />
                    </Columns>
                </asp:GridView>

Upvotes: 1

Related Questions