2012-10-22 133 views
0

我的要求與stockoverflow標記顯示相同。我有一箇中繼器控制,其中我綁定幾列。現在我想顯示與相應線程關聯的不同標籤。請找到代碼...佔位符不顯示超鏈接

<asp:Repeater ID="repForumThread" runat="server"> 
      <HeaderTemplate> 
      </HeaderTemplate> 
      <ItemTemplate> 
       <table width="100%"> 
        <tr> 
         <td>        
          <asp:HyperLink ID="hlCategory" runat="server"  NavigateUrl='<%#GetRouteUrl("CategoryRoute", new {CategoryNo= Eval("CategoryId"),CategoryName = Eval("CategoryTitle").ToString().ToLower().Trim().Replace(" ","-")}) %>' 
          Text='<%# DataBinder.Eval(Container.DataItem, "CategoryTitle")%>'></asp:HyperLink> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:Label ID="lblThreadTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ThreadTitle") %>'></asp:Label> 
        </td> 
       </tr> 
       <tr> 
        <td>        
         <asp:HyperLink ID="hlThreadCreatedBy" runat="server" NavigateUrl='<%#GetRouteUrl("ProfileRoute", new {Member= Eval("MemberId"),MemberName = Eval("ThreadCreatedBy").ToString().ToLower().Trim().Replace(" ","-")}) %>' 
          Text='<%# DataBinder.Eval(Container.DataItem, "ThreadCreatedBy")%>'></asp:HyperLink> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:Label ID="lblCreatedOn" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ThreadCreatedOn") %>'></asp:Label> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:Label ID="lblDescription" runat="server" CssClass="HeaderTextSmall" Text='<%#DataBinder.Eval(Container.DataItem, "ThreadDescription")%>'></asp:Label> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:PlaceHolder ID="phTag" runat="server"></asp:PlaceHolder> 
        </td> 
       </tr> 
      </table> 
     </ItemTemplate> 
     <FooterTemplate> 
     </FooterTemplate> 
    </asp:Repeater> 


    public void BindRepForumThread() 
      { 
       SqlConnection dBConnection = null; 
       try 
       { 
        dBConnection = new SqlConnection(); 
        DataTable dtTag = new DataTable(); 
        dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 
        SqlDataAdapter dataAdapter = new SqlDataAdapter(); 

        SqlCommand cmd = new SqlCommand("SP_GetThreadDetails", dBConnection); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.Add("@threadId", SqlDbType.Int).Value = threadId; 

        dBConnection.Open(); 
        dataAdapter.SelectCommand = cmd; 
        DataSet ds = new DataSet(); 
        dataAdapter.Fill(ds); 

        if (ds.Tables[0].Rows.Count > 0) 
        { 
         DataTable dtThread = new DataTable(); 
         DataRow drThread; 
         dtThread.Columns.Add("ThreadId"); 
         dtThread.Columns.Add("CategoryId"); 
         dtThread.Columns.Add("CategoryTitle"); 
         dtThread.Columns.Add("ThreadTitle"); 
         dtThread.Columns.Add("ThreadDescription"); 
         dtThread.Columns.Add("ThreadCreatedBy"); 
         dtThread.Columns.Add("ThreadCreatedOn"); 
         dtThread.Columns.Add("MemberId"); 
         dtThread.Columns.Add("TagId"); 
         dtThread.Columns.Add("TagDescription"); 

         foreach (DataRow dr in ds.Tables[0].Rows) 
         { 
          drThread = dtThread.NewRow(); 

          drThread["ThreadId"] = dr["T_ThreadId"]; 
          drThread["CategoryId"] = dr["C_CategoryId"]; 
          drThread["CategoryTitle"] = dr["C_CategoryTitle"]; 
          drThread["ThreadTitle"] = dr["T_ThreadTitle"]; 
          drThread["ThreadDescription"] = dr["T_ThreadDescription"]; 
          drThread["ThreadCreatedBy"] = dr["Mem_MemberUserName"]; 
          drThread["ThreadCreatedOn"] = dr["T_ThreadCreatedOn"]; 
          drThread["MemberId"] = dr["T_ThreadCreatedBy"]; 

          if (ds.Tables[1].Rows.Count > 0) 
          { 
           phTag.Controls.Clear(); 

           foreach (DataRow dr1 in ds.Tables[1].Rows) 
           { 
            HyperLink hlTag = new HyperLink(); 
            hlTag.Text = dr1["Tg_TagName"].ToString(); 
            int TagId = Convert.ToInt32(dr1["Tg_TagId"].ToString()); 
            hlTag.ID = "hlTag" + TagId.ToString(); 

            hlTag.NavigateUrl = hlTag.GetRouteUrl("TagRoute", new { Id = TagId, TagName = hlTag.Text.ToString().ToLower().Trim().Replace(" ", "-") }); 
            phTag.Controls.Add(hlTag); 
           } 
          } 

          dtThread.Rows.Add(drThread); 
         } 

         repForumThread.DataSource = dtThread; 
         repForumThread.DataBind(); 

         dBConnection.Close(); 
        } 
       } 
       catch (Exception Ex) 
       { 
        throw Ex; 
       } 
       finally 
       { 
        // Close data reader object and database connection 
        if (dBConnection.State == ConnectionState.Open) 
         dBConnection.Close(); 
       } 
      } 

evreything工作正常。我能夠動態地生成超鏈接,但我無法顯示創建的超鏈接&它將錯誤「對象引用未設置爲對象的實例」作爲佔位符。

在此先感謝。

回答

1

那麼我已經解決了這個問題。我錯過了中繼器cotrol的OnItemCreate功能,我們可以找到一個佔位符的控件&,然後我們可以動態地添加超鏈接。

protected void repForumThread_ItemCrteated(object sender, RepeaterItemEventArgs e) 
     { 
      DataRowView drv = e.Item.DataItem as DataRowView; 
      DataTable dtTag = new DataTable(); 
      if (Cache["TagData"] != null) 
      { 
       dtTag = (DataTable)Cache["TagData"]; 
      } 
      if (dtTag.Rows.Count > 0) 
      { 
       if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) 
       { 

        PlaceHolder plTag = e.Item.FindControl("phTag") as PlaceHolder; 
        if (plTag != null) 
        { 
         plTag.Controls.Clear(); 
         foreach (DataRow dr1 in dtTag.Rows) 
         { 

          HyperLink hlTag = new HyperLink(); 
          hlTag.Text = dr1["Tg_TagName"].ToString(); 
          int TagId = Convert.ToInt32(dr1["Tg_TagId"].ToString()); 
          hlTag.ID = "hlTag" + TagId.ToString(); 

          hlTag.NavigateUrl = hlTag.GetRouteUrl("TagRoute", new { Id = TagId, TagName = hlTag.Text.ToString().ToLower().Trim().Replace(" ", "-") }); 
          plTag.Controls.Add(hlTag); 
         } 
        } 
       } 
      } 
     }