简体   繁体   中英

Disabling AutoPostback on gridview asp:hyperlinkfield?

I am trying to execute some javascript on the click of a asp gridview's hyperlinkfield. The javascript executes fine but the page posts back so when the iframe flickers before showing (The javascript simply sets the iframe's source and then its display from none to block.). How can I go about disabling autopostback on this control since it does not have such an attribute?

<Columns>
<asp:hyperlinkfield Text="Update"
                    NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');"
                    HeaderText="Update"
                    target="_blank"/>
<asp:hyperlinkfield Text="Add Findings"
                    NavigateUrl="javascript:ShowForm('frmAuditFindings.aspx');"
                    HeaderText="Add Findings"
                    target="_blank" />
</Columns>

CSS

.ShowMe{display:block;} 
.HideMe{display:none;}

Javascript

function ShowForm(urlToGoTo){
document.getElementById('myiFrame').src = urlToGoTo;
document.getElementById('myiFrame').className = "ShowMe";
}

Try using the TemplateFiled and the anchor tag like this :

<Columns>
    <asp:TemplateField>
        <ItemTemplate>

             <a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">
        </ItemTemplate>
    </asp:TemplateField>    

if you want you can set runat="server" and give it an ID

In your javascript event handler add return false so the browser does not continue the click action, in this case doing the postback.

Of course, ideally, you wouldn't be using <asp:HyperlinkField> . Have you considered using <a runat="server"> instead?

Try this NavigateUrl attribute in the markup

NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false;"

And also you can try basic HTML anchor tag and javascript to open new window.

 NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false"

See if you are not doing anything on postback of these hyper links then what is the purpose to draw server side control.

These affects the performance while rendering on web page. So I would suggest you to use <a> insread of these hyperlinkfields

you can use instead :

            <a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">

Postback will never occure in this case and reduce the rendering time and momory cost in comparative to byperlinkfield.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM