Find Control in asp:repeater on button click event

前端 未结 1 703
一个人的身影
一个人的身影 2021-02-05 21:40

I have a dropdown list inside a asp:repeater item template. how can I get its value on button click event.



        
相关标签:
1条回答
  • 2021-02-05 21:58

    Assuming that BtnSave is also inside the repeater.

    You get the RepeaterItem by casting the button's NamingContainer. Then you can use FindControl to get the reference to your DropDownList:

    protected void BtnSaveClick(object sender, EventArgs e) {
        var btn =  (Button)sender;
        var item = (RepeaterItem)btn.NamingContainer;
        var ddl =  (DropDownList) item.FindControl("ddlWorkflowMembers");
        // ...
    }
    

    If the button is outside of the repeater and you want to save all items, you need to loop through all:

    protected void BtnSaveClick(object sender, EventArgs e) {
        foreach(RepeaterItem item in WorkflowListAfter.Items)
        {
            var ddl =  (DropDownList) item.FindControl("ddlWorkflowMembers");
            // ...
        }       
    }
    
    0 讨论(0)
提交回复
热议问题