0

i am trying to pass a value to a label from a data source, but it keeps on giving me this error.

form1.aspx:

 <asp:FormView ID="ProductsView" DataSourceID="ProductDataSource" DataKeyNames="Parking_ID"
                runat="server" >
                <ItemTemplate>
                    <div style="width: 200px; height: 200px; border: solid 10px white; background-position: center;
                        background-repeat: no-repeat; background-image: url('<%# Eval("Parking_ID", "images/{0}.bmp") %>');">
                    </div>
                    <asp:HiddenField Value='<%# Eval("Parking_Cost") %>' ID="hiddenPrice" runat="server" />
                    <asp:HiddenField Value='<%# Eval("Parking_Name") %>' ID="hiddenName" runat="server" />
                    <asp:HiddenField Value='<%# Eval("Parking_ID") %>' ID="HiddenField1" runat="server" />
                </ItemTemplate>
            </asp:FormView>
             <asp:Label ID="lblName" runat="server" Text="TEST" CssClass="title" Style="font-size: 17px;
                display: block;"></asp:Label>
            <asp:Label ID="lblPrice" runat="server" Text="TEST" CssClass="title" Style="color: Red;
                font-size: 12px;"></asp:Label>
                <asp:Label ID="lbl_ID" runat="server" Text="" CssClass="title" Style="color: Red;
                font-size: 12px;"></asp:Label>
                <asp:SqlDataSource ID="ProductDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:mydatabaseConnectionString2 %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Parking] WHERE ([Parking_ID] = @Parking_ID)">
    <SelectParameters>
        <asp:Parameter Name="Parking_ID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

form1.aspx.vb:

lblName.Text = DirectCast(Me.ProductsView.FindControl("hiddenName"), HiddenField).Value

I am using vb.net script

Any ideas ?

Regards.

1

3 Answers 3

1

The Controls are usually in the data row of the FormView.

Try

lblName.Text = DirectCast(Me.ProductsView.Row.FindControl("hiddenName"), HiddenField).Value

But in general, when you use Control.FindControl(), there is a chance it will return Nothing if it cannot find it. So the best thing to do is do a TryCast instead of DirectCast and check to see if it's Nothing.

Sign up to request clarification or add additional context in comments.

Comments

1

I guess the error source is the following statement:

Me.ProductsView.FindControl("hiddenName")

And the reason is that you dont have a Control with the ID of "hiddenName" directly in the ProductsView control.

You may need to use a recursive function to get the control ..

Comments

0

Is this code in the bind event?

lblName.Text = DirectCast(Me.ProductsView.FindControl("hiddenName"), HiddenField).Value

If so, it might be that you have to check which template it is running:

if (e.Item.ItemType == ItemType || e.Item.ItemType == AlternateItemType)

(I'm not sure of the types for FormView, but in a GridView it would be like this.

The control hiddenName doesn't existing on other templates.

Also you have to find the control from e.Item which is the current template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.