2014-01-08 60 views
1


我用一個FormView顯示在我的網頁數據:
如何在FormView中獲取超鏈接的文本?

 <asp:FormView ID="PFull" runat="server" > 
    <ItemTemplate> 
     <div class="post_con"> 
      <h4 class="post-headers"><asp:HyperLink ID="hprPTitle" runat="server" NavigateUrl='<%#Eval("PID","~/Paper.aspx?pid={0}")%>'><%#Eval("PTitle")%></asp:HyperLink></h4> 
     </div> 
    </ItemTemplate> 
    </asp:FormView> 

綁定表單視圖:

public void ShowFullPaper(int id) 
{ 
    DataTable dt = paper.ShowFullPaper(Convert.ToInt32(Request.QueryString["pid"])); 
    PFull.DataSource = dt; 
    PFull.DataBind(); 
} 

現在,我想改變網頁的標題與該超鏈接的文本這樣的:

protected void PFull_DataBound(object sender, EventArgs e) 
{ 
    this.Title = ((HyperLink)PFull.FindControl("hprPTitle")).Text; 
} 

但就是不行。請幫我...
謝謝。

+0

這是什麼回報? –

+2

可能的重複 - http://stackoverflow.com/questions/6105634/cant-find-control-in-formview –

回答

2

修改您的aspx標記如下。

<asp:FormView ID="PFull" runat="server" > 
    <ItemTemplate> 
     <div class="post_con"> 
      <h4 class="post-headers"><asp:HyperLink ID="hprPTitle" runat="server" NavigateUrl='<%#Eval("PID","~/Paper.aspx?pid={0}")%>' Text='<%#Eval("PTitle")%>'></asp:HyperLink></h4> 
     </div> 
    </ItemTemplate> 
</asp:FormView> 

然後在您的ItemDataBound事件中,您可以在下面找到它。

protected void PFull_DataBound(object sender, EventArgs e) 
{ 
    this.Title = ((HyperLink)PFull.FindControl("hprPTitle")).Text; 
} 
+1

謝謝,它工作正常:) – Farzaneh

1

改變設計中場休息

<asp:FormView ID="PFull" runat="server" OnDataBound="PFull_DataBound" > 
<ItemTemplate> 
    <div class="post_con"> 
     <h4 class="post-headers"><asp:HyperLink ID="hprPTitle" runat="server" NavigateUrl='<%#Eval("Dosage","~/Paper.aspx?pid={0}")%>' Text='<%#Eval("PTitle")%>'></asp:HyperLink></h4> 
    </div> 
</ItemTemplate> 
</asp:FormView>