【问题标题】:ASP.NET (VB): GridView with ImageButton > Populate text box with commentsASP.NET (VB):带有 ImageButton 的 GridView > 用注释填充文本框
【发布时间】:2010-08-31 18:38:25
【问题描述】:

我已经很久没有用 VB 编写页面了。对于我的生活,我只是不记得如何做到这一点。

我在 ASP 页面上有一个 GridView。每行都有一个注释 ImageButton。我已经将网格视图和文本框包裹在 UpdatePanel 中。网格视图显示所有正确信息。当用户单击该行的 ImageButton 时,我只需要添加一些代码来填充带有注释的文本框。

如果有区别,这些 cmets 将存储在 SQL 2005 DB 中。我应该将 cmets 推到 gridview 的隐藏字段中,还是有一个函数可以让我查询 db 以获取该特定评论。

最终目标是尽可能不刷新页面。

非常感谢您的帮助,帮助我克服这个作家的障碍!

【问题讨论】:

  • 你能发布你的标记吗?不知道我明白你想要做什么......

标签: asp.net vb.net gridview updatepanel


【解决方案1】:

好的,您可以通过HiddenField 或在数据库中查找来做到这一点。

隐藏字段

在包含 ImageButton 的 ItemTemplate 中,为 ImageButton 添加 CommandName 和 CommandArgument 属性,以及从数据库中绑定到评论字段的 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>

在代码隐藏中,为 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 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

数据库查找

为 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

【讨论】:

  • 太好了,我明天试试。我想这正是我想要的。
【解决方案2】:

这是标记...

        <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 中。希望能更好地解释它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2013-03-20
    • 2017-07-26
    相关资源
    最近更新 更多