1

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I used to the following code because I was using a master page. Once again thanks ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1"); Response.Write(((Button)cph.FindControl("a")).Text);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.