简体   繁体   English

如何找到动态创建的按钮 ASP.net 的容器

[英]How to find container of dynamically created button ASP.net

I have a WebForm whose controls are created dynamically as they can vary in number.我有一个 WebForm,它的控件是动态创建的,因为它们的数量可能会有所不同。 Each element is a Panel containing a TextBox, a DropDownList and a Button.每个元素都是一个 Panel,其中包含一个 TextBox、一个 DropDownList 和一个 Button。
Each control is given a unique ID and all the buttons have the same clickEvent.每个控件都有一个唯一的 ID,并且所有按钮都具有相同的 clickEvent。 Inside the code of the clickEvent, I want to obtain the ID of the panel in which the presedButton belongs so I can access the chosen value from that Panel's DropDownList and the text from the TextBox.在 clickEvent 的代码中,我想获取 presedButton 所属的面板的 ID,以便我可以从该面板的 DropDownList 和 TextBox 中访问所选值。
How can I do the above?我该怎么做以上?

private void button1_Click(object sender, EventArgs e)
    {
        var panelId = ((Button)sender).Parent.ID;

    }

Or you could get the controls directly或者您可以直接获取控件

private void button1_Click(object sender, EventArgs e)
    {

        var myTextbox = ((Button)sender).Parent.Controls.OfType<TextBox>().FirstOrDefault();
        var myDropDownlist = ((Button)sender.Parent.Controls.OfType<DropDownList>().FirstOrDefault();

    }

or by control ID:或通过控制 ID:

private void button1_Click(object sender, EventArgs e)
    {

        var myTextbox = ((Button)sender).Parent.Controls.OfType<TextBox>().Where(x => x.ID == "textboxID").SingleOrDefault();
        var myDropDownlist = ((Button)sender).Parent.Controls.OfType<DropDownList>().Where(x => x.ID == "dropdownlistID").SingleOrDefault();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ss.net中将ssclass添加到动态创建的按钮中? - How to add ssclass to a dynamically created button in asp.net? 如何在 ASP.NET 中动态创建的用户控件上查找控件? - How to find controls on a dynamically created user control in ASP.NET? 动态找到一个按钮的位置是asp.net? - Dynamically find the position of a button is asp.net? ASP.NET:如何避免动态创建的按钮事件刷新页面而不是做相关代码的情况 - ASP.NET: How to avoid situation that dynamically created button event refreshes the page instead of doing relevant code 在asp.net c#中,如何在单击“提交”按钮后获取动态创建的控件的值 - in asp.net c# how to get the value of dynamically created controls after clicking submit button 如何在动态创建的ASP.net控件中动态创建ASP.net控件 - How to dynamically create ASP.net controls within dynamically created ASP.net controls 如何在ASP.net中选中/取消选中动态创建的复选框 - How to check/uncheck dynamically created checkboxes in ASP.net 如何在ASP.NET中动态创建的链接按钮中传递查询 - How to pass query in a dynamically created linkbutton in asp.net 如何从ASP.NET中动态创建的文本框中获取文本 - How to get text from dynamically created textboxes in ASP.NET 如何获取动态创建的asp.net GridView的行索引? - How to get the row index of a dynamically created asp.net GridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM