2010-08-31 53 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显示出来。希望能更好地解释它。