如何手动设置一些listView insertItem数据(在代码中)? (简单但需要帮助)

发布于 2024-08-14 06:27:45 字数 1047 浏览 8 评论 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>

hi i have an insertItemTemplate as follows, and all i want to do is to programmatically add all the values myself, without asking the user, of course, the userID, picID and dateTime should not be asked to the user, the comments field of course, i want to ask the user as they are leaving a comment about a picture on the site :)... seems simple but really frustrating.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-08-21 06:27:45

我尝试了以下操作,它有效,

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 上执行此操作,当您收到命令时,代码完全相同,并且适用于提到的两个事件!

i tried the following and it worked

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

you can do it on the ItemCreated event, but then it alters the data sent to the browser, and then this data is going to be sent back (unnecessary round trip), so i did it on ItemCommand, which is when you receive the command, the code is exactly the same and will work on both of the events mentioned!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文