Solved

Updatepanel Trigger on <A HREF?

Posted on 2008-06-23
10
2,077 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

774 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