简体   繁体   中英

How can I access buttons using string Ids in ASP.Net?

I am using Asp.Net. I have a page full of buttons and every button has an id example: btn_1_1, btn_1_2 etc. From the code side I have a loop and I am generating the button's Ids as a string using random numbers, example: "btn_1_1", "btn_1_2" etc. . My question is, how I can use this string value to access the button's properties that have the same ID?

Thanks

You can use FindControl method that accepts control's ID as a string.

string id = "btn_1_1";
Button btn1 = FindControl(id) as Button;
if (btn1 != null)
{
// Manipulating button's properties
}

Just make sure you are calling FindControl on the nearest parent of a button, since this method does not perform a recursive search over the controls tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM