原创

Repeater嵌套控件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/5414355

在我们编写程序的时候经常会遇到控件嵌套的问题。
通常最经常用到的控件时repeater,以下是一两个列子。
HTML
<asp:repeater id="repeaterMain" Runat="server" OnItemDataBound="repeaterMain_ItemDataBound">
        <ItemTemplate>
        <table>
        <tr><td>
        <a href= " <%#"Details.aspx?au_id=" + DataBinder.Eval(Container,"DataItem.au_id")%> "> <%#DataBinder.Eval(Container,"DataItem.au_id")%> </a> 大类<br>
        </td>
        </tr>
            <asp:repeater ID="repeaterSub" Runat="server">
            <ItemTemplate>
              <tr>
              <td>
              <a href= " <%#"Details.aspx?title_id=" + DataBinder.Eval(Container,"DataItem.title_id")%> "> <%#DataBinder.Eval(Container, "DataItem.title_id")%> </a> 小类
              </td>
              </tr>
            </ItemTemplate>
            </asp:repeater>
        </table>
        </ItemTemplate>
       
        </asp:repeater>

C#code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindMain();
        }
    }

    private void BindMain()
    {
        SqlConnection cn = new SqlConnection(@"server=(local);uid=sa;pwd=1234567890;database=pubs");
        SqlDataAdapter da = new SqlDataAdapter("select   au_id   from   authors", cn);
        DataSet ds = new DataSet();
        cn.Open();
        da.Fill(ds);
        repeaterMain.DataSource = ds.Tables[0].DefaultView;
        repeaterMain.DataBind();
        cn.Close();
    }

    protected void repeaterMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string au_id = ((DataRowView)e.Item.DataItem).Row["au_id"].ToString();
            Repeater repeaterSub = (Repeater)e.Item.FindControl("repeaterSub");
            if (repeaterSub != null)
            {
                SqlConnection cn = new SqlConnection(@"server=(local);uid=sa;pwd=1234567890;database=pubs");
                SqlCommand cmd = new SqlCommand("select   title_id   from   titleauthor   where   au_id   =   @au_id", cn);
                cmd.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = au_id;
                cn.Open();
                repeaterSub.DataSource = cmd.ExecuteReader();
                repeaterSub.DataBind();
                cn.Close();
            }
        }
    }
C# code
checklistbox
如果是bind checklistbox
protected void group_manage_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            string au_id = ((DataRowView)e.Item.DataItem).Row["id"].ToString();
            CheckBox chk = (CheckBox)e.Item.FindControl("chk_lock");
            chk.Text = au_id.ToString();  

            DABaseAccess DB = new DABaseAccess();
            string Str = "select r.rolename from users u left join meun_userrole ur on u.id=ur.userid "
                          + " left join meun_role r on ur.roleid=r.id "
                          + "where u.id=" + au_id;

            Repeater repeaterSub = (Repeater)e.Item.FindControl("repeaterSub");
            if (repeaterSub != null)
            {
                repeaterSub.DataSource = DB.Query(Str);
                repeaterSub.DataBind(); 
            }


        }
    }
取值关于控件取值问题

for (int i = 0; i < this.group_manage.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)group_manage.Items[i].FindControl("chk_lock");
                if (chk != null)
                {
                    chk.Checked = false;

                }

           

 

 

文章最后发布于: 2010-03-25 09:07:00
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览