Button_Click method no longer works - c#

I am modifying a large (11 page) web form that I inherited from a previous employee. I am new at ASP and HTML5, etc. I modified many lines of code, but I haven't touched the button code in either HTML or C#. The Button_Click method used to work, in that clicking the button would run the form validation code and the data saving code. Here is the HTML:
<div style="position: relative; float: right">
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" class="btn btn-primary" />
</div>
And here is the code-behind for Button_Click:
protected void Button1_Click(object sender, EventArgs e)
{
if (ValidationForm())
{
SaveData();
}
}
I put breakpoints inside Button1_Click and the function is never entered. Is there another way to debug to see what's going on?
Here's the top of the aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="smacklabProject._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
...
I have not changed the inherits. I don't even know what inherits does.
Here's the top of the aspx.cs (below using):
namespace smacklabProject
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ValidationForm())
{
SaveData();
}
}
private void SaveData()
{
string study = Study_Dropdown.SelectedValue;
...

OK this happened to me once.
So if you're using visual studio, click the button at the top that has the start sign to run it and start debugging.
If you run it by right-clicking it from the solution explorer and selecting the view in browser option, it would not consider the back-end code.
I can't comment yet Sadly that's why i had to post this as an answer.

A co-worker solved the problem for me. There was a hidden text box that had a range validator and a default value that was outside of the range. So it would always fail validation, but the failure text was hidden.
For reference, never do this:
<p>
<asp:RangeValidator runat="server" ID="RangeValidator1" ControlToValidate="IPAQ_Q2_Days" MinimumValue="1" MaximumValue="7" Type="Double" Text="The value must be from 1 to 7" Font-Size="Large" ForeColor="Red" />
</p>
<asp:Label ID="IPAQ_Q2_Days_Label" AssociatedControlID="IPAQ_Q2_Days" ForeColor="White" Font-Size="11" Font-Weight="Normal" Text="Please indicate the number of days you performed vigorous activities at work." runat="server">
<asp:TextBox ID="IPAQ_Q2_Days" runat="server" class="form-control" placeholder="___ days per week" Width="200" value="0"></asp:TextBox>
</asp:Label>

Related

Is it possible to open popup window in button click event without using jquery in asp.net C#?

Hi I want to open popup window in button click event without using j-query in asp.net C#.
This is simple task i am familiar with Asp.Net MVC. But i am not familiar in Asp.Net so only i am struggling for this simple task.
Please any one tell me how to open popup window by button click event without using jquery in asp.net C#.
My web form
SpecilalityMaster.Aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="specialitymaster.aspx.cs" Inherits="popupwindow.admin.specialitymaster" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<form id="frmPopupExample" runat="server">
<asp:Button ID="btnOpenPopupWindow"
runat="server"
Text="Open Popup Window" OnClick="btnOpenPopupWindow_Click"
/>
</form>
<div class="popup">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</div>
</asp:Content>
specialitymaster.aspx.cs
public partial class specialitymaster : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
i just tried simply. i designed one button and one popup window.if i click open popup window in button click event i need to open popup window. i donno it is possible or not.i created button click event in CS page but i donno how to call that popup window name in button click event .
example
Desirable result: Please see image.
If I understand you correctly, you don't need a popup, you just trying to activate some div on your page. You will need to make this div server side
<div class="popup" runat="server" style="display:none" id="divPop">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</div>
and then in your server side click
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
divPop.Attributes.Add("style", "display:block);
}
this will make your div visible
EDIT
better solution in your case would be using asp panel instead of div.
<asp:panel id="divPop" visible="false" runat="server">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</asp:panel>
and then you will be able to manipulate visibility in your server side code very easy
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
divPop.Visible=true;
}
you are using server side control in your code.
<asp:Button ID="btnOpenPopupWindow"
runat="server"
Text="Open Popup Window" OnClick="btnOpenPopupWindow_Click"
/>
click of this button will trigger immediate post to the server and server (your ASP.NET application) will handle the event. The only way you will be able to open a popup from the server if you will register script to do this. Your CLick should look something like
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"yourWindowName",
String.Format("<script>window.open('{0}');</script>", yourURL));
}
Use this:
var DefineWindowSize= "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var URL = "https://www.google.com";
var win = window.open(URL, "_blank", DefineWindowSize);
Create a function and add it inside the function.

Ektron Multiview not switching views

Completly stumped here, Im creating a widget in ektron that was working fine 2 days ago and all the sudden all events stopped working for no reason. Ive been trying to track down the culprit to no avail.
My aspx file is a basic multiview with two views, one for edit settings and the other for content.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ContentPlus.ascx.cs" Inherits="widget_ContentPlus" Debug="true" %>
<asp:multiview ID="contentPlusWidget" runat="server">
<asp:View ID="vEdit" runat="server">
<table>
<tr>
<td class="label">Content: </td>
<td><asp:DropDownList ID="ddlContentType" runat="server" class='displayType'></asp:DropDownList></td>
</tr>
<tr>
<td><asp:Button ID="btnSave" runat="server" Text="Save" /></td>
<td><asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
</tr>
</table>
</asp:View>
<asp:View ID="vContent" runat="server">
<!-- Content Code -->
</asp:View>
</asp:multiview>
Then in the contentplus.ascx.cs file I have my page events and then my btn events
protected void Page_Init() {
_host = Ektron.Cms.Widget.WidgetHost.GetHost(this);
_host.Title = "My Widget";
_host.Edit += new EditDelegate(EditEvent);
_host.ExpandOptions = Expandable.ExpandOnEdit;
btnSave.Click += new EventHandler(btnSave_Click);
btnCancel.Click += new EventHandler(btnCancel_Click);
}
protected void Page_Load() {
if(!this.IsPostBack)
ChangeViewTo(vContent);
}
protected void btnSave_Click(object sender, EventArgs e) {
Javascript.Alert("SAVED!");
SaveSelectedIndex();
ChangeViewTo(vContent);
}
protected void btnCancel_Click(object sender, EventArgs e) {
Javascript.Alert("CANCEL!");
ChangeViewTo(vContent);
}
public void EditEvent(string settings) {
Javascript.Alert("EDIT!");
ChangeViewTo(vEdit);
PopulateDropDownBoxWithAvailableContent();
}
void ChangeViewTo(View view) {
Javascript.ConsoleLog(view.ID);
contentPlusWidget.SetActiveView(view);
if(view.Equals(vEdit))
PopulateDropDownBoxWithAvailableContent();
}
Several things are not working, first when the settings button is clicked, the screen turns grey with the spinning icon and when it comes back nothing has happened. This usually happens when the _host.Edit += new EditDelegate(EditEvent); delegate has not been set, but as you see it has been. Second, When I set the settings view to default, and I press the save or cancel buttons, you get the spinning dialog and then nothing. The event code is never triggered. But you can see that I assigned the event delegate in Page_Init()
I feel like Im missing something obvious but Ive been stumped for a day. Any ideas?
Edit: changed title to reflect current understanding of the issue
OK found the solution and ill post it for anyone else.
When Ektron does a ajax postback it expects the response to be in the format
1|#||4|6696|updatePanel|ctl00_cphBillBoard_dzLeft_updatepanel|
<div id="ctl00_cphBillBoard_dzLeft_dzcontainer" class="dropzone PBClear" EditMode="true">....<the rest of your content>.....
Those pipes in the beginning are very important, in several places in my code I was dynamically injecting javascript for debugging inside the browser purposes, I was also logging some error messages to the screen using Response.Write();
these got injected before my pipes so it messed up the ajax request. When I realized this and removed all my logging, everything worked!

I am unable to cancel an ASP.NET page

I am new to web programming. I have a pretty standard page that accepts contact information and I am trying to add validation controls to it. When the user clicks the cancel button nothing is saved so I just want to unload the page and go back to the previous page. My problem is, even the the cancel button has CausesValidation="false the validation events are still firing and a post back does not occur. I am using Visual Studio 2012 and C#. I've created a test page to reduce the code I'm working with to the minimum required code to demonstrate the issue. I have tried the javascript disableValidation() function with both Page_Validation = false; and the loop to disable the individual validation controls without success. When I check Page_Validation it is false, and the loop throws the exception Page_Validators is undefined.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TestMe.aspx.cs" Inherits="ElmoWeb.TestMe" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Simple Little Test Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="uxEmail" runat="server" placeHolder="email address" type="email" AutoCompleteType="Email"></asp:TextBox>
<asp:RegularExpressionValidator ID="uxEmailValidator" runat="server"
ControlToValidate="uxEmail"
ValidationExpression="^[a-zA-Z][\w\._%+-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
ErrorMessage="The email address is invalid"
EnableClientScript="false" ValidationGroup="testing"
Display="Static"/>
</div>
<div>
<asp:Button ID="Submit" runat="server" Text="Submit" CausesValidation="true" UseSubmitBehavior="true" ValidationGroup="testing" />
<asp:Button ID="Cancel" runat="server" Text="Cancel" CausesValidation="false" UseSubmitBehavior="true" ValidationGroup="FakeValidationGroup" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" HeaderText="You must correct the following errors." ValidationGroup="testing" />
</div>
</form>
</body>
</html>
Currently the code behind just writes a message to the output window so I can see if the post back occurred.
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("TextMe.aspx Page_Load event, sender = " + sender.ToString() + ", EventArgs = " + e.ToString());
}
The natural way of solving issues where two or more submit buttons should behave in a different way is to use validation groups.
A validation group is set with the ValidationGroup attribute on both a submit button and a group of validators. The same value binds a button and validators and only validators with the same value of the attribute fire when the button is clicked.
In your case, setting the ValidationGroup on the accept button and your validators to "Accept" and leaving the default (empty) value on the cancel button would solve your issue. This is much simpler than any of the workarounds you tried.

Why IE does not refresh the page after a JS call to a click button?

On a ASP.Net page, I am in front of a problem only with IE. Here is the description.
On a page, there is two buttons and one label. The first button is visible and calls a JS function on the click event. This JS function calls the click function of the second button. The second button has an C€ event handler on the click event. The C# event handler edit the label.
In Firefox : the label is correctly edited after the clicks.
In IE (8) : the label is not edited, despite the C€ event handler has been correctly hit.
Also, I observed, in IE, that the Page_Load event is called two times after the JS button click :
Page_Load
button2_OnClick => change of the Label Text
Page_Load => The Label Text is reset :(
In Firefox, the Page_Load is called only once.
My question is : how to make IE refresh correctly the page as Firefox does after a JS button click ?
Below is the sample test code :
1) Page ASPX
<head runat="server">
<title></title>
<script type="text/javascript">
function button1Click(sender, args) {
var button2 = document.getElementById("button2");
button2.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="button1" Text="Click-me!" OnClientClick="button1Click();" />
<asp:Button runat="server" ID="button2" Text="Second" OnClick="button2_OnClick" style="display:none" />
<p />
<asp:Label runat="server" ID="label1" Text="Init" />
</div>
</form>
</body>
</html>
2) C# code-behind :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_OnClick(object sender, EventArgs e)
{
label1.Text = "Changed";
}
}
The ID of your button will not be button1 or button2 when it's rendered. It will probably be something like ctl001_button1. Therefore your javascript will not work. In ASP.NET 4 you can override this behaviour by using an assigned ClientID.
<asp:Button runat="server" ID="button1" Text="Click-me!"
OnClientClick="button1Click();" ClientIDMode="Static" />
<asp:Button runat="server" ID="button2" Text="Second"
OnClick="button2_OnClick" style="display:none" ClientIDMode="Static" />
As an aside, this alludes to the main problem with ASP.NET Winforms - it tricks developers into thinking that the web is a connected environment.
What actually happens when you click an <asp:Button /> element by default is that a postback is invoked. I.e. Your browser sends a request to the server for a new page. It sends up something called ViewState which is how the server knows what you've done and what to render. There is no "event" handled as such.
I think the problem is with the way you are trying to get the hidden button
var button2 = document.getElementById("button2");
maybe change this to
var button2 = document.getElementById("<%= button2.ClientID %>");
After the buttons are rendered in the browser, the ID is changed by the ASP.Net engine, and not the same as your source.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Hope this helps.

Setting focus onto another contol from button click

I am using ASP.NET 2.0 and VB.NET (C# code will also help me out)
On top of my page I have a button called btnViewRecords. When the User click on the button I want to set focus to another button or label further down on the same page. How can this be done.
This code does not work for me..............
btnTheRecords.Focus()
or
lblHeader.Focus()
Thanks in advance!!
Edit:
Even if my code did work, i dont want to reload the page every time.
Here's a relatively simple solution...
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return SetFocus()" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<script type="text/javascript">
function SetFocus() {
document.getElementById('<%=TextBox2.ClientID %>').focus();
//or this if you're using jQuery
//$("#<%=TextBox2.ClientID %>").focus();
return false;
}
</script>
You can use JavaScript to do this.
document.getElementById ( "btnTheRecords" ).focus();
By setting focus what do you mean. Do you want to bring this control into view if it is way down the page. Then there are better ways to do this.
Edit:
You can place an anchor tag near the button or the bale and set
location.href = "#anchorId";
where anchorId is the id of the anchor tag.
will move the focus to the anchor tag.
Try this,
I wrote a small code and it seems to be working for me. sorry this is in C# but if you click on a button and set focus on another button then page will automatically scroll down for you.
ASP.NET
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div style="height:50px;"></div>
<div>
<asp:Button ID="Button1" runat="server" Text="Button 1" onclick="Button1_Click" />
</div>
<div style="height:1000px;"></div>
<div>
<asp:Button ID="Button2" runat="server" Text="Button 2" onclick="Button2_Click" />
</div>
</form>
</body>
</html>
AND Code Behind
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button2.Focus();
Label1.Text = "button1 clicked & button2 in focus";
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Focus();
Label1.Text = "button2 clicked & button1 in Focus";
}
}
I'm not sure exactly of the problem you're having. But in cases where the SetFocus() function and such just don't work (and they often don't in VB for some reason). I've often used workarounds such as shortcut Keys & mnemonics in combination with SendKeys() to get focus where I need it to be.
Depending on whether the buttons you are talking about are in your ASP.NET code or in your VB.NET code, this may be a viable workaround for you as well.
document.getElementById("spanId").scrollIntoView(false)
Reference:
.scrollIntoView
If the button is server side, set the "OnClientClick"
action on it to a javascript function. Cancel windows
events so the page location does not change.
function goto(){
window.event.cancelBubble = true;
window.event.returnValue = false;
document.getElementById("pageloction").focus()
}
asp:Button runat="server" ID="btnTheRecords" OnClientClick="javascript:goto()" />
span id="pageloction">

Categories

Resources