日期:2014-05-17  浏览次数:20487 次

关于repeater中的DataSourceID和OnItemDataBound
前台代码:

HTML code


<table class="ProTab marginT" border="0" cellspacing="0" cellpadding="0" width="245px">
<tbody>
  <tr>
    <th>&nbsp;品名</th>
    <th>价格</th>
    <th>发布日期</th>
    </tr>
  <asp:Repeater ID="repPro" runat="server" OnItemDataBound="repPro_ItemDataBind"> 
  <ItemTemplate>
  <tr>
    <td style="height: 24px" ><a title="<%# Eval("Name") %>" href="#">&nbsp;<%# Eval("Name") %></a></td>
<!--需要控制权限是否显示-->
<div id="guoji" runat="server" visible="true">   
 <td style="height: 24px" >&nbsp;<%# Eval("price") %></td>
</div><!--需要控制权限是否显示-->
    <td style="height: 24px" >&nbsp;<%# Eval("Addtime") %></td>
    </ItemTemplate></asp:Repeater>
 </tbody></table>




后台:

C# code


        public void Rep3()
        {
           string sql = "select top 15 * from Products order by classid desc";
           SqlDataAdapter da = new SqlDataAdapter(sql, conn);
           DataSet ds = new DataSet();
           da.Fill(ds, "a");
           this.repPro.DataSource = ds.Tables["a"].DefaultView; 
           this.repPro.DataBind();
           //绑定输出数据
        }

   protected void repPro_ItemDataBind(object sender, RepeaterItemEventArgs e)
        {
            if (Session["UserName"] != null)
            { 
                if (Session["UserName"].ToString() == "")
            {
                e.Item.FindControl("guoji").Visible = false; 
                }
            } 
        }




我想在前台,去控制那个<%# Eval("price") %>是否有权限查看。 可是好像不灵。

------解决方案--------------------
protected void repPro_ItemDataBind(object sender, RepeaterItemEventArgs e)
 {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (Session["UserName"] != null)

if (Session["UserName"].ToString() == "")
{
Panel pan= (Panel )e.Item.FindControl("guoji");// 用panel 别用div
pan.Visible = false;

}



}