Solved

How to programmatically click a link?

Posted on 2010-11-09
6
769 Views
Last Modified: 2012-05-10
Hi,
I have a link that when clicked wil prompt a popup window using jquery.

However, i would like to do the same in my code asp.net programatically. Basically, under certain logical algorithm, i want to trigger the click (just like as if i click it myself) to prompt the popup. The codes are below.

Please advise.

HTML
====
<a runat="server" href="#" id="ex3cTrigger">view</a> (notice)
...
<%--<div style="position: absolute; margin: -100px 0 0 100px;">--%>
<div style="position: absolute; margin: -100px 100 0 100px;">
<div id="ex3c" class="jqmNotice">
    <div class="jqmnTitle jqDrag">
      <h1>Did you know?</h1>
    </div>
 
  <div class="jqmnContent">
  <p>Pine, spruce, or other evergreen wood should never be used in barbecues. These woods, when burning or smoking, can add harmful tar and resins to the food. Only hardwoods should be used for smoking and grilling, such as oak, pecan, hickory, maple, cherry, alder, apple, or mesquite, depending on the type of meat being cooked.</p>
  </div>
 
  <img src="Image/clip_thumbnail.jpg" alt="close" class="jqmClose" />
  <img src="Image/clip_thumbnail_old.jpg" alt="resize" class="jqResize" />
</div>
</div>

JQUERY
=======
<script type="text/javascript">

    $().ready(function () {
    $('#ex3c')
    //.jqDrag('.jqDrag')
    //.jqResize('.jqResize')
    .jqm({
        trigger: '#ex3cTrigger',
        overlay: 0,
        onShow: function (h) {
            /* callback executed when a trigger click. Show notice */
            h.w.css('opacity', 0.92).slideDown();
        },
        onHide: function (h) {
            /* callback executed on window hide. Hide notice, overlay. */
            h.w.slideUp("slow", function () { if (h.o) h.o.remove(); });
        }
    });
    });

</script>
0
Comment
Question by:tangteng78
6 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 34091021
add JS function:function ClickTriggerLink() {  document.getElementById('ex3cTrigger').click();}in your code behind C#:Page.ClientScript.RegisterStartupScript(  this.getType(),   "ClickTriggerLink",   "ClickTriggerLink();",  true);
0
 

Author Comment

by:tangteng78
ID: 34091055
Not working. This is what i had on my .aspx.vb and javascript. Supposely on the first load, i should be able to see the popup window
.aspx.vb
---------------

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Page.IsPostBack Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ClickTriggerLink", "ClickTriggerLink();", True)
End If

End Sub
javascript:
---------------
function ClickTriggerLink() {
document.getElementById('ex3cTrigger').click();
}

$().ready(function () {
$('#ex3c')
//.jqDrag('.jqDrag')
//.jqResize('.jqResize')
.jqm({
trigger: '#ex3cTrigger',
overlay: 0,
onShow: function (h) {
/* callback executed when a trigger click. Show notice */
h.w.css('opacity', 0.92).slideDown();
},
onHide: function (h) {
/* callback executed on window hide. Hide notice, overlay. */
h.w.slideUp("slow", function () { if (h.o) h.o.remove(); });
}
});
});
0
 
LVL 8

Expert Comment

by:guvera
ID: 34091732
Hi,

 Please check the below condition

if (!ClientScript.IsStartupScriptRegistered(thisType,
"ClickTriggerLink"))
{
ClientScript.RegisterStartupScript(thisType,"ClickTriggerLink", "ClickTriggerLink();", True);
}
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:devlab2012
ID: 34093782
In your original javascript code, add a line:
document.getElementById('ex3cTrigger').onclick();

Complete code will be:

<script type="text/javascript">

    $().ready(function () {
    $('#ex3c')
    //.jqDrag('.jqDrag')
    //.jqResize('.jqResize')
    .jqm({
        trigger: '#ex3cTrigger',
        overlay: 0,
        onShow: function (h) {
            /* callback executed when a trigger click. Show notice */
            h.w.css('opacity', 0.92).slideDown();
        },
        onHide: function (h) {
            /* callback executed on window hide. Hide notice, overlay. */
            h.w.slideUp("slow", function () { if (h.o) h.o.remove(); });
        }
    });
    document.getElementById('ex3cTrigger').onclick();
    });

</script>
0
 

Author Comment

by:tangteng78
ID: 34098866
Guvera, it doesn't work either.
devlab2010, adding the the line will cause the javascript to error out.
Any other ideas are greatly appreciated.
0
 
LVL 8

Assisted Solution

by:guvera
guvera earned 250 total points
ID: 34099290
Try this code in codebehind

                Dim strjscript As String = "<script language='javascript'>"
                strjscript += "ClickTriggerLink() "
                strjscript += "</script" + ">"
                ClientScript.RegisterStartupScript(Me.GetType, "ClickTriggerLink", strjscript)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A short film showing how OnPage and Connectwise integration works.
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 …

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now