2010-08-31 26 views
0

它已經很長時間了,因爲我已經在VB中編寫了一個頁面。對於我的生活,我不記得如何做到這一點。ASP.NET(VB):帶ImageButton的GridView>填充帶註釋的文本框

我在ASP頁面上有一個GridView。每行都有一個註釋ImageButton。我已經在UpdatePanel中包裝了gridview和文本框。 gridview顯示所有正確的信息。我只需要添加一些代碼來在用戶點擊該行的ImageButton時用註釋填充文本框。

註釋存儲在SQL 2005數據庫中,如果這有所幫助。我應該在GridView的隱藏字段中推送註釋,還是有一個函數可以讓我查詢數據庫中的特定註釋。

最終目標是儘可能不刷新頁面。

非常感謝您的幫助,以幫助我克服這位作家的障礙!

+0

您可以發佈您的標記?不知道我得到你想要做的... – PhilPursglove 2010-08-31 18:53:12

回答

1

好的,您可以通過HiddenField或通過在數據庫中查找來實現。

HiddenField

在包含ImageButton的ItemTemplate中,添加的CommandName和CommandArgument屬性到ImageButton的,並綁定到註釋字段從數據庫中HiddenField:

<asp:TemplateField HeaderText="Comment"> 
    <ItemTemplate> 
     <asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" CommandName="SelectComment" CommandArgument='<%# Container.DataItemIndex %>' /> 
     <asp:HiddenField runat="server" id="CommentHiddenField" Value='<%# Eval("Comment") %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

在代碼-behind,添加一個方法用於處理RowCommand事件爲GridView:

Private Sub Gridview2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview2.RowCommand 

    Dim rowIndex As Integer 
    Dim commentHiddenField As HiddenField 

    If e.CommandName = "SelectComment" Then 
     rowIndex = Integer.Parse(e.CommandArgument.ToString) 

     commentHiddenField = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("CommentHiddenField"), HiddenField) 

     txtComments.Text = commentHiddenField.Value 
    End If 

End Sub 

DB查詢

添加屬性到的ImageButton:

<asp:TemplateField HeaderText="Comment"> 
    <ItemTemplate> 
     <asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" CommandName="SelectComment" CommandArgument='<%# Container.DataItemIndex %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

在後臺代碼,添加一個方法來處理爲GridView的RowCommand事件:

Private Sub Gridview2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview2.RowCommand 

    Dim rowIndex As Integer 
    Dim key As String 

    rowIndex = Integer.Parse(e.CommandArgument.ToString) 

    key = Gridview1.DataKeys(rowIndex).Value.ToString 

    txtComments.Text = GetCommentFromDB(key) 
End Sub 
+0

偉大的我明天會試試這個。我認爲這正是我正在尋找的。 – 2010-09-01 02:11:14

0

這裏是標記...

 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
     <tr class="dvrow" align="center"> 
     <td style="text-align:left;" colspan="2">History<br /> 
      <div id="div1" style="width:600px;"> 
      <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        GridLines="None" DataKeyNames="RepIssueHistoryID" 
       DataSourceID="sqlIssueHistory" Width="600px" AllowSorting="True" 
        AllowPaging="True" CssClass="grid" RowStyle-Height="15px"> 
       <PagerStyle CssClass="footer" /> 
       <Columns> 
        <asp:TemplateField HeaderText="Mail"> 
         <ItemTemplate> 
          <asp:CheckBox ID="chkMailComment" runat="server" /> 
         </ItemTemplate> 
        </asp:TemplateField> 
       <asp:BoundField DataField="RepIssueStatus" SortExpression="RepIssueStatus" 
         HeaderText="Status" ItemStyle-Width="120px" > 
<ItemStyle Width="120px"></ItemStyle> 
        </asp:BoundField> 
       <asp:BoundField DataField="RepIssuePriority" SortExpression="RepIssuePriority" 
         HeaderText="Priority" ItemStyle-Width="100px" > 
<ItemStyle Width="100px"></ItemStyle> 
        </asp:BoundField> 
       <asp:BoundField DataField="User" SortExpression="User" HeaderText="Rep" 
         ItemStyle-Width="180px" > 
<ItemStyle Width="180px"></ItemStyle> 
        </asp:BoundField> 
       <asp:BoundField DataField="DateUpdate" SortExpression="DateUpdate" 
         HeaderText="Date" ItemStyle-Width="200px" > 
<ItemStyle Width="200px"></ItemStyle> 
        </asp:BoundField> 
        <asp:TemplateField HeaderText="Comment"> 
        <ItemTemplate> 
         <asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" /> 
        </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Attachment"> 
        <ItemTemplate> 
         <asp:ImageButton ID="btnAttachment" runat="server" ImageUrl="images/folder.gif" /> 
        </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 
       <EmptyDataTemplate> 
        No history currently exists.<br /> 
       </EmptyDataTemplate> 
    <RowStyle Height="15px"></RowStyle> 

       <EmptyDataRowStyle CssClass="empty" /> 
      </asp:GridView> 
      </div> 
      </td> 
     </tr> 

     <tr class="dvrow" align="center"> 
      <td style="text-align:left;" colspan="2">Rep Comment<br /> 
      <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" 
      Width="600px" Height="180px" ReadOnly="true" /> 
      </td> 
     </tr> 
</ContentTemplate> 
     </asp:UpdatePanel> 

所以基本想法是有人在btnComment點擊取其行,然後該意見將在txtComment顯示出來。希望能更好地解釋它。