Click Event of Hyperlink - c#

How to find Whether a hyperlink is clicked or not in ASP.net C# in runtime?
I want to write code on like that
Response.Redirect("Default.aspx");

If you want to execute server code upon a click in a link, then you should use the ASP.NET control <asp:LinkButton>
This is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.

You would attach either the event in the code behind, or in the ASPX / ASCX of your link in question like so:
<asp:LinkButton ID="linkGoSomewhere" runat="server" Click="linkGoSomewhere_Click" />
OR
linkGoSomewhere.Click += (linkGoSomewhere_Click);
With an event handler looking like so in your code:
public void linkGoSomewhere_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
HOWEVER
In this situation, you don't need a server side control to just send the user somewhere else. You just need a simple hyperlink:
Go somewhere else

if this HyperLink you can do it using javascript but if it is LinkButton you can do it inside onclick event
<asp:LinkButton ID="MyLnkButton" runat="server" onClick="MyLnkButton_Click" Text="Click Me!">
protected void MyLnkButton_Click(Object sender,EventArgs e)
{
Response.Redirect("Default.aspx");
}

The onclick server side handler can be added to achieve this.
<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>

You can determine this with the Click event of the LinkButton

Related

OnClick Event Not Firing on Button Click ASP.NET

I have an asp:Button that is in a JavaScript dialog window. It has an OnClick event called DialogWindowButton_Click as you can see in the code below. The event is not firing and I have put breakpoints in the C# file and it is not even entering the function. I'm not sure why and have looked at other forum posts to try to figure this out. I have 1) deleted the button and recreated the button and OnClick event themselves (this didn't work), and 2) added CausesValidation="False" to the asp:Button tag. Neither avenue has worked. What I have is shown below:
<div style="margin:auto; width:100px; padding-bottom:15px;">
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>
Then in the C# file, I have:
...
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DialogWindowButton_Click(object sender, EventArgs e)
{
DialogWindowButton_ClickHelper();
...
}
protected AddressBookEntry DialogWindowButton_ClickHelper()
{
...
}
...
I have the correct file for the CodeBehind tag as well as for the Inherits tag. In the C# file you can see that the original OnClick event calls on a helper function defined directly below it, but breakpoints in the top of DialogWindowButton_Click() aren't being reached. There are no build errors either. Could there be something else I'm missing? Thank you!
You need to set UseSubmitBehavior to false (default is true):
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />
From Reference:
Use the UseSubmitBehavior property to specify whether a Button control
uses the client browser's submit mechanism or the ASP.NET postback
mechanism. By default the value of this property is true, causing the
Button control to use the browser's submit mechanism. If you specify
false, the ASP.NET page framework adds client-side script to the page
to post the form to the server.

How I can use HTML and CSS Tags with a C# Event?

net application and I want to create me a own Button with css and html and use the onclick event for execute c# code. But I get the error:
Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefine
Here is my Code:
ASPX file:
<div id="GuestListViewControls">
<table>
<td>Hinzufügen</td>
</table>
</div>
My C#:
protected void BtnAdd_Click(object sender, EventArgs e)
{
Response.Write("Add");
}
Can I use this Button with C# Code in a ASP.NET Application? :/
It's not a c# error, It's like a javascript error , Now You can use LinkButton for onclick event, It's look like also anchor tag
<asp:LinkButton ID="MyLink" runat="server" class="GuestButtons" OnClick="BtnAdd_Click" Text="Click Here"></asp:LinkButton>
or use onserverclick event instead of onclick event
test
You are using "A" tag. This is HTML Anchor control and it does not support server side event.
Use
<asp:Button id="mybutton" Text="Submit" runat="server" onclick="BtnAdd_Click" />
You can do it using HTML controls only
you just need to use on onserverclick
Sample Test stuff
test
code behind
protected void anchorclick(object sender, EventArgs e)
{
// do stuff here
}
See Refrence
HTML Anchor Server Side Click
HTML Button Click Server Side
You're getting this error:
Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefined
Because you're using an onclick event on an HTML anchor tag.
You need to use an asp:Button tag instead, for which the onclick event will point to a server-side method. Something like this:
<asp:Button class="GuestButtons" runat="server" onclick="BtnAdd_Click" text="Hinzufügen"></asp:Button>
You are getting "Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefine" because onclick searches for the method in the Javascript.
One option would be have a javascript method on the href click and from that method you can invoke the server side method using Ajax post or get or simple .Ajax
Please refer solution 4 in the url http://www.codeproject.com/Questions/334635/how-to-call-a-csharp-function-in-a-href-onclick-ev

On click event of image button

I have a problem that when I click on the image button instead of redirect me to the corresponding page, it will just redirect back to the same page.
This is the code in asp;
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" />
And this is in my page load in the code behind;
header1.Attributes.Add("onclick", "~/ChildSelection.aspx");
Any ideas why this is happening?
Your Image button should have an onclick event.
<asp:ImageButton ID="header1" ImageUrl="Resources/Icons/Header1.jpg" runat="server" OnClick="header1_Click" />
protected void header1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/ChildSelection.aspx");
}
The onclick event executes javascript if I'm not mistaking. OnClick refers to a server-side event. Try setting the window's location to redirect to the corresponding page.
header1.Attributes.Add("onclick",
string.Format("window.location = '{0}'", ResolveClientUrl("~/ChildSelection.aspx")));
I guess you can achieve it by replacing it by
header1.Attributes.Add("PostBackUrl", "~/ChildSelection.aspx");
Onclick is an event. You might also consider using a hyperlink with ImageSrc property instead of an ImageButton.
[Edit]
If you are just trying to redirect a better approach could be
<asp:HyperLink ID="header1" runat="server" ImageUrl="Resources/Icons/Header1.jpg">Click Here</asp:HyperLink>
And in code behind
header1.NavigateUrl = "~/ChildSelection.aspx";
Use the following code:
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" PostBackURL="~/ChildSelection.aspx"/>

JavaScript in button onclick not working

I have a webpage (ASP.NET C#) that has a button:
<asp:Button ID="btnHide" runat="server" OnClick="HidePanel" Text="Hide"/>
and I'm doing a JavaScript alert like:
public void HidePanel(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello","<script type=text/javascript> alert('Hello') </script>");
}
If I modify the function to not have object sender and EventArgs e then I can call it from Page_Load, and it works fine, I get the alert.
As written above I expect to see the alert when I click the button, but it's not happening. I'm sure it's something obvious, but I don't see it.
Use OnClientClick instead of OnClick. And add a return false to avoid a postback on the page.
<asp:Button ID="btnHide" runat="server" OnClientClick="alert('Hello'); return false;" Text="Hide"/>
You could try using,
ClientScript.RegisterClientScriptBlock(GetType(), "nameOfScript", scriptString);
I'ved used that before in a click event.
edit:
I can't edit posts yet, but your code works fine for me as well.
And depending on the situation he might want to do some server side stuff as well on the click event, or build up the script dynamically.
Edit:
If you already registered a script with the same name, it won't run, eg.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> var i = 0;</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> alert('other hello') </script>");
You can remove the code to register the JavaScript code and instead do this:
<asp:Button ID="btnHide" runat="server"
OnClick="HidePanel" Text="Hide" OnClientClick="alert('Hello');"
UseSubmitBehavior="false" />
This:
UseSubmitBehavior="false"
will cause the button to fire the client-side script, and it will run the server-side code (post-back).

How to make the button_Click event opens the page in a pop-up window in ASP.NET?

The question is simple but I can't find a simple answer to it! .. btw I'll need to pass a QueryString to the page to be open.
Any ideas ?
You can actually link a javascript code into .NET with C#, below is an example, you could replace with your info, and push the parameters.
Response.Write("<script type='text/javascript'>window.open('Page.aspx?ID=" + YourTextField.Text.ToString() + "','_blank');</script>");
You can append on the end of it ?Field=your value passing&nextField=another value.
Is the answer to do this in javascript. As you make the underlying page in asp.net, provide it with the javascript to catch the buttons onclick event and call window.open(URL)
It depends on what you're trying to do but the simplest is to use the OnClientClick property of the Button. Take a look at http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx, in particular the details bout this property a little bit down.
Basically you'd do something like
<asp:Button ID="Button1" Runat="server"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
With the JS to do your popup
<script type="text/javascript">
function ShowPopup() {
window.open('ThankYou.aspx');
}
</script>
You can also do both an OnClientClick and an OnClick if you need as well.
<asp:Button ID="Button1" Runat="server"
OnClick="Button1_Click"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
Code behind
protected void Button1_Click(Object sender, EventArgs e)
{
Label1.Text = "Server click handler called.";
}

Categories