asp.net: monitor user click - asp.net

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

Related

asp:checkedchanged event not firing?

there. I have a gridview with a column of check boxes which was working when it was a small test project but when adding it to a page on my teams project it stopped firing the checkedChanged event. The check mark still appears or disappears but nothing fires. The only major difference is I was using an sqlDataSource object at first but for the new project i had to bind it to a database in the behind code.
Here's my html:
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
and some simple behindcode:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Console.WriteLine("clicked!");
}
set AutoPostBack to True to enable post back
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
I found the solution. I added the conditional the block if(!page.isPostBack) around the stuff in my page load event.
Kept searching for about an hour till I found out that the actual reason why the OnCheckedChanged event was not firing on my page was due to duplication of names. Duh
An input control had a name 'submit' and there also exists a method in my JavaScript that is called submit(). The JavaScript interpreter was confused between the name of the control and a function, every time it needed to fire the event. Changed the name of the control and everything went back to working perfectly.

How to submit data twice on an ASP.NET page

I have a webpage. At the top is a search bar that is inside a <form id="form1" runat="server">.
I also want to add a form on that same page that would allow users to register their details. Problem, ASP.NET only allows one form per page.
How can I achieve my goal? Any workarounds?
You can only have one server side form on a page.
If it is an option, you can have a client side form (without runat="server") with a separate action - this POST can go to a different page, where you will have to accessRequest.Form` to retrieve the values posted.
Another option is to use separate buttons for posting with different event handlers.
You can use the simple HTML form approach but there is the problem of always post the whole page back. or use mvc:
Are multiple forms on a single page supported in asp.net 3.5?
Put the code you want to be called in different button click events. Therefore, if the search button is clicked, only the code in the search buttons click event is run. If the register button is clicked, only the code in the registers click event is run.
Here is an example:
protected void Page_Load(object sender, EventArgs e) {
// Common code
}
protected void btnSearch_Click(object sender, EventArgs e) {
// Search code
}
protected void btnRegister_Click(object sender, EventArgs e) {
// Register code
}
Double clicking the buttons in the designer will create the click events in the code behind.
As already stated in the answer by #Simon, it's easy to have multiple click handlers in your code-behind to process exactly what you need on the page - this is the easiest way to solve the "lack of multiple forms" issue.
Something very useful in this situation is the DefaultButton attribute of the <asp:Panel> control. This means that should you have multiple areas of your page with (for instance) <asp:TextBox> controls, and each of those areas has a specific <asp:Button> associated, if the focus is in one of the textboxes then pressing Return or Enter will result in the DefaultButton being clicked.
For example...
<asp:Panel runat="server" DefaultButton="btnSearch">
Search: <asp:TextBox runat="server" id="txtSearcn"/>
<asp:Button runat="server" id="btnSearch" Text="Search" OnClick="btnSearch_Click"/>
</asp:Panel>
See MSDN for more information on the DefaultButton attribute of <asp:Panel>

Working with checkbox in treeview asp.net

i want to know how to program the checkbox checked inside treeview, i want to write code when user checks checkbox inside the treeview in asp.net, i got the event known as TreeNodeCheckChange event, i wrote a response.write() message inside it, but when i check the checkbox, nothing happens, does the asp.net treeview supports handling the checkbox from code behind.
Thanks in advance.
When u click on the check box the postback event won't fire, this is ootb settings. You have to check the checkbox first and then click on the checkbox title. Only then the postback event will fire.
Then in the code behind you can access the checkbox node properties using this :-
protected void someTree_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
if (e.Node.Checked)
{
}
}
The other workaround (the more user friendly way) is to fire the postback immediately when the checkbox is checked. In order to do that you can follow this tutorial here:- http://www.keirgordon.com/post/PostBack-on-TreeView-Checkbox-Click.aspx
Hope this helps.
Try setting SelectAction="Select" on the TreeNode element.
<asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
<Nodes>
<asp:TreeNode ShowCheckBox="true" SelectAction="Select" />
</Nodes>
</asp:TreeView>
Here is a nice walk-though:
ASP.NET TreeView and Checkboxes

Asp.net page timeouts altough button is clicked in an update panel

I have an asp.net page for a simple blog which looks like
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine">Default Text</asp:TextBox>
<asp:LinkButton ID="lbSend" runat="server" Text="Send" OnClick="lbSend_Click" />
<script type="text/javascript">
//some javascript to click btnSave in update panel for every five minutes.
</script>
<asp:UpdatePanel ID="upSave" runat="server" Style="display: none;">
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And in the code behind
protected void lbSend_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table A with txtContent
}
protected void btnSave_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table B with txtContent
}
Everything works okay. I fill the form and insert the content into tables with lbSend or btnSave. JavaScript is also clicking btnSave. However when it takes long to fill txtContent, about 40 minutes or more, the page is like expired. Clicking lbSend refreshes the page in an asynchronous way and everything you have written is gone. It is like the page have sleeped and wakes ups when I click. And lbSend works normal when you click it again.
What is the reason of this "sleep" altough btnSave continues to communicate with server? And how can I prevent it.
You are doing an Ajax post. Since it is an asynchronous post your browser happily moves on and you don't notice a problem until there is an error. If it was not Ajax then the whole page would have refreshed and you would have seen an error - assuming you display errors in a friendly format.
While implementing Ajax you should implement your own error handler so you can display to the user that an error has occurred. Also, you should implement a floating div or dialog that prevents the user from using your web page until the Ajax request has completed. I realize 40minutes is really really bad for a simple insert but I will leave that for your DBA to fix.
Let's talk about the floating div or dialog or "modal popup". You want to open it in the before_Ajax_send function and close it in your after_Ajax_receive function. Please refer to your Ajax library documentation for more information on these functions.
When you implement this dialog box it will enhance your user experience because it will let the user know that your application is doing something so please hold your horses.
Hope this helps.

Attaching event to a control

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.

Resources