æ¬æ¥ã®æçš¿ã¯ãASP.NET 4ã®Webãã©ãŒã ã§è¡ã£ãéèŠãªå€æŽã«çŠç¹ãåœãŠãã·ãªãŒãºã®æåã®æçš¿ã§ãããã¯ãªãŒã³ã§CSSãã¬ã³ããªãŒãªããŒã¯ã¢ããæšæºãçæããŸãã ä»æ¥ã¯ãã¯ã©ã€ã¢ã³ãã®ãµãŒããŒã³ã³ãããŒã«ã«ãã£ãŠçæããããIDãå±æ§ãããé©åã«å¶åŸ¡ããããã«è¡ã£ãäœæ¥ã«ã€ããŠèª¬æããŸãã
ã¯ãªãŒã³ã§æšæºã«æºæ ããCSSãã¬ã³ããªãŒããŒã¯ã¢ãããçæãã
éçºè ã®æãäžè¬çãªäžæºã®1ã€ã¯ãASP.NET Webãã©ãŒã ã§ãµãŒããŒã³ã³ãããŒã«ã®éåžžã®ã¬ã€ã¢ãŠããçæã§ããªãããšã§ããã ASP.NETã®ä»¥åã®ããŒãžã§ã³ã®ç¹å®ã®åé¡ã®ãªã¹ãã¯æ¬¡ã®ãšããã§ãã
- HTMLã§IDå±æ§ãèªåçæãããšãJavaScriptãšCSSã§ã®äœæ¥ãå°é£ã«ãªããŸãã
- äžéšã®ã³ã³ãããŒã«ïŒaspïŒã¡ãã¥ãŒãªã©ïŒã«ã»ãã³ãã£ãã¯ããŒã¯ã¢ããã®ä»£ããã«ããŒãã«ã䜿çšããŸãã
- ã¹ã¿ã€ã«ããããã£ãæ瀺çã«æå®ãããŠããªãå Žåã§ããäžéšã®ã³ã³ãããŒã«ã¯ã€ã³ã©ã€ã³ã¹ã¿ã€ã«ãçæããŸããã
- ViewStateã¯ååãªå€§ããã§ããããšãå€ããçæ³ããã¯ã»ã©é ããã®ã§ãã
ASP.NET 4ã¯ãæšæºã«æºæ ããããŒãžãããã«äœæããããã®æ¹åããããµããŒããæäŸããŸãã çŸåšãASP.NET 4ã®çµã¿èŸŒã¿<aspïŒ>ãµãŒããŒã³ã³ãããŒã«ã¯ãããã¯ãªãŒã³ãªããŒã¯ã¢ãããçæããCSSã¹ã¿ã€ãªã³ã°ããµããŒãããŠããŸããããã«ãããäžèšã®åé¡ããã¹ãŠãªããªããŸãã
æ¢åã®ASP.NET Webãã©ãŒã ã¢ããªã±ãŒã·ã§ã³ãæŽæ°ããéã®ããŒã¯ã¢ããã®äºææ§
ASP.NET 4ã«ç»å Žããããã¯ãªãŒã³ãªããŒã¯ã¢ããã«ã€ããŠèãããšãã«ããèããã質åã¯ããçŽ æŽãããã§ãããçŸåšãæ¢åã®ã¢ããªã±ãŒã·ã§ã³ã¯ã©ããªããŸããïŒãã§ãã ãããã®é©æ°ã¯ããããžã§ã¯ããæŽæ°ãããšãã«ãã¹ãŠãå£ããŸããïŒã
æ¢åã®ASP.NET Webãã©ãŒã ã¢ããªã±ãŒã·ã§ã³ã§ã¬ã€ã¢ãŠããšã¹ã¿ã€ã«ãäžå®ã§ããããšã確èªããããã web.configã«æ§æãã©ã°-controlRenderingCompatbilityVersionãè¿œå ããŸãããããã«ããã䜿çšããããŒã¯ã¢ãããžã§ãã¬ãŒã¿ãŒã®ããŒãžã§ã³ã決å®ã§ããŸãã
controlRenderingCompatbilityVersionãã©ã°ã ã3.5ãã«èšå®ãããŠããå Žåãã¢ããªã±ãŒã·ã§ã³ããã³ãµãŒããŒåŽã®ã³ã³ãããŒã«ã¯ãããã©ã«ãã§ããã©ã«ãã®.NET 3.5ããŒã¯ã¢ãããæç»ããŸãã controlRenderingCompatbilityVersionãã©ã°ã ã4.0ãã«èšå®ãããŠããå Žåãã¢ããªã±ãŒã·ã§ã³ããã³ãµãŒããŒã³ã³ãããŒã«ã¯ãXHTML 1.1æšæºã«æºæ ããã»ãã³ãã£ãã¯ããŒã¯ã¢ãããçæããŸããã¯ã©ã€ã¢ã³ãIDã¯ããã¯ãªãŒã³ã§ãäœåãªã©ã€ã³ã¹ã¿ã€ã«ã¯ãããŸããã
æ¢å®ã§ã¯ãASP.NET 4ã䜿çšããŠæ§ç¯ããããã¹ãŠã®æ°ããASP.NET Webãã©ãŒã ã¢ããªã±ãŒã·ã§ã³ã®ãã®ãã©ã°ã¯4.0ã§ããVS2010ã䜿çšããŠæŽæ°ãããä»ã®ãã¹ãŠã®ä»¥åã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ã controlRenderingCompatbilityVersionãã©ã°ãèªåçã«3.5ã«èšå®ããŸãã åŸã§ãã¢ããªã±ãŒã·ã§ã³å šäœã®ã¬ãã«ãšãããŒãžããšãŸãã¯ãã£ã¬ã¯ããªå šäœã®web.configãã¡ã€ã«ã§ãå€ãç°¡åã«å€æŽã§ããŸãã
ã¯ã©ã€ã¢ã³ãID
çæãããHTMLèŠçŽ ã«æ確ã§äºæž¬å¯èœãªIDå±æ§ãæã€æ©èœã¯ãéçºè ãé·ãéWebãã©ãŒã ã«æ±ããŠãããã®ã§ãïŒãctl00_ContentPlaceholder1_ListView1_ctrl0_Label1ãã®ãããªIDå€ã¯ããŸãäžè¬çã§ã¯ãããŸããïŒã IDå€ãå¶åŸ¡ããããšã«ãããJavaScriptã®èšè¿°ãCSSã®äœ¿çšãããã³å€§ããªããŒãžã®ããŒã¯ã¢ããã®éã®åæžã容æã«ãªããŸãã
ClientIDModeã³ã³ãããŒã«ã®æ°ããããããã£
ASP.NET 4ã¯ãControlåºæ¬ã¯ã©ã¹ã®æ°ããClientIdModeããããã£ããµããŒãããŠããŸãã ClientIdModeããããã£ã¯ ãã³ã³ãããŒã«ãã¯ã©ã€ã¢ã³ãIDãçæããæ¹æ³ãæå®ããŸãã ClientIdModeããããã£ã¯ã次ã®4ã€ã®å€ããµããŒãããŠããŸãã
- AutoID-.NET 3.5ã®ååã«åŸã£ãŠçæããŸãïŒèªåçæãããIDã¯ãäºææ§ã®ããã«ctrl00ãã¬ãã£ãã¯ã¹ãè¿œå ããŸãïŒ
- äºæž¬å¯èœïŒããã©ã«ãïŒ -è¡IDã§ã ctl00 ããåãæšãŠããªã¹ããŸãã¯ã³ã³ãããŒã®å Žåã芪IDãåã«è¿œå ããŸãïŒid =â ParentControl_ChildControlâïŒ
- éç -ã³ã³ãããŒã«ã®åœåãéçºè ã«å®å šã«å¶åŸ¡ããŸããIDãäœã瀺ããŠããŠããçµæã«ã¯ãã¹ãŠã衚瀺ãããŸãã
- ç¶æ¿ -芪ã³ã³ãããŒã«ã®åœåã¢ãŒãã䜿çšããããã«ã³ã³ãããŒã«ã«æ瀺ããŸã
ClientIDModeããããã£ã¯ãã³ã³ãããŒã«ãŸãã¯ã³ã³ããã«çŽæ¥èšå®ã§ããå éšèŠçŽ ã¯ç¶æ¿ãããèšå®ã«ãªããŸãã
ãŸãã¯ããã£ã¬ã¯ãã£ã<ïŒ @ PageïŒ >ãŸãã¯<ïŒ @ ControlïŒ >ã䜿çšããŠããŒãžã¬ãã«å šäœã§èšå®ã§ããŸãããã®å ŽåãããŒãžãŸãã¯ãŠãŒã¶ãŒã³ã³ãããŒã«å ã®ã³ã³ãããŒã«ã¯èšå®ãç¶æ¿ãããªãã·ã§ã³ã§ãããããªãŒããŒããŒãã§ããŸãã
ãŸãã¯ãã¢ããªã±ãŒã·ã§ã³ã®web.configã«ã€ã³ã¹ããŒã«ããããšãã§ããŸãããã®å Žåãã¢ããªã±ãŒã·ã§ã³ã®ããŒãžã¯èšå®ãç¶æ¿ããåæ§ã«èšå®ããªãŒããŒããŒãããå¯èœæ§ããããŸãã
ãã®ãã¹ãŠã«ãããæè»ã«åœååäœãã«ã¹ã¿ãã€ãºããããå¿ èŠã«å¿ããŠãªãŒããŒããŒããããã§ããŸãã
äŸïŒéãªã¹ãã³ã³ãããŒã«ã«ClientIdModeããããã£ã䜿çšãã
ãIDããèšå®ããŠãããŒãžäžã®ã³ã³ãããŒã«ã«æ°ããClientIdModeããããã£ã䜿çšããæ¹æ³ãèŠãŠã¿ãŸãããã ãã¢ã³ã¹ãã¬ãŒã·ã§ã³ã®ããã«ããã¹ã¿ãŒããŒãžãSite.Masterãã«åºã¥ãããSingleControlExample.aspxããšããååã®åçŽãªããŒãžãäœæãã<aspïŒcontent>ã«ãããMessageãIDãæã€<aspïŒlabel>èŠçŽ ã1ã€å«ã¿ãŸãã ãMainContentããšããååïŒ
åé¢ã³ãŒããã¡ã€ã«ã«ã以äžã«ç€ºãããã«ç°¡åãªã³ãŒããè¿œå ããŠãã©ãã«ããã¹ããåçã«èšå®ããŸãã
ASP.NET 3.5ã䜿çšããŠã¢ããªã±ãŒã·ã§ã³ãå®è¡ããããããŒãžã§ã³3.5ããã®ã¬ã³ããªã³ã°ã䜿çšããããã«ASP.NET 4ã¢ããªã±ãŒã·ã§ã³ãæ§æããããClientIdMode = AutoIDãèšå®ãããšãã¯ã©ã€ã¢ã³ãã«éä¿¡ãããçæãããããŒã¯ã¢ããã¯æ¬¡ã®ããã«ãªããŸãã
IDã¯äžæã§ãããããã¯é©åã§ããããctl100ããã¬ãã£ãã¯ã¹ãåå ã§ããããããŸãåã°ããŸããã
ClientIdModelããäºæž¬å¯èœãã«èšå®ãããŠããå Žåã«ããŒã¯ã¢ãããçæããŸã
ASP.NET 4ã§ã¯ãClientIdMode =â Predictableâã䜿çšããŠããµãŒããŒåŽã®ã³ã³ãããŒã«ãããã©ã«ãã§çæãããŸãã ããã«ããããã¹ãŠã®IDå€ãäžæã§ãããããŒãžã§ç«¶åãçºçããªãããšã確èªã§ããŸãããIDã¯åé·æ§ãäœããäºæž¬å¯èœã§ãã ã€ãŸããASP.NET 4ã®<aspïŒlabel>ã³ã³ãããŒã«ã®ããã©ã«ãã¬ã€ã¢ãŠãã¯æ¬¡ã®ããã«ãªããŸãã
æ¥é èŸãct100ããæ¶ããŠããããšã«æ³šæããŠãã ããã ãMessageãã³ã³ãããŒã«ã¯ãMainContentãã³ã³ããã«ãããããããã©ã«ãã§ã¯ãããŒãžäžã®ä»ã®ã³ã³ãããŒã«ãšã®æœåšçãªç«¶åãé²ãããã«ããã®IDã®å é ã«ãMainContent_Messageããä»ããŸãã
ClientIdModelããéçãã«èšå®ãããŠããå Žåã«ããŒã¯ã¢ãããçæãã
IDå€ãéå±€çã«å ¥ãåã«ããããªãå ŽåããããŸãããèšå®ãããå€ããã®ãŸãŸäœ¿çšããŸãã
ããã«ãããã¯ã©ã€ã¢ã³ãã³ã³ãããŒã«IDã®å€ãå®å šã«å¶åŸ¡ããæ©äŒãäžããããŸãã
äŸïŒããŒã¿ã«é¢é£ä»ãããããªã¹ãã«ClientIdModeããããã£ã䜿çšãã
ããŒã¿ã«é¢é£ä»ãããããªã¹ããŸãã¯ã°ãªããã¯ãWebãã©ãŒã ã§èªåçæãããIDãæäœããéã«ãã¹ã¿ã€ã«ã®äœ¿çšãŸãã¯ã«ã¹ã¿ãã€ãºããããŸã§å°é£ã§ããã 次ã«ãASP.NET 4ã§ListViewã®IDãæ§æããã·ããªãªãèŠãŠã¿ãŸãããã
以äžã¯ãé¢é£ããã³ã¬ã¯ã·ã§ã³ïŒãã®å Žåã¯ç©ºæž¯ïŒã®ã³ã³ãã³ãã衚瀺ããListViewã³ãŒãã¹ããããã§ãã
次ã«ãã³ãŒãããã€ã³ããã¡ã€ã«ã«ã³ãŒããèšè¿°ããŠã空枯ã®ãªã¹ããListViewãšåçã«ãªã³ã¯ããŸãã
å®è¡æãããã©ã«ãã®ãªã¹ãã¯<ul>ã®ããã«ãªããŸãã ListViewã®<ul>ããã³<li>èŠçŽ ã¯ãµãŒããŒã³ã³ãããŒã«ã§ã¯ãªãããããã®ããŒã¯ã¢ããã«ã¯IDããªãããšã«æ³šæããŠãã ããã
åè¡ã«ã¯ã©ã€ã¢ã³ãIDãè¿œå ããŸã
JavaScriptãä»ããŠå<li>ã«ããã°ã©ã ã§ã¢ã¯ã»ã¹ããã«ã¯ãã¯ã©ã€ã¢ã³ãIDãè¿œå ããå¿ èŠãããããšãæ³åããŠã¿ãŸãããã IDããŒã¿ã¯ãäžæã§ãäºæž¬å¯èœã§ãèªèå¯èœã§ããå¿ èŠããããŸãã
runat = "server"å±æ§ãèšå®ããåidã« "airport"ãæå®ããããšã«ããããã³ãã¬ãŒãå ã®å<li>ããµãŒããŒã³ã³ãããŒã«ãšããŠããŒã¯ããå¿ èŠããããŸãã
ããã©ã«ãã§ã¯ãASP.NET 4ã¯ã¯ãªãŒã³IDãçæããŸãã以äžã«ç€ºãããã«ãctl001ã¯ãããŸããã
ããããã£ClientIDRowSuffixã䜿çšããŸã
äžèšã®ãã³ãã¬ãŒãã¯ãå<li>èŠçŽ ã«å¯ŸããŠäžæã®IDãçæããããã«ãªããŸããããJavaScriptãä»ããŠããã°ã©ã ã§ã¢ã¯ã»ã¹ããå Žåã¯ãIDã«ç©ºæž¯ã³ãŒããå«ããå¿ èŠããããŸãã è¯ããã¥ãŒã¹ã¯ãé¢é£ä»ããããASP.NET 4ã³ã³ãããŒã«ã®æ°ããClientIDRowSuffixããããã£ã䜿çšããŠãéåžžã«ç°¡åã«ãããå®è¡ã§ããããšã§ãã
ãããè¡ãã«ã¯ãListViewã§ClientIDRowSuffixããããã£ããCodeãã«èšå®ããŸãã IDãçæãããšãã«ãé¢é£ãããã³ãŒããããããã£ãAirportã¯ã©ã¹ã§äœ¿çšããããã«ListViewã«æ瀺ããŸãã
ãããŠä»ãæ¥å°ŸèŸã1ããŸãã¯ã2ãã®ä»£ããã«ãå€Airport.CodeïŒ_CLEã_CAKã_PDXïŒãååŸããŸãã
GridViewãªã©ãããŒã¿æ¥ç¶ãæã€ä»ã®èŠçŽ ã§ClientIDRowSuffixã䜿çšã§ããŸãã
ãŸãšã
ASP.NETã䜿çšãããšããµãŒããŒã³ã³ãããŒã«çšã®ããã¯ãªãŒã³ãªHTMLããŒã¯ã¢ãããçæã§ããŸãã
次ã®èšäºã§ã¯ãASP.NET 4ã®ããŒã¯ã¢ããã«åœ±é¿ãäžãããã®ä»ã®é©æ°ã«ã€ããŠèª¬æããŸãã