Asp.net 如何手动(在代码中)设置某些listView插入项数据?(简单但需要帮助)
您好,我有一个insertItemTemplate,如下所示,我想做的就是自己以编程方式添加所有值,而不需要询问用户,当然,不应该向用户询问userID、picID和dateTime,当然是comments字段,我想在用户在网站上留下关于图片的评论时询问用户:)。。。看起来很简单,但确实令人沮丧Asp.net 如何手动(在代码中)设置某些listView插入项数据?(简单但需要帮助),asp.net,vb.net,listview,listviewitem,Asp.net,Vb.net,Listview,Listviewitem,您好,我有一个insertItemTemplate,如下所示,我想做的就是自己以编程方式添加所有值,而不需要询问用户,当然,不应该向用户询问userID、picID和dateTime,当然是comments字段,我想在用户在网站上留下关于图片的评论时询问用户:)。。。看起来很简单,但确实令人沮丧 <InsertItemTemplate> <span style="">UserID: <asp:TextBox Visible="false" ID="UserIDT
<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>
用户标识:
CommentForPicID:
评论:
已添加日期:
我尝试了以下方法,效果很好
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上执行了此操作,即当您接收到该命令时,代码完全相同,并且将在提到的两个事件上工作 您是否尝试过在数据绑定上执行此操作?这是一个好主意,在数据绑定上执行此操作时,您是否可以更好地访问数据库列/行元素,还是。。。?我认为ListView事件对linq数据源元素的访问能力较差。