Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Updatepanel Trigger on <A HREF?

Posted on 2008-06-23
10
2,091 Views
Last Modified: 2012-06-21
Is it possible to trigger a partial page update using an href with command arguments?

such as:

<a id="loadOrder" href="javascript:__doPostBack('LoadOrderDetail','<%# Container.Value %>')"><%#  Container.Value %> </a>
0
Comment
Question by:ChiBella
  • 6
  • 4
10 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21871264
Is __doPostBack() a Javascript function?  You actually shouldn't have any script in the href attribute.  It is better to put it in onclick, the event in the same tag.  That is where it belongs.

<a id="loadOrder" href="#" onclick="__doPostBack('LoadOrderDetail','<%# Container.Value %>')"><%#  Container.Value %> </a>

I believe __doPostBack() is actually for server script.  If that is the case then it would not be triggered even in an onclick event.  A clientside event can't trigger server code.

Let me know if you have a question or need more info.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21871271
If you do have a question then please show the browser source code (i.e. what you see by clicking View Source/Page).  Also show the javascript code for the function with that name.

bol
0
 

Author Comment

by:ChiBella
ID: 21871924
Yes, you are right and also found my issue. In order to do a partial postback, needed to add the gridUpdatePanel.ClientID to the eventTarget.

<a id="loadOrder" href="javascript: //" onclick="__doPostBack('<%# gridUpdatePanel.ClientID %>', '<%# Container.Value %>')"><%# Container.Value %></a>
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:ChiBella
ID: 21871939
I'm kinda confused by your statement:

I believe __doPostBack() is actually for server script.  If that is the case then it would not be triggered even in an onclick event.  A clientside event can't trigger server code.

Because it actually does trigger a postback and within the  page load you can capture the eventtarget/eventarguement. My issue was with my updatepanel which was not firing correctly..I was getting a full page refresh instead of the partial.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 21877147
So is this solved now or do you still have a question or need help?  I will respond to parts of your last 2 posts but it wasn't clear if the problem is solved or how I can help with it from your responses.

>> I'm kinda confused by your statement: <<

Sorry if that caused confusion and it may have been a little general too.  If you look at the browser source do you see runat="server" in the anchor tag?  If not then look for something for that function or a general instruction to run on the server.  With .NET the server can send html or script to the browser to let it know something needs to be done on the server.  This would allow an exception to what I said in the block you asked about.  There would still be signs of this in the browser source but .NET isn't my expertise so I can't tell you exactly what or how it works.

The html you ended up with or showed in the comment at http:#21871924 is not really valid.  That href value is still a problem.  If possible the server script should create a link like the one I showed above but could have "return false" at the end of the onclick event so the browser ignores the href value.  E.g.

<a id="loadOrder" href="#" onclick="__doPostBack('<%# gridUpdatePanel.ClientID %>', '<%# Container.Value %>'); return false;"><%# Container.Value %></a>

I don't know for sure how or if you can do that in the .NET code you have but it would be best for html and validation if your result is like the one above (when viewed in the browser source).

I hope this cleared up what I said.  If you still need help with this then please let me know what.

bol
0
 

Author Comment

by:ChiBella
ID: 21878393
Well I still don't know why this is a problem? It works like a charm now.

<a id="loadOrder" href="javascript: //" onclick="__doPostBack('<%# gridUpdatePanel.ClientID %>', '<%# Container.Value %>')"><%# Container.Value %></a>



My issue was that I needed to supply the updatepanel : <%# gridUpdatePanel.ClientID %> as the eventtarget. Once I did this, it all works well and very fast.

I still do not understand the issue with href="javascript: //" .... but I am new, so maybe it's me, sorry.

View Source:
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
<div style="width:100%;height:100%;text-align:left">            
<a id="loadOrder" href="javascript: //" onclick="__doPostBack('ctl00_ContentPlaceHolder1_gridUpdatePanel', '8108853')">8108853</a>


   </div>
0
 

Author Comment

by:ChiBella
ID: 21878418
In the code behind, when the postback occurs....we check for the eventTarget and eventArgument:

protected void Page_Load(object sender, EventArgs e)
    {



        this.ClientScript.GetPostBackEventReference(this, string.Empty);
        if (!Page.IsPostBack)
        {
            AttachGrid();
        }
             string eventTarget = (this.Request["__EVENTTARGET"] == null ? string.Empty : this.Request["__EVENTTARGET"]);
         string eventArgument = (this.Request["__EVENTARGUMENT"] == null ? string.Empty : this.Request["__EVENTARGUMENT"]);
         if (eventTarget == "ctl00_ContentPlaceHolder1_gridUpdatePanel")
        {
             LoadOrderDetail(eventArgument);
        }

    }
0
 

Author Comment

by:ChiBella
ID: 21878560
I tried your code...and it works as well, though I still do not know what the side effects would be using the javascript: version. Is this outdated usage?

<a id="loadOrder" href="#" onclick="__doPostBack('<%# gridUpdatePanel.ClientID %>', '<%# Container.Value %>'); return false;"><%# Container.Value %></a>
0
 

Author Comment

by:ChiBella
ID: 21878608
This is the only side-effect I can find:
http://blog.metawrap.com/blog/ToHrefjavascriptfOrHrefOnclickfThatIsTheQuestion.aspx

and I don't believe it relates to what I'm doing.

Gonna give you the points because I learned from you.

Thanks!
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21879190
>> Well I still don't know why this is a problem? <<
>> I still do not understand the issue with href="javascript: //" .... but I am new, so maybe it's me, sorry. <<
>> though I still do not know what the side effects would be using the javascript: version. Is this outdated usage?  <<

Putting javascript in the href attribute is outdated.  Probably even from before good and consistent events in browsers but I haven't actually given it much thought.  Current specs say the href should have a URL.  Most important is it would have something that can be used even if script were disabled or not supported.  This doesn't really matter here so I recommend and would use # but in some cases you might put a page.  One that would be used if the onclick event didn't work or wasn't used.

I will admit I am being strict with this and most browsers will not have a problem with what you used.  In fact they will probably still validate the page.  However that may not continue, especially if the doctype you use is a strict one.  If the browser doesn't "recognize" that html then it would have to use "quirks" mode to render the page.  That can cause unpredictable results for the rest of your page, including content, layout, etc.

My main point was to show the best way to do it.  Keeping script out of href, src, and similar attributes and leaving it in events is best.  When the tag is something like an anchor which would try to go to the href "page" adding return false at the end of the script will prevent that from happening IF THE SCRIPT IS USED AND WORKS.  It seems to be the perfect setup and made to handle various situations smoothly.  Of course actually getting a server script to create that content may be hard but it seems like you got that worked out.

As I mentioned, this isn't a big deal at this time.  There are times where I have to ignore some "rules" to get the results or because it doesn't matter.  I am glad this was informative though and I could help.  Thanks for the fun question, the grade and the points.  Let me know if you have any other questions about this.

bol

p.s.  You are right the article at metawrap doesn't really relate here although it is on this topic and also informative.  Good find!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 questions 10 29
HTML - Color not displaying correctly in EMAIL. 6 49
how to double quote a string for an inline sql statement. 8 73
IEnumerable<T> to a List<T> 8 37
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question