2011-03-02 61 views
0

我需要能夠從我的detailsview編程數據綁定上修改控件。現在我使用這段代碼,但我得到一個「索引超出範圍」的錯誤。如何以編程方式從detailsview訪問控件?

Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound 
    Dim resumeLink As HyperLink = dtlApplication.Rows.Item(0).FindControl("lnkResume") 
    resumeLink.NavigateUrl = "Resumes/" 
End Sub 

我也試過這個,但得到了「對象引用未設置爲對象的實例」錯誤。

Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound 
    Dim resumeLink As HyperLink = dtlApplication.FindControl("lnkResume") 
    resumeLink.NavigateUrl = "Resumes/" 
End Sub 

我認爲這個問題可能是在DetailsView沒有任何控件當頁面最初加載,因爲它沒有得到他們,直到我在我的主要的GridView中選擇一行。基本上,我試圖在gridview中選擇一行時執行此代碼,而不是在頁面最初加載時執行此代碼。這可能嗎?如果是這樣,如果不在detailsview數據綁定中,我應該在哪裏執行此代碼?

這裏是在DetailsView和相應的數據源標記:

<asp:DetailsView ID="dtlApplication" runat="server" AutoGenerateRows="false" 
         DataKeyNames="appID" DataSourceID="ds2" CellPadding="0" BorderColor="Transparent" 
         BorderWidth="0px" GridLines="None" HorizontalAlign="Left" Width="459" CssClass="dtlView"> 
         <Fields>         
          <asp:TemplateField showheader="false"> 
           <ItemTemplate> 

            <h3>Resume</h3> 

            <asp:HyperLink runat="server" ID="lnkResume" Text="View Resume &raquo;"></asp:HyperLink>           

           </ItemTemplate> 
          </asp:TemplateField>         
         </Fields> 
         <PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="5" FirstPageText="&larr; First" LastPageText="Last &rarr;" 
          nextpagetext="Next &raquo;" previouspagetext="&laquo; Previous" /> 
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" CssClass="paging" /> 
        </asp:DetailsView> 
<asp:SqlDataSource ID="ds2" runat="server" ConnectionString="<%$ ConnectionStrings:cn %>" 
         SelectCommandType="StoredProcedure" SelectCommand="sp_SelectApplicationDetail" 
         EnableCaching="true" CacheDuration="600"> 
         <SelectParameters> 
          <asp:ControlParameter Name="appID" ControlID="gvAdmin" PropertyName="SelectedValue"></asp:ControlParameter> 
         </SelectParameters>   
        </asp:SqlDataSource> 
+0

您可以發佈您的detailsview標記嗎? – Alex 2011-03-02 20:09:25

+0

我用標記更新了問題 – 2011-03-02 20:20:04

回答

1

DetailsView的數據源使用GridView的的SelectedValue,因爲它是選擇控制參數,並在頁面加載gridview的沒有的selectedIndex還,所以在DetailsView是空的。我必須在頁面加載時設置gridview的selectedindex來解決問題。

0

看來,數據綁定事件不是針對這樣的問題,一個最好的事件。嘗試使用ItemCreated event事件處理程序。喜歡這裏,例如:

Private Sub dtlApplication_ItemCreated(sender As Object, e As EventArgs) Handles dtlApplication.ItemCreated 

    Dim someRow As DetailsViewRow = dtlApplication.Rows(0); 
    If someRow Is Nothing Then Exit Sub  
    Dim link As HyperLink = DirectCast(someRow.FindControl("lnkResume"), HyperLink) 

    If link Is Nothing Then Exit Sub 

    link.NavigateUrl = "Resumes/" 
End 
+0

我試過了,仍然是同樣的錯誤。難道是detailsview的數據源使用我的主GridView的selectedvalue來填充它? – 2011-03-02 20:17:05

+0

這是可能的,但我不確定實際上resumeLink是什麼都沒有。這是因爲它沒有明確的數據綁定。 – apros 2011-03-02 21:50:00

+0

我試了你的代碼,第一行出現這個錯誤:「索引超出範圍,必須是非負數,小於集合的大小。」 參數名稱:index。「這告訴我,dtlApplication在ItemCreated事件上沒有任何行。我在正確的軌道上嗎? – 2011-03-03 13:54:53

0

您還可以將detailsview visible屬性設置爲false在頁面加載事件

相關問題