Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

¿ When To Use "e.Item.FindControl()" ? 1

Status
Not open for further replies.

MCuthill

Technical User
Jul 19, 2006
669
CA
Hi Everyone,

It was recommended I post this in the ASP forum instead of the Visual Basic forum. [pc2]

Perhaps the solution to this is more simple than I am making it out to be. I am trying to get the contents of a field within a DataList to assign to a variable, and thought I could use e.Item.FindControl(). After several attempts and variations on the code I still get a Null return from fields I know to be populated.

Can I get the text contained from a field for a particular Item using this method, or do I need to use an SQLReader to obtain the information?

I am using Visual Basic Code Behind on ASP.NET pages (using Visual Studio 2005)

Thanks,


Mike
"It Seems All My Problems Exist Between Keyboard and Chair"
 
Can you post the aspx portion that contains your DataList?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I am coding the event in
Code:
Private Sub SearchResults_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles SearchResults.ItemCommand

Here is the ASPX for the DataList.

Code:
 <asp:DataList ID="SearchResults" runat="server" DataSourceID="SqlDataSource1"
        Font-Bold="True" Style="color: white" Width="536px" DataKeyField="ResourceID">
        <ItemTemplate>
            <asp:Label ID="ResourceIDLabel" runat="server" Text='<%# Eval("ResourceID") %>' Visible="False"></asp:Label>&nbsp;
            <asp:Label ID="ActivityTypeIDLabel" runat="server" Text='<%# Eval("ActivityTypeID") %>'
                Visible="False"></asp:Label><br />
            <asp:Label ID="TitleLabel" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="14pt"
                Text='<%# Eval("Title") %>'></asp:Label><br />
            by:
            <asp:Label ID="AuthorLabel" runat="server" Text='<%# Eval("Author") %>'></asp:Label><br />
            <br />
            Description:
            <asp:Label ID="DescriptionLabel" runat="server" Font-Bold="False" Font-Italic="False"
                Text='<%# Eval("Description") %>'></asp:Label><br />
            <br />
            Format:
            <asp:Label ID="FormatLabel" runat="server" Font-Bold="False" Text='<%# Eval("Format") %>'></asp:Label><br />
            Date Signed Out:
            <asp:Label ID="DateSignedOutLabel" runat="server" Font-Bold="False" Text='<%# Eval("DateSignedOut", "{0:d}") %>'></asp:Label><br />
            Loan Period:
            <asp:Label ID="LoanPeriodLabel" runat="server" Font-Bold="False" Text='<%# Eval("LoanPeriod") %>'></asp:Label><br />
            Signed Out To:
            <asp:Label ID="SignedOutToLabel" runat="server" Font-Bold="False" Text='<%# Eval("SignedOutTo") %>'></asp:Label><br />
            <br />
            <asp:Button ID="cmd_SignOut" runat="server" Text="Sign Out This Resource" Width="240px" commandname="SignOut" Visible='<%# Container.DataItem("ResourceID") %>' /><br />
            &nbsp;
        </ItemTemplate>
        <ItemStyle BorderStyle="Solid" BorderWidth="1px" />
    </asp:DataList>

Mike
"It Seems All My Problems Exist Between Keyboard and Chair"
 
ok.. so where is the code you are placing in the event that is causing the error or not working
 
I found examples of using "Dim TitleLbl As Label = CType(e.Item.FindControl("Title"), Label)" on the web, MSDN I think. In my situation they are always returning NULL, I also have tried using e.item.parent.findcontrol("title").

Code:
Private Sub SearchResults_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles SearchResults.ItemCommand
        Dim primaryKey As Integer = e.Item.ItemIndex
        'Pull Contents of the Resources Fields
        Dim TitleLbl As Label = CType(e.Item.FindControl("Title"), Label)
        Dim AuthorLbl As Label = CType(e.Item.FindControl("Author"), Label)
        Dim FormatLbl As Label = CType(e.Item.FindControl("Format"), Label)
        'Cast Labels to String Values
        Dim Title As String = Val(TitleLbl)
        Dim Author As String = Val(AuthorLbl)
        Dim Format As String = Val(FormatLbl)

        MsgBox("Object Selected: " & primaryKey & ", " & Title, MsgBoxStyle.OkOnly, "Object Selected")
    End Sub

Mike
"It Seems All My Problems Exist Between Keyboard and Chair"
 
i am surprised you arenot getting "object not set to instance of an object" error
You are trying to find the dataset columns, not the controls.

Change:
Code:
  Dim TitleLbl As Label = CType(e.Item.FindControl("Title"), Label)
        Dim AuthorLbl As Label = CType(e.Item.FindControl("Author"), Label)
        Dim FormatLbl As Label = CType(e.Item.FindControl("Format"), Label)

To:
Code:
  Dim TitleLbl As Label = CType(e.Item.FindControl("[b]TitleLabel[/b]"), Label)
        Dim AuthorLbl As Label = CType(e.Item.FindControl("[b]AuthorLabel[/b]"), Label)
        Dim FormatLbl As Label = CType(e.Item.FindControl("[b]FormatLabel[/b]"), Label)

Jim
 
Thanks Jim, [thumbsup]

I had to change the code a little to get the value as a string. The MsgBox is just used to test my code, and all values show.

Below is the working code.
Code:
Private Sub SearchResults_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles SearchResults.ItemCommand
        Dim primaryKey As Integer = e.Item.ItemIndex
        'Pull Contents of the Resources Fields
        Dim Title As String = CType(e.Item.FindControl("TitleLabel"), Label).Text
        Dim Author As String = CType(e.Item.FindControl("AuthorLabel"), Label).Text
        Dim Format As String = CType(e.Item.FindControl("FormatLabel"), Label).Text

        MsgBox("Object Selected: " & primaryKey & ", " & Title & ", " & Author & ", " & Format, MsgBoxStyle.OkOnly, "Object Selected")
    End Sub
[peace]

Mike
"It Seems All My Problems Exist Between Keyboard and Chair"
 
Glad it is working :) and glad to help.

Just remember that a messagbox only shows on the server, NOT the client, so your code wouldn't work if your PC was not the server. Just a little side note for you to consider.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top