我有一个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>
答案 0 :(得分: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上做了,这是你的接收命令,代码完全相同,并将在提到的两个事件上工作!!