开发者

href link to trigger asp.net c# button event

开发者 https://www.devze.com 2023-03-19 09:35 出处:网络
I have a navigation bar consisting of list items. Right at the end of the nav bar I want a logout link:

I have a navigation bar consisting of list items. Right at the end of the nav bar I want a logout link:

//other links

...

<li>
    <a href="#"> logout </a>
</li>

I have an event in my C# code called Logout_Click(object sender, EventArgs e) that I want to run when the user clicks the above navbar link, but obviously ordinary <a> tags don't trigger ASP.NET click events.

So I thought I'd be clever and create a HIDDEN <asp:Button> called logout that does trigger the Logout_Click function, and then get the JavaScript to click the button for me.. here is my code:

<a href="javascript:document.getElementById('logout').click();" class="top">Log Out</a>

<form runat="server">
    <asp:Button ID="logout" runat="server" onclick="Logout_Click" Visible开发者_JS百科="false" />
</form>

But it still didn't work.

Can someone help me?


Try:

<a href="javascript:document.getElementById('<%= logout.ClientID %>').click();" class="top">Log Out</a>


Can you try without the Visible="false" on the asp:button?

And if it works hide it with css instead, style="display:none;"


I think the script needs the client side id of your asp.net control:

javascript:document.getElementById('<%=logout.ClientID').click()


Or you can use the __doPostBack function ...

http://wiki.asp.net/page.aspx/1082/dopostback-function/enter link description here


Why not you are using Asp.net LinkButton ? It's easy to do this work with LinkButton.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号