2014-07-03 61 views
0

我有一個gridview,變得相當長,可能有些隱藏的信息。在點擊時顯示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 | 
+---------+-------------+--------------------------------+ 

一旦用戶點擊文本,它會展開/打開一個彈出窗口或其他東西。

這怎麼辦?

+1

您更好地使用JavaScript/jQuery的是什麼,以及避免回傳點擊。現在,如果您使用javascript查找彈出式庫,然後決定是否使用ajax來獲取信息,或者您已在頁面中顯示並顯示()/ hide()它。 – Aristos

回答

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

說那個System.Web.UI.WebControls.GridView沒有公共屬性BoundField和TemplateField和ItemTemplate –

+0

oh,nvm。我忘了把它放在

+0

我看到了。它是否像魅力一樣工作?如果它起作用,那麼接受我的答案是正確的。 – Ondipuli

1

爲什麼不使用jQueryUI的對話框了良好的外觀和感覺彈出

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