come impostare alcuni dei dati listView InsertItem manualmente (in codice)? (Semplice ma bisogno di aiuto)
-
19-09-2019 - |
Domanda
Ciao ho un InsertItemTemplate come segue, e tutto quello che voglio fare è aggiungere a livello di codice tutti i valori di me stesso, senza chiedere l'utente, naturalmente, l'userID, picID e dateTime non dovrebbe essere chiesto per l'utente, i commenti campo, naturalmente, voglio chiedere all'utente come stanno lasciando un commento su una foto sul sito:.) ... sembra semplice ma davvero frustrante
<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>
Soluzione
ho provato quanto segue e ha funzionato
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
si può fare sull'evento ItemCreated, ma poi si altera i dati inviati al browser, e quindi questi dati sta per essere rispedito (inutili andata e ritorno), quindi l'ho fatto ItemCommand, che è quando si ricevere il comando, il codice è esattamente lo stesso e funziona su entrambi gli eventi menzionati !!