2

First off I'm actually a DBA and not a web developer so... what I write will probably look ugly. I am building a website (they asked me if I'd try /shrug) and I have 168 checkboxes that have the same action when checked. But these actions, though the same, are performed on different controls (related to the checkbox). Instead of having a switch statement with 168 conditions can I do something like the following?

CheckBox myCB = (CheckBox)(sender);

String mySTR = myCB.ID.ToString();
String myGVstr = “gv” + mySTR.Substring(mySTR.IndexOf(‘cb’) + 1);
String myBTNstr = “btn” + mySTR.Substring(mySTR.IndexOf(‘cb’) + 1);

GridView myGV = myGVstr;
Button myBTN = myBTNstr;

// Do what I need to do with these controls
...

1 Answer 1

1

This really doesn't seem like a plausible thing to do. Maybe on loading you can place all your controls into a large array? It would take 1 large section of code to place each control into the array but afterwards you can access them in a similar (more reliable) fashion.

If you insist on doing it this way the 'FindControl' method might be of some use.

http://msdn.microsoft.com/en-us/library/486wc64h.aspx

Something like...

GridView myGV = (GridView)FindControl(myGVstr)

Not tested, but might work.

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

5 Comments

Could also consider a dictionary with the Checkbox (or checkbox ID) as the key.
Plausible or possible? Since I am writing it, I have used a very strict naming "standard" so I'm not to worried if you should do it this way or not.
I haven't been able to get it to work. I even hard-coded the GridView I'm trying to grab and myGV is still null. Tried changing the FindControl to (GridView)this.Page.FindControl("gvA1"); and still no dice... was hoping this would work, having to code the same 29 lines 168 times where only the control name changes stinks...
I've even tried the example from the link you provided and the example doesn't work LOL guess today is Monday part II
I figured it out. Thanks for you help! :)

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.