致命的HeaderTempate,導致ItemDataBound中e.Item.FindControl失效(修正)

來源:互聯網
上載者:User

我們在使用DataList時常常用到ItemDataBound事件,但是這次我在使用中
e.Item.FindControl失效了,在DataList中放置的一個HyperLink控制項,
通過e.Item.FindControl怎麼也找不到了,我查看代碼,沒有錯誤,
最後才發現只要在HTML部分含有<HeaderTemplate></HeaderTemplate>就會失效,
刪除後正常。查了資料,沒找到(不想找了)

HTML Part:

<asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server"
                    Width="584px" Height="208px">
                    <HeaderTemplate>
                    <!--錯誤就在這裡 begine-->
                    </HeaderTemplate>
                    <ItemTemplate>
                        <!--錯誤就在這裡 end-->
                        <asp:HyperLink id=HyperLink1 runat="server" Width="520px" Text='<%# DataBinder.Eval(Container.dataItem,"companyName") %>'>
                        </asp:HyperLink>
                    </ItemTemplate>
                </asp:datalist>

Codebehind Part:

Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim t As New DataTable
        Dim cm As New SqlCommand
        cm.CommandText = "select * from customers"
        cm.CommandType = CommandType.Text
        cm.Connection = SqlConnection1
        Dim ad As New SqlDataAdapter
        ad.SelectCommand = cm
        ad.Fill(t)
        DataList1.DataSource = t
        DataList1.DataBind()
    End Sub
    Private Sub DataList1_ItemDataBound()Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        Dim hl As HyperLink
        '在這裡會出錯
        hl = CType(e.Item.FindControl("HyperLink1"), HyperLink)
        hl.Text += "ddd"
    End Sub    

看了Meyer的回複:

我明白了,在執行時它是對所有的ItemType的進行查詢,當我添加 HeaderTempate時候,它首先對ListItemType.Head 查詢,這時當然用FindControl時找不到HyperLink1,於是下面自然會報沒有執行個體化的錯誤了。改善的代碼是。
要注意是在ItemTemplate中含有兩種ListItemType,分別是AlternatingItem和Item                    Private Sub DataList1_ItemDataBound()Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        Dim hl As HyperLink
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            hl = CType(e.Item.FindControl("HyperLink1"), HyperLink)
            hl.Text += "ddd"
        End If
    End Sub

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.