Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2251
  • Last Modified:

Updatepanel Trigger on <A HREF?

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
ChiBella
Asked:
ChiBella
  • 6
  • 4
1 Solution
 
b0lsc0ttCommented:
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
 
b0lsc0ttCommented:
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
 
ChiBellaAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ChiBellaAuthor Commented:
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
 
b0lsc0ttCommented:
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
 
ChiBellaAuthor Commented:
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
 
ChiBellaAuthor Commented:
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
 
ChiBellaAuthor Commented:
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
 
ChiBellaAuthor Commented:
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
 
b0lsc0ttCommented:
>> 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now