我的要求与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工作正常。我能够动态生成超链接,但我无法显示创建的超链接&amp;它为占位符抛出错误“对象引用未设置为对象的实例”。
提前致谢。
答案 0 :(得分:1)
我已经解决了这个问题。我缺少OnItemCreated转发器控制功能,我们可以找到一个占位符控制器和然后我们可以动态添加超链接。
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);
}
}
}
}
}