FormView FindControl Item

Hi Experts!
I use .net 3.5 vb; This project is using an Object Data Source (SQL 2005) DataTable Adapter...

Could someone please show how an accurate Sub would look for setting a cookie based on the value of a FormView Item DataBound Label?

My aspx has lblCustomerName the ID and Text as (eval) "CustomerName"... It pulls the data fine from a preceding page, I just have not found the correct code to set that Eval "CustomerName" as the cookie Value...
ncrockerAsked:
Who is Participating?
 
guru_samiConnect With a Mentor Commented:
Protected Sub fvCustomer_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles fvCustomer.DataBound
        Dim lbl As Label = CType(FormView1.FindControl("lblCustomerName"),Label)
        If (Not (lbl) Is Nothing) Then
            SetCookies(lbl.Text)
        End If
    End Sub

Private Sub SetCookies(ByVal customername As String)
        Dim CustomerCookies As HttpCookie = Response.Cookies("CustomerCookies")
       
        Response.Cookies("CustomerCookies")("lblCustomerName") = customername


    End Sub
0
 
Bob LearnedCommented:
Context...we need context...where and when do you need this value?  Do you need this on the page load?
0
 
ncrockerAuthor Commented:
I'm sorry; I am attaching Code Snippets.
It does not need to be in the PageLoad (I don't think?) I need to send it as a cookie to populate an Aspose.Words Document...
And the problem seems to be actually setting it. I'm assuming I need to call it as the FormView Control (Label) that it is and then convert to string for the cookie; or not, since the item controls are already a string value?

 <asp:FormView ID="fvCustomer" runat="server" DataKeyNames="customerID" DataSourceID="odsCustomerLetter">
                        <ItemTemplate>
                        <table border="0" id="customerTable" cellpadding="3" cellspacing="0" width="100%">
                         <tr>
                         <td colspan="2">
                          TO: <asp:Label ID="CustomerID" runat="server" 
                                    Text='<%# Eval("customerID") %>' Visible="False" />
                             
                        </td>
                    </tr>
                   <tr>   
                               <td colspan="2">
                               <asp:Label ID="lblCustomerName" runat="server" 
                                    Text='<%# Bind("CustomerName") %>' />
                               </td>
                               </tr>




 Private Sub SetCookies()
        Dim CustomerCookies As HttpCookie = Response.Cookies("CustomerCookies")
        
        Response.Cookies("CustomerCookies")("lblCustomerName") = Convert.ToString("CustomerName")


    End Sub


 Private Sub PopulateCustomerLetter(ByRef asposeDoc As Aspose.Words.Document)
        Dim licAsposeWord As New Aspose.Words.License

       
        If Convert.ToString(Request.Cookies("CustomerCookies")("CustomerMailCode")) <> "" Then
            asposeDoc.Range.FormFields("lblCustomerName").SetTextInputValue(Convert.ToString(Request.Cookies("CustomerCookies")("CustomerName")))
           

        End If

    End Sub

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Bob LearnedCommented:
If you need to get a value for a label, you can use the FindControl like this:

    Dim lblCustomerName As Label = TryCast(fvCustomer.Row.FindControl("lblCustomerName"), Label)
    If lblCustomerName IsNot Nothing Then
        Dim customerName As String = lblCustomerName.Text
    End If
0
 
ncrockerAuthor Commented:
Yay!!! Thank you EVER SO MUCH!!!!!!!
You just made my day....
0
 
ncrockerAuthor Commented:

Sorry, I need to "clarify" on the solution...
Because (and I will open another question for points if you'd like)... While your code technically worked... I cannot get multiple variables added..
You had: Private Sub SetCookies(ByVal customername As String)
Which would be swell if I only had to call that one label..
I modified the code to:
Private Sub SetCookies(ByVal sender As Object)
Dim lblCustomerName As String
Dim lblCustomerPhone As String
    Response.Cookies("CustomerCookies")("lblCustomerName") = customername
  Response.Cookies("CustomerCookies")("lblCustomerPhone") = customerPhone
 
End Sub  
But get "Warning" that there is an unused local variable. Now I know this is just a warning, however, my document will not populate... I've tried "Try and Catch" still doesn't work.
Can you help again and/or let me know if you want me to post it as a new question?
0
All Courses

From novice to tech pro — start learning today.