【问题标题】:Display gridview data on click点击时显示gridview数据
【发布时间】:2014-07-03 10:59:35
【问题描述】:

我有一个很长的网格视图,其中包含一些可能有些隐藏的信息。

这是我的 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();

这给了我一个看起来像这样的网格视图:

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

我想要的是这样的:

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

一旦用户点击文本,它就会展开/打开一个弹出窗口或其他东西。

如何做到这一点?

【问题讨论】:

  • 您最好使用 javascript/jquery,并避免回传点击。现在,如果您使用 javascript 找到一个弹出库,然后决定是否使用 ajax 来获取信息,或者您将它放在页面中并显示()/隐藏()它。

标签: c# asp.net gridview


【解决方案1】:

这段代码可以帮到你。

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

【讨论】:

  • 说 System.Web.UI.WebControls.GridView 没有公共属性 BoundField 和 TemplateField 和 ItemTemplate
  • 哦,nvm。我忘了把它放在 之间
  • 我明白了。它像魅力一样起作用吗?如果它有效,那么接受我的回答是正确的。
【解决方案2】:

为什么不使用 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>

然后在正文中

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

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2021-08-17
    • 2011-01-23
    相关资源
    最近更新 更多