views:

314

answers:

2

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

have you tried to do it on databinding?

darthjit
thats a neat idea, do you get better access to the database colum/row elements when doing it on databinding or...? as i think the listviews events give poor access to the linq data source elements.
Erx_VB.NExT.Coder
+1  A: 

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

Erx_VB.NExT.Coder