Attaching event to a control - asp.net

How to insert an event to the aspx.cs page. I have one asp:button and i wish to add an event of that button in the aspx.cs page. how it done

Assume that you have a button like that :
<asp:Button runat="server" ID="myButton" />
You can add Click event at your code-behind like that :
myButton.Click += new EventHandler(myButton_Click);
Or you can specify your event at design like that :
<asp:Button runat="server" ID="myButton" OnClick="myButton_Click" />

Just add the following to your .cs file, and amend according to whatever event you are looking to raise.
public delegate void MyEvent(myParams);
public event MyEvent AnEvent;
If it just to handle the click event of your button you can just double click the button and it will automatically take you into the OnClick event in the .cs file.

At compile time, add the text:
OnClick="MethodName"
into the button declaration. MathodName is what gets called when the event is raised (in my example, the Click event).
To dynamically do this, look up the C# or VB.NET syntax for adding and removing event handlers. I think you will also have to ensure ViewState is saved or else the handlers will disappear on the first submit.

Related

asp.net: monitor user click

I'm trying to fire an event when a user clicks a hyperlink. But it would complain that the event is not defined:
<asp:HyperLink ID="HyperLink1" onmouseover="btnSubmit_Click" runat="server">www.google.com</asp:HyperLink>
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
protected void btnSubmit_Click(object sender, EventArgs e)
{
btnSubmit.Text = "clicked a link!!!";
}
I see several problems.
You do not have any sort of click event setup on your hyperlink. You do have a "onmouseover" but based on MSDN's documentation there is no click event for that control.
You have a button defined, but no events associated with that button.
You have a function that appears to be an event handler, but the naming convention suggests that it is associated with the button that has no events.
Can you provide more detail of what you are trying to do? I assume the c# code you have posted resides in the code behind?
Update:
Try changing your code to this -
<asp:LinkButton ID="lb_Link" OnClick="btnSubmit_Click" Text="www.google.com" runat="server" />
Obviously this will not redirect you, but based on what your code does, it doesn't sound like you want a redirect...
The event you're trying to trigger is a server side event. You need to use client side code for what you want to do. Plus, there is no property known as onmouseover, you can add it as a client side event from code behind
HyperLink1.Attributes.Add("onmouseover","yourClientFunction");//this can be done in page load

HTML buttons: Code-behind operation

Earlier, I had a button control on my web page. but now I changed it to a CSS button suting my needs. Before this, the button control was performing code-behind operation but now I switched to this CSS button.
Delete profile
How can I accomplish the same code-behind process now?
You can change the anchor to be a server side control (turning it to a HtmlAnchor control) and use the ServerClick event:
Delete profile
To keep it simple, you could use a linkbutton and get the server side click event
<asp:LinkButton ID="lb1" runat="server" onclick="lb1_Click" CssClass="cssClass" />

Passing additional arguments into the OnClick event handler of a LinkButton using Javascript

I have a ASP.NET Website, where, in a GridView item template, automatically populated by a LinqDataSource, there is a LinkButton defined as follows:
<asp:LinkButton ID="RemoveLinkButton" runat="server" CommandName="Remove"
CommandArgument='<%# DataBinder.GetPropertyValue(GetDataItem(), "Id")%>'
OnCommand="removeVeto_OnClick"
OnClientClick='return confirm("Are you sure?");'
Text="Remove Entry" />
This works fine. Whenever the Button is Clicked, a confirmation dialog is displayed.
What I am trying to do now, is to allow the user to enter a reason for the removal, and pass this on the the OnClick event handler. How would I do this?
I tried OnClientClick='return prompt("Enter your reason!");', but, of course, that did not work =)
Personally, I would stash the reason into a hidden field.
It would work something along these lines: your OnClientClick method would take the return value of a JS method, which does the prompt, and then places the result of the prompt into the hidden field.
You can also look into calling __doPostBack from your client-side code instead of using the OnClick postback. Then you can capture the reason and pass it server-side.

Cannot add event handlers to ASP. net page

In visual studio 2008 when I drag and drop asp controlXXX on aspx page. Property page for this control, does not show event handlers button. It can reappear after switching to designer view but then disappears again. Screenshot attached.
Yes, that's annoying, but it works probably only in designer.
But you can still add event handlers manually
in html add onclick property and write name of the method
<asp:ImageButton ID="btnAdd" runat="server" onclick="btnAdd_Click" />
and in code-behind add method with two parameters of types: (object, EventArgs) like this:
protected void btnAddTag_Click(object sender, ImageClickEventArgs e)
{
}
or you can also add event handler in Page_Init method
btnAdd.Click+=new ImageClickEventHandler(btnAdd_Click);
(this will also generate automatically the method, only after += press TAB twice)

Reverse order of operations for OnClick and OnClientClick?

I have some simple javascript that I'd like to run when a button is clicked, but I also want some postback action to occur on the server. The logical code for this looks like this:
<asp:Button ID="btnOK" runat="server" Text="Save Changes" OnClientClick="UpdateParent();" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="window.close();" />
<script language="javascript" type="text/javascript">
function UpdateParent()
{
window.opener.document.location.reload(true); // or should we postback instead?
window.close();
}
</script>
Basically, a popup window should refresh its parent and then close itself. However... if I call window.close(), the postback does not occur and the button handler is not called. But obviously OnClientClick is called before the postback happens. Am I going to have to emit this javascript in the button handler and run it when the page loads after postback? If so, what is the proper way to do this these days for ASP.NET 2.0?
It's a shame that the code above doesn't work as it's elegantly simple and straightforward.
You have to do the postback before closing the window. Also you want to do the postback before refreshing the parent window, as I guess that the reason to refresh the window is to display the information that you are about to save.
Use the RegisterStartupScript in the ClientScript object to run the code after postback:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload(true);window.close();", true);
However, if the parent page is a result of a postback, this would cause a dialog window in the browser informing the user that a post request is needed to reload the page. To avoid this you would have to do something like calling a function in the parent page that could do a postback to update the page.

Resources