Server controls in an asp.net repeater

前端 未结 3 755
时光取名叫无心
时光取名叫无心 2021-01-15 09:54

Seems like I\'ve ran into a wall here. I want some datasource to be bound to an asp.net repeater (well, doesn\'t have to be a repeater, but it seems like that\'s what I want

3条回答
  •  别那么骄傲
    2021-01-15 10:47

    Seems like my problem was in the way I was thinking :)

    My solution: I just added controls as I normally would do, but inside the ItemTemplate. On callback events of the controls, I'd go for:

    (Button example)

    protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (b != null)
            {
                RepeaterItem ri = b.Parent as RepeaterItem;
                if (ri != null)
                {
                    string name = null;
    
                    //Fetch data
                    TextBox txtName = ri.FindControl("txtName") as TextBox;
    

    .. etc..

    So, after finding the RepeaterItem i just treat it as I would with any ControlGroup. Doesn't matter that I actually got 5 different textbosses, coded with ID="txtName", since asp.net automagically gives the controls "obfuscated" names in the client markup, and translates this back to my ID's on postback.

    Hope this helps someone, and sorry for bothering :)

提交回复
热议问题