On click event of image button - c#

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"/>

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.

OnClick event of button fires everytime on PageLoad

I have a button below:
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" />
Event handler of this button on server side is :
protected void btnApprove_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('Button Approve Clicked')", true);
}
Just get alert on the button click from the server side.
My issue is that once I clicked on Approve button, now when I load or refresh my page again this btnApprove_Click event gets executed everytime.
I have many others button on the same form but none shows this strange kind of behaviour. I tried to change this button as HTML but still the same behaviour.
Can anyone please help me to get out of it. Thanks in advance.
You should use IsPostBack on Page load to prevent every time page load on button click. You can check every time on Page_Load.
Sample Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// First Time Load When User Come
}
else
{
// Every Time Load When Any click button in page
}
Try to Add OnClientClick="return false;"
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" OnClientClick="return false;"/>
How about using RegisterOnSubmitStatement instead?
Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "key", "alert('Button Approve Clicked')");

How to implement a click event on textbox in ASP.NET?

In my web application I need a functionality so that when users click on textbox to input values, it should make the button and the other fields visible?
I am using the code provided below but, could not get it working.
C#:
protected void TextBox1_Click(object sender, EventArgs e)
{
ButtonSearch.Visible = true;
}
ASP.NET:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" Visible="False" />
How to accomplish this?
Set AutoPostback="True". This way the event will be fired server-side, and the button will become visible.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click" AutoPostBack="true"></asp:TextBox>
However, if you only want to toogle visility of a button, you really should considerate javascript. This will save a trip back to the server.
<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" style="display:none;" />
<script type="text/javascript">
function txtBox1_ClientClicked(){
var theButton = document.getElementById('<%=ButtonSearch.ClientID%>');
theButton.style.display = 'block';
}
</script>
You do not need to post back to the server to accomplish your job. You can use client side onFocus event and javascript/jquery, for example.
I know I used an input of type text, and you are using an ASP Control which posts on server, but here is a JSFiddle to get you on the right track: http://jsfiddle.net/Mmjtz/1/
$("<%= ButtonSearch.ClientID %>").click(function(){
$("#TextBox1").show():
});
In this code you need to pass fields ID which you want to visible on the click of button.
Put the textbox inside a div and use the div's onClick event from codebehind. It's not what you asked but it works for me without any errors. Here is a javascript function to implement requested event:
function toggleVisibility()
{
document.getElementById('TextBox1').disabled = true;
/*
...some other code...
*/
}
And of course, you have to define your onclick event at the div definition after implementing this JS function.
<div id="TBdiv" onClick="toggleVisibility()">
<asp:TextBox ID="TextBox1"..../>
</div>
IMPORTANT: Since you now disabled your TextBox from codebehind, you have to enable it in somewhere before you want to use it again. Otherwise you will not see it while the page is running.
jQuery is the perfect solution for your problem. The code would be something like this:
$("#TextBox1").on("click",function(){$("#ButtonSearch").css("visibility", "visible");})
You include the script by adding <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the page and then you can add the piece of code above to within <script></script> tags.

Click Event of Hyperlink

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

Using a PostBack on a LinkButton in ASP.NET

I have an .ascx control in my searchresults.aspx page:
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt" PostBackUrl="~/searchresults.aspx?type=topics"
runat="server"/>
But when I click on it, it does the postback but the type=topics doesn't appear to be sent. Any suggestions?
Try out HyperLink to navigate to an other page:
<asp:HyperLink NavigateUrl="~/searchresults.aspx?type=topics" />
From MSDN:
HyperLink
A control that displays a link to another Web page.
On LinkButton class page:
If you want to link to another Web page when the control is clicked,
consider using the HyperLink control.
EDIT: Answer to comments
Remove PostBackUrl from LinkButton
Add <asp:LinkButton OnClick="OnTopicsTypesEnabled" ... />
In code behind (searchresults.aspx.cs)
protected void OnTopicsTypesEnabled(object sender, EventArgs args)
{
// handle this particular case
}
I believe your code will perform an POST, whereas you need a GET to transfer your variables through QueryString.

Categories