Parser error when trying to generate gauge chart - asp.net

I'm trying to generate a gauge chart based on changing MTBF values, However when I run the webapplication, I get the error
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'WebApplication1.WebForm3' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
Line 1:
Line 2: <%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>
Line 3:
Line 4:
Source File: /WebForm3.aspx Line: 2
THe code that i have used is as follows,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebApplication1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (Testme entities = new Testme())
{
var execution = entities.TestExecutionDetails.Where(p => p.TestExecutionID == TestExecID).Select(p => p).OrderBy(p => p.StartTime).ToArray();
var failures = entities.TestExecutionDetails.Select(p => p.Result != "Pass ").Count();
var uptime = entities.TestExecutionDetails.Where(p => p.Result == "Pass ").Select(p => p);
TimeSpan elapsedDuration = new TimeSpan(0);
foreach (var p in uptime)
{
elapsedDuration += (p.EndTime.Value - p.StartTime.Value);
}
var mtbfdisplay = elapsedDuration.TotalSeconds / failures;
string str1 = #"
data = google.visualization.arrayToDataTable([";
string str2 = #"['Label', 'Value'],['MTBF'," + mtbfdisplay + #"],]);";
string str3 = #"var options = {
width: 400, height: 120, redFrom: 90, redTo: 100, yellowFrom: 75, yellowTo: 90, minorTicks: 5
};
var chart = new google.visualization.gauge(document.getelementbyid('chart-success'));
chart.draw(data, options);";
string postData = str1 + str2 + str3;
Console.WriteLine(postData);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "test", postData, true);
}
}
}
}
The aspx code is as follows
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Label ID="Label2" runat="server" Text="1"></asp:Label>
<br />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
<br />
<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<div id="chart-success"></div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
I'm really stuck, I don't even know if this is the right approach to generate the charts, please help.
Thanks

Class WebForm3 is not declared anywhere (at least in the code that you have posted), so most likely this line:
public partial class WebApplication1 : System.Web.UI.Page
should be
public partial class WebForm3 : System.Web.UI.Page

Related

How to bind a generic list of enums to a GridView

I am unable to bind a generic list of enums to a GridView, below are all of the details:
Error Message
The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
ASPX Page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Amikids.Pages.WebForm1" %>
<!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:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Amikids.Pages
{
public partial class WebForm1 : System.Web.UI.Page
{
public enum Pet
{
Cat = 0,
Dog = 1,
Bird = 2
}
protected void Page_Load(object sender, EventArgs e)
{
List<Pet> pets = new List<Pet>();
pets.Add(Pet.Bird);
pets.Add(Pet.Dog);
GridView1.DataSource = pets;
GridView1.DataBind();
}
}
}
I was not able to figure out how to directly bind a list of enum values to a GridView. As work-around I converted the list of enum values to a list of string values.
Below - Code for Converting list of enum values to list of string values.
public static List<string> ConvertListOfPetsToListOfStrings(List<Pet> pets)
{
List<string> listOfStrings = new List<string>();
foreach (Pet pet in pets)
{
listOfStrings.Add(pet.ToString());
}
return listOfStrings;
}
Below - Code for binding to GridView
GridView1.DataSource = ConvertListOfPetsToListOfStrings(pets);
GridView1.DataBind();
Below - ASPX markup for binding to template field and setting a meaningfull HeaderText.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Type of Pet">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

DropDownList1 does not exists in the current context error

I have an aspx application. I have defined a dropdownlist in the .aspx file and try to load some data into it from database in .aspx.cs file. But I am getting the error in .aspx.cs as "DropDownList1" does not exists in the current context.
Below is the code:
Wiki.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Wiki.aspx.cs" Inherits="FinalProj2._Wiki" %>
<!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>
<style type="text/css">
body { margin: 4%; }
#space1 { height:1em; }
#space2 { height:1em; }
</style>
</head>
<body>
<h1>WIKI</h1>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100%"></asp:DropDownList>
</div>
<div id="space1"></div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack = "true" Height = "150px" Width = "100%"></asp:ListBox>
<div id="space2"></div>
<asp:TextBox ID="TextBox2" runat="server" Height="250px" Width="100%" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
Wiki.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FinalProj2.Controllers;
namespace FinalProj2
{
public partial class _Wiki : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();
Response.Write("<br/>Page.User.Identity.Name: " + Page.User.Identity.Name);
Response.Write("<br/>Page.User.Identity.IsAuthenticated: " + Page.User.Identity.IsAuthenticated);
Response.Write("<br/>Page.User.Identity.AuthenticationType: " + Page.User.Identity.AuthenticationType);
var query = from meet in db.Meets
select meet.Summary;
// where meet.Meeting_ID = (from meet_emp in db.Meet_Emps
//where meet_emp.Employee_Name == Page.User.Identity.Name
//select meet_emp.Meeting_ID)
DropDownList1.DataSource = query;
DropDownList1.DataBind();
}//end
}
}
What could be the possible reason. According to me I feel everything is fine as had used the same way for a list box into another application.
Make sure you have no files that accidentally try to inherit or define the same (partial) Mlass as other files.
Thus no other code behind file should have same signature in FinalProj2 Namespace.
public partial class _Wiki : System.Web.UI.Page

Custom validator dynamic ServerValidate using Reflection

I have a UserControl which contains a TextBox and a CustomValidator.
I would like to set the CustomValidator.ServerValidate to a method in the page that contains the UserControl
I found this code which will allow me to dynamically set the custom validators validation function:
cusvCustom.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(MethodName);
The problem is that a string value won't work there. It needs to be a reference to the method. Is it possible to use reflection (or some other method) to get a valid reference to the parent controls method using only the string name of it? The reason I want to use the string value of the method name is so I can place the control on the page thusly:
<uc1:TextBoxField ID="tbUserName" runat="server" CustomValidationMethod="ValidateUserName" />
I did some research and I found Type.GetMethod and MethodInfo but I can't get them to work. Primarily because I don't know the type of the parent control and can't figure out how to get it.
EDIT: My code for matt-dot-net
WebUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" Inherits="WebApplication1.WebUserControl" %>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed" OnServerValidate="CustomValidator1_ServerValidate" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />
WebUsecControl.ascx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebUserControl : System.Web.UI.UserControl
{
public ServerValidateEventHandler Validating;
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
if (Validating != null)
Validating(sender, e);
}
}
}
TestPage.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="WebApplication1.TestPage" %>
<%# Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!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>
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />
</div>
</form>
</body>
</html>
TestPage.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//WebUserControl1.Validating += WebUserControl1_Validating;
}
protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
}
}
}
As you can see it's almost an exact duplicate of your code. For whatever reason it does not work for me as I have it here. When I click on the button the page reloads and is the same. When I un-comment the one line though and click the button then I see the error message.
I think this is what you want to do:
WebUserControl.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed"
ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />
And in your code behind:
public partial class WebUserControl : System.Web.UI.UserControl
{
public event ServerValidateEventHandler Validating;
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
if (Validating != null)
Validating(sender, e);
}
}
This will allow you to use the control on a page the way you want to:
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />
Then in your .aspx.cs code behind:
protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
{
//do validation here
}

LinkButton's server-Side event not firing when disabled=true, and inside an UpdatePanel, in IE8

It is hitting the Page_Load event, but not the LinkButton's click :
<%# 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:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:LinkButton ID="btnRenewAll" runat="server" onclick="LinkButton1_Click" OnClientClick="javascript:return ClientMe()">LinkButton</asp:LinkButton>
<br />
<asp:Label ID="lblMe" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
<script>
function ClientMe() {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
return true;
}
</script>
</html>
CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lblMe.Text = "Checked";
UpdatePanel1.Update();
}
}
If you replace your JS with:
function ClientMe() {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(startRequest);
return true;
}
function startRequest(sender, e) {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
}
it should work. citing:
link text
Change this line:
btnRenewall.disabled = 'disabled';
Here is a list of the minimized attributes in HTML and how they should be written in XHTML :
HTML XHTML
compact compact="compact"
checked checked="checked"
declare declare="declare"
readonly readonly="readonly"
disabled disabled="disabled"
selected selected="selected"
defer defer="defer"
ismap ismap="ismap"
nohref nohref="nohref"
noshade noshade="noshade"
nowrap nowrap="nowrap"
multiple multiple="multiple"
noresize noresize="noresize"

How to use validator in this case is ASP.NET?

I have two input text boxes which are decimal.
The sum of the two inputs cannot be more than 100. When input1 is 40, input2 cannot be more than 60.
I need to do everything in client side and need to allow client to enter any value. If the entered value is more than the limit, I need to show the validator error message:
Page.isValid = false
Now I have two separate validator for each input box but I don't know how to change the valueToCompare of two validators in client side.
Please advise,
Thanks
You should look into using the CustomValidator control. Here's an article that walks you through using it.
Your code could look something like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1._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>
<script type="text/javascript">
function validateTextBoxen(sender, args) {
// You'll have more thorough validation, I'm sure
var value1 = parseFloat(
document.getElementById('<%=textBox1.ClientID%>').value);
var value2 = parseFloat(
document.getElementById('<%=textBox2.ClientID%>').value);
args.IsValid = (value1 + value2) < 100;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="textBox1" runat="server" />
<asp:TextBox ID="textBox2" runat="server" />
<asp:CustomValidator runat="server" EnableClientScript="true"
OnServerValidate="onCustomValidation" ID="customValidator"
ErrorMessage="Invalid!"
SetFocusOnError="true" ClientValidationFunction="validateTextBoxen"/>
<asp:Button runat="server" OnClick="button_Click"/>
<asp:Literal runat="server" ID="placeholder" />
</div>
</form>
</body>
</html>
Code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void onCustomValidation(
object sender, ServerValidateEventArgs e)
{
float value1 = 0f;
float value2 = 0f;
if (!float.TryParse(textBox1.Text, out value1)
|| !float.TryParse(textBox2.Text, out value2)
|| value1 + value2 > 100f)
{
e.IsValid = false;
}
}
protected void button_Click(object sender, EventArgs args)
{
placeholder.Text = Page.IsValid ? "Valid" : "Invalid";
}
}
}
Nowadays the best way to validate HTML forms IMO is with a jQuery plugin like this one.
On client side, on the click of button, use ClientClick property, and set it to javascript function name to validate the input
e.g.
<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" ></asp:Button>
And write a validate() function in javascript, which check the sum of both inputs

Resources