0

嗨,我有一个 insertItemTemplate 如下,我想做的就是自己以编程方式添加所有值,而不询问用户,当然,不应该向用户询问 userID、picID 和 dateTime,当然是评论字段,我想问用户,因为他们正在网站上留下关于图片的评论 :)... 看起来很简单,但真的很令人沮丧。

<InsertItemTemplate>
 <span style="">UserID:
 <asp:TextBox Visible="false" ID="UserIDTextBox" runat="server" Text='<%# Bind("UserID") %>' />
 <br />CommentForPicID:
 <asp:TextBox Visible="false" ID="CommentForPicIDTextBox" runat="server" 
  Text='<%# Bind("CommentForPicID") %>' />
 <br />Comment:
 <asp:TextBox TextMode="MultiLine" ID="CommentTextBox" runat="server" Text='<%# Bind("Comment") %>' />
 <br />DateAdded:
 <asp:TextBox Visible="false" ID="DateAddedTextBox" runat="server" 
  Text='<%# Bind("DateAdded") %>' />
 <br />
 <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
  Text="Insert" />
 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
  Text="Clear" />
 <br /><br /></span>
</InsertItemTemplate>
4

1 回答 1

0

我尝试了以下方法,它奏效了

Protected Sub lvComments_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvComments.ItemCommand
    If e.Item.ItemType = ListViewItemType.InsertItem Then
        Dim tb = New TextBox
        tb = e.Item.FindControl("UserIDTextBox")
        If tb IsNot Nothing Then
            tb.Text = Request.Cookies("UserID").Value
        End If
        tb = Nothing

        tb = e.Item.FindControl("CommentForPicIDTextBox")
        If tb IsNot Nothing Then
            tb.Text = Request.Cookies("ShownPicID").Value
        End If
        tb = Nothing

        tb = e.Item.FindControl("DateAddedTextBox")
        If tb IsNot Nothing Then
            tb.Text = DateTime.Now.ToString
        End If
        tb = Nothing

    End If
End Sub

您可以在 ItemCreated 事件上执行此操作,但随后它会更改发送到浏览器的数据,然后此数据将被发回(不必要的往返),所以我在 ItemCommand 上执行此操作,也就是您收到命令时, 代码是完全相同的,将适用于提到的两个事件!!

于 2009-12-13T18:52:17.867 回答