Javascript ile hiddenfield değerini değiştirmek

December 23, 2008 at 1:46 PMoozturk

Javascript ile bir hiddenfield'in değerini değiştirmek istiyorsak; javascript ile öncelikle onu bulmamız daha sonra değerini değiştirmemiz gerekir. Bu yapmak esasen çok kolay gözükse de masterpage kullandığımız zamanlarda hiddenfield kontrol'ünün id'si değiştiği için onu bulmak güç olabilir.

 Aşağıdaki kod ile bu işi çözebiliriz.

   var hdn = document.getElementById(<% ="\'" + HiddenField1.ClientID + "\'" %>);

Şimdi de bir örnek ile hem ajax, hem masterpage'in kullanıldığı bir ortamda hiddenfield değerini değiştirip, kontrol edelim.

1. Yeni bir proje içine bir masterpage ve bu masterpage'i kullanan bir contentpage hazırlayalım.

2. Masterpage'imize; bir scriptmanager, bir updatepanel koyalım. Updatepanel içine de bir label (Label1),  bir html buton (Button1) son olarak da bir hiddenfield (HiddenField1) koyalım. Bir de head kısmına aşağıdaki kodu yazalım.

   function setz() {

        var hdn = document.getElementById(<% ="\'" + HiddenField1.ClientID + "\'" %>);

        if(hdn)

        {

           hdn.value = "deneme";

          __doPostBack(<% ="\'" + UpdatePanel1.ClientID + "\'" %>, '');

        }

   }

3. Masterpage'imizdeki Button1 kontrolümüzü tanımladığımız satıra aşağıdaki işaretli kısmı ekleyelim.

     <input id="Button1" type="button" value="button" onclick="return setz();" />

4. Masterpage'imizin kod sayfasında, form load içine de aşağıdaki kodu yazalım. 

     Label1.Text = HiddenField1.Value;

5. Contentpage'imiz boş kalsın.

Uygulamayı çalıştıralım. Düğmeye bastığımızda, label'in yazısının deneme diye değiştiğini göreceğiz. Bunu dinamik herhangi bir olay neticesinde yapabiliriz.

Bildiğiniz gibi bir html butonunun herhangi bir .net kodu ya da kendinden bir postback özelliği yoktur. Haliyle tamamen client tarafında değeri değiştirip daha sonra UpdatePanel ile postback yaptık ki o da zaten server tarafında sadece gerekli kısmın değişmesini sağlayıp, bütün sayfanın yenilenmesini engelliyor. Aslında ajax kullandığımız için javascript içinde kullandığımız _doPostBack komutu yerine aşağıdaki kodu da kullanabilirdik.

    <%= ClientScript.GetPostBackEventReference(HiddenField1, "") %>;

Ayrıca kodda bazı optimizasyonlar da yapılabilir. Misal olarak her seferinde hdn değerini eşitlemek yerine javascript genelinde bir değişken açıp oradan da tanımlayabilirdik mesela.

İyi çalışmalar dilerim.

Posted in: ASP.NET | Ajax | Javascript

Tags: , , , , ,

Comments (100) -

Great post thx a lot !

Reply

You should wite a blog post about a recommendation of sites like Javascript ile hiddenfield degerini degistirmek. I came across your blog two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them.  But blogengine is cool.

Reply

I think you have considered putting a few video clips to your site to keep the readers more entertained? I mean I just read through the entire blog here and it was fine but since most people are more of a visual learner, I found that to become more helpful. Well please let me know how it turns out! I like what you guys are doing and up too. Such smart work and opinions! Keep up the great work guys. I've included this site to my blogroll. This is a cool blog thanks for sharing this informative information.. I will visit your site regularly for some latest post.

Reply

Whats Up
don't really agree with your page but it is well written.  I like your colors that you use in your site.

Reply

Do you like running Blogengine?  If I might --perhaps you should consider adding a few jquery animations.  I dont mean to disrespect what you've done; its very good stuff, indeed.  However, I think would respond to it more positively if you could be something animated to your site.  Keep it up, but put a little more into it next time.

Reply

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

Reply

great blog! keep up the great work!

Reply

Considerably, the article is in reality the best on that precious topic. I harmonise with your conclusions and will eagerly look forward to your next updates. Simply saying thanks will certainly not just be enough, for the phenomenal lucidity in your writing. I can directly grab your rss feed to stay informed of any updates. De lightful work and also much success in your business dealings!

Reply

Do you mind if I quote you on my blog if I link back to your website?

Reply

Well, the post is actually the sweetest on this deserving topic. I concur with your conclusions and will thirstily look forward to your next updates. Just saying thanks will not just be adequate, for the exceptional lucidity in your writing. I will immediately grab your rss feed to stay informed of any updates. Solid work and much success in your business efforts!

Reply

mmmm, I think you may be right in what you have said in this blog post. Whoever there are many people will disagree with your opinion.

Reply

Valuable thoughts and advices. I read your topic with great interest.

Reply

I kinda found your blog by accident, but your blog caught my attention and I thought that I would comment to let you know that I like it.

Reply

Just thought I would drop by to see what is happening. I have read this blog before and thought I would come back to see what's going on. Looks great.

Reply

Thanks for posting this. i really enjoyed reading this.

Reply

As an amateur writer from London, England, I infrequently come across really well written publications like your blog which really helps me inspirationally. It shows me what superb postings are all about. There are tons of substandard writings on the web and it's quite sad. Thank you for your well written posts! I frequently read your blog and enjoy the content you share. I look forward to more of your work.

Reply

Have you considered adding some differing opinions to your article? I think it will really enhance viewers' understanding.

Reply

I love this blog layout . How was it made!? It is very cool.

Reply

Have you considered adding some relevant links to the article? I think it will really enhance viewers' understanding.

Reply

This blog site has got some very helpful stuff on it! Thank you for informing me!

Reply

I like to take a break during the my day and browse through some blogs to see what others are talking about. This blog appeared in my searches and I could not help clicking on it. I'm glad that I did because it was a very enjoyable read.  

Reply

Considerably, the post is really the best on this precious topic. I concur with your conclusions and will eagerly look forward to your upcoming updates. Just saying thanks will not just be adequate, for the fantasti c lucidity in your writing. I will immediately grab your rss feed to stay abreast of any updates. Gratifying work and much success in your business dealings!

Reply

This website has got lots of extremely useful stuff on it. Cheers for helping me!

Reply

Hey =) that was some good reading =)

Reply

I am really not too familiar with this topic but I do like to visit blogs for layout ideas and interesting topics. You really described a subject that I usually don't care much about and made it very exciting. This is a unique blog that I will take note of. I already bookmarked it for future reference. Thank you

Reply

I'm getting a javascript error, is anyone else?

Reply

This blog site is pretty cool. How can I make one like this .

Reply

Wow. This website is awesome. How can I  make it look like this !?

Reply

Have you thought about adding some videos to the article? I think it might enhance everyone's understanding.

Reply

Have you considered adding some differing opinions to your article? I think it will really enhance everyone's understanding.

Reply

This really solved my problem, thank you!

Reply

This really solved my problem, thank you!

Reply

This blog is pretty cool! How did you make it ?

Reply

Its a pity you dont have a donate button, i would donate some =)

Reply

Just discovered this blog thru Yahoo, what a way to brighten up my month!

Reply

How did you make a blog site look this cool! Email me if you want and share your wisdom. I'd be thankful!

Reply

Amazing article, thank you, I will subscribe to you RSS now.

Reply

Could you message me with some tips about how you made this blog look this cool, I'd appreciate it.

Reply

How did you make a site look this good. Email me if you get the chance and share your wisdom. I'd be appreciative!

Reply

Mate. This blog site is sick. How did you  make it look like this !

Reply

I'm getting a browser error, is anyone else?

Reply

This website is very cool. How can I make one like this ?

Reply

Could you message me with any hints & tips on how you made your website look this good, I'd be appreciative!

Reply

pukyhsxodmsbmzm, <a href="http://kathyvanzeeland.org">kathy van zeeland</a>, yDCAhIyubIMCuiQwLEYP.

Reply

lhpugntyukzzodw, <a href="http://spaceheaterbuy.com">space heater</a>, lyndBNxuESGMdImtrQbZ.

Reply

devhkicrlbpfhjp, <a href="http://honorar.si">zaposlitev</a>;, cfOkJEHltAmgbjOdnYFu.

Reply

fmrwzmhqwcqxebr, <a href="http://stearnsandfoster.org">stearns and foster</a>, bUhrMPwHyXHtHomogeKM.

Reply

jlbhxhxzkmwxxsx, <a href="http://garniernutrisse.org">garnier nutrisse</a>, oUSRdaJCulcUsupwjFao.

Reply

felymrodlroopxk, <a href="http://honorar.si">zaposlitev</a>;, dtntQQZWaiBoVuexBZSF.

Reply

Ha great weblog how did you get all those people to RT. I heard its not very expensive, OH, fav toy in the world for a 30 yo is got to be a <A href="http://nerfgunsforsale.org/">Buzzbee Shotgun</A> </SPAN></SPAN>I bought it for my brother and theyve never seen it, I shoot up my brother and its awesome ;)

Thought you may like to know I clicked this page from the first page of aAsk seach. Nice work. I know how tidious it is to get your your website on the first page of a search. Ive read ebook after ebook and now got <A title="listen to music online for free without downloading" href="http://donate-mycar-tocharity.org/">car donation tax deduction</A></SPAN></SPAN></SPAN> on the first page. thank gosh only took 4months!!

Reply

How do you make your blog look this sick. Email me if you get the chance and share your wisdom. I'd be appreciative.

Reply

Awesome post . Thank you for, writing on my blog dude! I'll message you again! I did not realise that!

Reply

Could you email me with any hints & tips on how you made this blog site look this good , I would appreciate it.

Reply

This really answered my problem, thank you!

Reply

How do you make your blog look this awesome!? Email me if you get the chance and share your wisdom. I'd appreciate it.

Reply

Oh man. This blog is awesome. How do you  make it look this good .

Reply

Its a pity you dont have a donate button, i would donate some =)

Reply

Which dating network has the most users?

Reply

This website has got a lot of extremely useful stuff on it! Cheers for sharing it with me!

Reply

I'm getting a javascript error, is anyone else?

Reply

I like the site layout . How was it made!? It's really nice.

Reply

eijdbibegtxecclpov, http://thesak.org the sak, zgmXVnvnNmlHvYgnwMtn. mqndosjsasqkciaalm, http://expressjeans.org, iZXEOQuGBLrudEVLNzYO.

Reply

Could you message me with any hints about how you made this blog look this good , I would be thankful!

Reply

Hudrends of people will agree with your opinion whoever many other's will not agree,but I appreciate your hard work.

Reply

Hey, Are you looking for any guest writers right now as I am willing to provide you with high quality articles written specfically for your website.

Reply

Do you mind if I quote you on my blog if I link back to your website?

Reply

Hi It's a wonderful post. You have gained a new subscriber. The clearness in your post is simply spectacular and i can assume you are an expert on this subject.

Reply

smxpzyyqkuodlkdfda, http://www.physique-iq.com , XwtLXGZHGSgENjAneceg.

Reply

Thanx for the effort, keep up the good work Great work. I would like to thank you for sharing your thoughts and time into the stuff you post!! Hope you will make some more posts.

Reply

When are you going to post again? You really entertain me!

Reply

yoygvlhhbmwwmqvvhf, http://stearnsandfoster.org , jYLXFUKShckIDoZFWPfu. fxushwfnaesegqlabc, http://thesak.org , MsdzrDUiMUutTMaCqEEN.

Reply

Its a pity you dont have a donate button, i would donate some =)

Reply

I'm getting a browser error, is anyone else?

Reply

That ConLib economics policy in full; "We're filthy rich, so sod the rest of you!"

Reply

I came across your website while seeking for some thing distinct on The internet about topics related to movies, but I had the possibility to go through this blog post and I found it extremely interesting certainly.

Reply

They were talking about this on our public television station last night.  I happened to be recording on my DVR - I'll try to put it up on good ole youtube.com and post a link!

Reply

I adore that blog site layout ! How do you make it. It is rather sweet!

Reply

If you could email me with a few tips about how you made your blog look like this , I'd be thankful!

Reply

This blog site is extremely good! How was it made !

Reply

Oh man! This site is sick. How did you  make it look this good !?

Reply

How do you make your blog look this sick! Email me if you can and share your wisdom. I'd be appreciative!

Reply

This site has some very helpful information on it! Cheers for informing me!

Reply

Awesome post . Thank you for, writing on my blog man. I'll email you some time. I didn't know that!

Reply

We trained very hard, but it seemed that every time we were beginning to form into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising, and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation.   -   Gaius Petronius

Reply

A taste for irony has kept more hearts from breaking than a sense of humor, for it takes irony to appreciate the joke which is on oneself.

Reply

Hello, Thank you for the information site in aol. The article is very professionally written. I enjoy reading good url!.

Reply

I'm getting a javascript error, is anyone else?

Reply

Hey, Found your blog on yahoo, and I just wanted to say that I appreciated the information.

Reply

Live a life as a monument to your soul.   -   Ayn Rand

Reply

Nice!! Great Ifo. Great People. Great Blog. Thank you for all the great sharing that is being done here.

Reply

Major thanks for the article.Thanks Again. Cool.

Reply

Very worthwhile blog post.  Your own homepage is rather quickly starting to be among my favorite features.

Reply

The Simpsons relatives consists of Homer, Marge, Bart, Lisa, and Maggie. They portrait the common American loved ones.

Reply

Very nice thank you for the article. A fabolous page of information.

Reply

Wonderful thank you very much for this info. A Brilliant example of information.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading