come impostare alcuni dei dati listView InsertItem manualmente (in codice)? (Semplice ma bisogno di aiuto)

StackOverflow https://stackoverflow.com/questions/1893602

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>
È stato utile?

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top