Monday, May 11, 2009

ASP.NET User Controls and jQuery

If you like programing pure in client side to render every thing your self for more control , you should integrate your client side scripts with server side technology.
Some times what your server side code outputs restrict your client side scripting , user controls is one of this examples .


The problem is finding objects in each user control . if you use server controls like asp:textbox , asp.net change object id to keep it unique in host page (maybe use multiple instances of a user control in one page or host page have another object has same id ) . The problem is finding objects in client side .
Common solution is Injecting htmlControl.clientID from server side code into rendered html for accessing in client side . because clientID attribute gives you unique id that asp.net generated in host page . but its not a good idea and nobody like use that because it restricts your scripting.

Another solution is using css class instead of id.
but here is a problem ... css classes is unique in user control area but not unique in host page with multiple user control instances . I have a trick for you ...

First you should make user control boundary identifiable .

I use a div with class named controlContainer for my user control boundary.
Know but all of your objects inside this container . ok lets talk about finding each object of user control inside of user control boundary and do jobs with a unique object in a host page with multiple user control instances . for finding an object uniquely I should find user control boundary and search for object with specified css class inside this boundary .

using .parents('.controlContainer') to find user control boundary from inside , and using .find('.Text1') for reference to object with css class value of '.Text' .

lets have an example :
In this Example we develop an ASP.NET user control with a div and a button , when user click the button , div toggle hide and show .
First Create user control html ...


Lets see the Code behind ...
javascripts cannot write inside a user control and you should use Page.ClientScript.RegisterStartupScript to register client scripts on user control html output . ok make a draft of necessary script for showHide method . this method calls button click .
function showHide(obj){
var x=$(obj).parents('.controlContainer');
$(x).find('.Text1').toggle('slow');
}

Concat this script lines using stringBuilder class in .net and register it as I explained in page load event handler.

VB :
CS :

For Run and test , add more than one instance of this user control to an ASP.NET page and run .
the result show you despite having several objects on page with css class Text1 , a button on each user control hide and show just div inside same user control . yes , and now you can easy write your client side script on user controls without worry about unique id's that generates by ASP.NET .

Happy scripting ...

41 comments:

Nerver Say No said...

good and thnaks jow

bpbrainiak said...

thanks a lot man, I'll deep it and I'll publish

A IT Devotee said...

almost same implementation of the code here. please have a look. this may help to reduce code.

A IT Devotee said...

sorry forgot the path

http://aspnetnova.blogspot.com/2009/08/usercontrol-javascript-problems.html

Alireza said...

Good job .Thanks!

遊樂園 said...

你的選擇就是做或不做,不做就永遠不會有機會 ....................................................

早餐 said...

一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光色情自慰自拍成人全裸打炮打手槍打飛機巨乳巨奶女優大奶性交性愛淫蕩淫慾淫亂淫婦淫妹淫叫淫水淫女

火吟 said...

與其爭取不可能得到的東西,不如善自珍惜運用自己所擁有的........................................

仁南 said...

how do u do?................................................................

水慧 said...

how do u do?xvideo打飛機專用網洪爺免費洪爺色情片洪爺貼圖區洪爺成人線上洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼援交妹辣妹野球拳情色文學情趣聊天室性感辣妹裸體遊戲做愛偷拍一夜情視訊洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺免費洪爺色情片洪爺貼圖區洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼洪爺成人線上

佑英 said...

一個人的快樂,不是因為他擁有的多,而是他計較的少。..................................................

重俐 said...

你要保守你的心,勝過保守一切,因為一生的果效是由心發出........................................

志義 said...

良言一句三冬暖,惡語傷人六月寒。..................................................

BokHaile8854 said...

好中意你的BLOG阿~加油!........................................

DennyR_Guari佳穎 said...

生命是一頓豐富的宴席,有人卻寧可挨餓........................................

ShilaS_群平 said...

Many a true word is spoken in jest...................................................

健豪 said...

這麼好的部落格,以後看不到怎麼辦啊!!!..................................................

LorenE_Mese鈺雯 said...

看後受益良多,謝謝∪ˍ∪..................................................

又陽 said...

Knowledge is power.............................................................

陳卓人 said...

文章不求沽名釣譽,率性就是真的..................................................

慧君 said...

原來天鵝嫁給癩蛤蟆就會生出醜小鴨.................................................................

貢慧 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................

萱祥 said...

Poverty is stranger to industry.................................................................

戴昀德 said...

成熟,就是有能力適應生活中的模糊。.................................................................

云依恩HFH謝鄭JTR安 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

李威昌 said...

It takes all kinds to make a world.............................................................

玉苓玉苓 said...

寂寞又無聊 看到你的BLOG 加油喔!!..................................................................

韋志韋志 said...

時間可證明一切~你的BLOG是我覺得不賴的............................................................

天花天花 said...

最敏捷的,未必贏得競賽:最強大的,未必贏得戰爭:時間與機會才是主人................................................

宜欣宜欣 said...

Practice what you preach.............................................................

琬安琬安 said...

你不能改變容貌~~但你可以展現笑容.................................................................

劉士賢 said...

幸福不是一切,人還有責任。............................................................

謝俊宏 said...

It is easier to get than to keep it.......................................................................

凱v胡倫 said...

Cast not the first stone. ............................................................

承王蓁 said...

友誼能增進快樂,減少痛苦......................................................................

灏群 said...

耐心是一株很苦的植物,但果實卻很甜美。..................................................

王辛江淑萍康 said...

成功可招引朋友,挫敗可考驗朋友......................................................................

tongtong said...

我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。................................................................

于庭吳 said...

如果你批評他人。你就沒有時間付出愛......................................................................

佳陳容 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼......................................... ........................

偉DimpleHolloway043昀 said...

你不能改變容貌~~但你可以展現笑容............................................................

Post a Comment