点击显示gridview数据

本文关键字:数据 gridview 显示 | 更新日期: 2023-09-27 18:11:28

我有一个gridview,它变得很长,有些信息可以隐藏。

我的asp:

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

下面是我的代码:

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();

这给了我一个像这样的gridview:

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

我想要的是这样的:

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

一旦用户点击文本,它就会展开/打开一个弹出窗口之类的。

如何做到这一点?

点击显示gridview数据

这段代码将帮助您。

<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>

为什么不使用jQueryUI Dialog来获得良好的弹出窗口的外观和感觉

<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>

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>