Link to home
Start Free TrialLog in
Avatar of ncrocker
ncrockerFlag for United States of America

asked on

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...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Context...we need context...where and when do you need this value?  Do you need this on the page load?
Avatar of ncrocker

ASKER

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

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
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yay!!! Thank you EVER SO MUCH!!!!!!!
You just made my day....

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?