Posted By

derekholmes on 12/31/09


Tagged

cGET documentgetElementById


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

fredblogs
emregulcan
danfinrud55


c# master page document.getElementById problem


 / Published in: C#
 

Because of master pages C# changes your id's to make them unique. This allows you to place your javascript in a seperate .js file or just get rid of a document.getElementById('');type solution

  1. /*place in app_code to use throughout your site:*/
  2.  
  3. /// <summary>
  4. /// setup cGET for all Page Controls
  5. /// </summary>
  6. public static void setup_cGET(Page page)
  7. {
  8. setup_cGET(AllControls(page), page);
  9. }
  10. /// <summary>
  11. /// setup cGET for all items in your control
  12. /// </summary>
  13. public static void setup_cGET(Control c, Page page)
  14. {
  15. setup_cGET(AllControls(c), page);
  16. }
  17. /// <summary>
  18. /// setup cGET for passed Controls ex. setup_cGET(txtbox1, txtbox2...);
  19. /// </summary>
  20. public static void setup_cGET(Page page, params Control[] wc)
  21. {
  22. if (wc.Length > 0)
  23. {
  24. List<Control> r = new List<Control>();
  25. for (int i = 0; i < wc.Length; i++)
  26. {
  27. r.Add(wc[i]);
  28. }
  29. setup_cGET(r, page);
  30. }
  31. }
  32. /// <summary>
  33. /// Setup a javascript cGET function that looks up controls. This solves the problem of not knowing the id in c# master pages
  34. /// Can only run this function once, should be using a scriptmanager
  35. /// </summary>
  36. /// <param name="c"></param>
  37. public static void setup_cGET(List<Control> lc, Page page)
  38. {
  39. if (lc.Count <= 0)
  40. return;
  41.  
  42. // Now loop through the controls and build the client and server id's
  43. string js = "var cGET_serverIDs = new Object();";
  44. foreach (Control c in lc)
  45. {
  46. if (c.ID != null && c.ID.Length > 0)
  47. js += "cGET_serverIDs['" + c.ID + "'] = '" + c.ClientID + "';\n";
  48. }
  49.  
  50. // Now register the method GetClientId, used to get the client id of tthe control
  51. ScriptManager.RegisterStartupScript(page, page.GetType(), "cGET",
  52. js+
  53. "function cGET(serverId) " +
  54. " { " +
  55. " if (cGET_serverIDs[serverId] != null) " +
  56. " return document.getElementById(cGET_serverIDs[serverId]); " +
  57. " " +
  58. " return document.getElementById(serverId);" +
  59. " } "
  60. , true);
  61.  
  62. }
  63.  
  64. /// <summary>
  65. /// Loads all controls recursively.
  66. /// </summary>
  67. /// <param name="container">The container to search for the control passed. Remember
  68. /// all controls (Panel, GroupBox, Form, etc are all containsers for controls
  69. /// </param>
  70. /// <returns></returns>
  71. private static List<Control> AllControls(Control container)
  72. {
  73. List<Control> r = new List<Control>();
  74.  
  75. foreach (Control ctrl in container.Controls)
  76. {
  77. foreach (Control c in AllControls(ctrl))
  78. r.Add(c);
  79. }
  80.  
  81. r.Add(container);
  82.  
  83. return r;
  84. }
  85. /* on each page */
  86. protected void Page_Load(object sender, EventArgs e)
  87. {
  88. GlobalFunction.setup_cGET(Form, this);
  89. }
  90.  
  91. /*.js file */
  92. cGET('textBoxID').value = '5';

Report this snippet  

You need to login to post a comment.