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:
good and thnaks jow
thanks a lot man, I'll deep it and I'll publish
almost same implementation of the code here. please have a look. this may help to reduce code.
sorry forgot the path
http://aspnetnova.blogspot.com/2009/08/usercontrol-javascript-problems.html
Good job .Thanks!
你的選擇就是做或不做,不做就永遠不會有機會 ....................................................
一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光色情自慰自拍成人全裸打炮打手槍打飛機巨乳巨奶女優大奶性交性愛淫蕩淫慾淫亂淫婦淫妹淫叫淫水淫女
與其爭取不可能得到的東西,不如善自珍惜運用自己所擁有的........................................
how do u do?................................................................
how do u do?xvideo打飛機專用網洪爺免費洪爺色情片洪爺貼圖區洪爺成人線上洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼援交妹辣妹野球拳情色文學情趣聊天室性感辣妹裸體遊戲做愛偷拍一夜情視訊洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺免費洪爺色情片洪爺貼圖區洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼洪爺成人線上
一個人的快樂,不是因為他擁有的多,而是他計較的少。..................................................
你要保守你的心,勝過保守一切,因為一生的果效是由心發出........................................
良言一句三冬暖,惡語傷人六月寒。..................................................
好中意你的BLOG阿~加油!........................................
生命是一頓豐富的宴席,有人卻寧可挨餓........................................
Many a true word is spoken in jest...................................................
這麼好的部落格,以後看不到怎麼辦啊!!!..................................................
看後受益良多,謝謝∪ˍ∪..................................................
Knowledge is power.............................................................
文章不求沽名釣譽,率性就是真的..................................................
原來天鵝嫁給癩蛤蟆就會生出醜小鴨.................................................................
在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................
Poverty is stranger to industry.................................................................
成熟,就是有能力適應生活中的模糊。.................................................................
人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................
It takes all kinds to make a world.............................................................
寂寞又無聊 看到你的BLOG 加油喔!!..................................................................
時間可證明一切~你的BLOG是我覺得不賴的............................................................
最敏捷的,未必贏得競賽:最強大的,未必贏得戰爭:時間與機會才是主人................................................
Practice what you preach.............................................................
你不能改變容貌~~但你可以展現笑容.................................................................
幸福不是一切,人還有責任。............................................................
It is easier to get than to keep it.......................................................................
Cast not the first stone. ............................................................
友誼能增進快樂,減少痛苦......................................................................
耐心是一株很苦的植物,但果實卻很甜美。..................................................
成功可招引朋友,挫敗可考驗朋友......................................................................
我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。.................................................................
如果你批評他人。你就沒有時間付出愛......................................................................
一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼......................................... ........................
你不能改變容貌~~但你可以展現笑容............................................................
Post a Comment