ASPX EventValidation error with DropDownList - asp.net

This must have an obvious solution, but I'm stumped. We are developing an application which is mostly XHR-based, so while we are using .aspx, very little is done with typical controls. However, in a few spots, we are just doing basic "throw this data into a spreadsheet for the user" things with a couple dropdowns for timespan for reports, etc.
The problem is when we use asp:DropDownList controls, it immediately causes any page we put them in to throw Event Validation errors on submit. I have created test pages that do not share the rest of the application's master pages (aka, no JS at all modifying things client side) just to be sure that we don't have some stray JS causing issues.
If I remove the DropDownList in the following example, the button click happens just fine. If I click the button with the page sitting as shown, it throws the Event Validation error.
However, other applications running on the same machine, in 4.0 Integrated app pools, do not exhibit this behavior, so I'm assuming it has something to do with the configuration. The web.config is pretty standard...tried turning httpCompression section off in a desperate attempt, but to no avail.
Does anyone have a suggestion on where to start here? Please remember... There is NO CLIENT SIDE MODIFICATION going on. This is straight from the server to the browser, then 'click' on the ASP-generated button.
Turning off Event Validation in the page directive does eliminate the error, but I'd rather not turn off validation if I can help it.
Environment:
Windows 7 Pro
IIS 7.5
.NET 4.0 Integrated app-pool
Error happens in IE9/Chrome/Firefox/Safari
Page:
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlDays" runat="server">
<asp:ListItem Text="30 Days" Value="30"></asp:ListItem>
<asp:ListItem Text="60 Days" Value="60"></asp:ListItem>
<asp:ListItem Text="90 Days" Value="90"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="butExport" Text="Export" runat="server" />
</div>
</form>
</body>
</html>
Codebehind:
protected void Page_Load(object sender, EventArgs e) {
butExport.Click += new EventHandler(butExport_Click);
}
void butExport_Click(object sender, EventArgs e) {
Syslog("clicked");
}
Form Data (according to Chrome Inspector):
__VIEWSTATE:/wEPDwULLTIwOTUzNjUzOTVkZIiv1cdholWibyWL8h5HASwxedB47NUpctCv8OQc1CWM
__EVENTVALIDATION:/wEWAgL0voCyDQKDgcL6CAX34hdaRiHyNiY1xLIh5Pr6aj5q8h8gGG875vMq1SXF
ddlDays:30
butExport:Export

OK, I started going through my project looking for any possible configuration item that could be causing issues. Turns out a co-worker used a WebControlAdapter and applied it to all DropDownLists and during the Render, did not use RegisterForEventValidation.
I don't particularly like the adapter, but in the interest of moving on with life, I left it there and while rendering bound items, I'm calling Page.ClientScript.RegisterForEventValidation for each value. This has fixed every problematic DropDownList we have in the application.
Thanks for the suggestions, all.

Related

Hiding Literal control declaratively in header (ASP.NET web forms)

I try to hide some Javascript code in header by just using declarations. The Visible property should be controlled by a setting in web.config (ConfigurationManager.AppSettings["key"]).
However, for some reason setting the Visible property like this has no effect - it is always true:
<head id="Head1" runat="server">
<asp:Literal ID="JSLiteral" runat="server" Mode="PassThrough" Visible='<%# false %>'>
Some JS
</asp:Literal>
</head>
It is probably linked to page life cycle in ASP.NET. It works when setting this property in Page_Load event, but that's not what I want as I need to insert this block to a lot of web applications, and I don't want to recompile all of them.
Any ideas how this could be accomplished with minimal effort in ASP.NET? I don't want to mess around with the JS code, which needs to be added in header - it is code of a third-party.
Thanks in advance and cheers,
Roger
Try the following.
protected void Page_Load(object sender, EventArgs e)
{
Head1.DataBind();
}
What you are using is a DataBinding expression. So DataBind() has to be called.

ASP.NET Button click event not working in IE11

I have a code which i have put in and that does not seem to work in IE11. Can anyone please help me out ?
I am working in VS2005 and click event is not firing in localhost.I have tried the following fixes; hotfix from microsoft(microsoft update standalone package) and installed .Net framework 4.5 Even then button event is not firing.
Below is the code which I have put in - The aspx and the aspx.cs lines are mentioned below :
aspx
<asp:Button ID="btnExcelExport" runat="server" Text="Excel" OnClick="btnExcelExport_Click" />
aspx.cs:
protected void btnExcelExport_Click(object sender, System.EventArgs e)
{
Label1.Text = "You clicked a button.";
}
Depending on the version of your .NET framework, this is actually a bug. There's a patch available from Microsoft.
http://blogs.telerik.com/aspnet-ajax/posts/13-12-19/how-to-get-your-asp.net-application-working-in-ie11
Is your tag wrapped in a:
<form id="form1" runat="server" enableviewstate="false">
<!-- postbacks and other .NET client-based functionality require a
wrapping form tag -->
</form>

Disable page refresh after button click ASP.NET

I have an asp button that looks like this:
Default.aspx
<asp:button id="button1" runat="server" Text="clickme" onclick="function" />
Default.aspx.cs
protected void function(object sender EventArgs e)
{
// Do some calculation
}
However, whenever I press the button, the entire page gets refreshed. I have looked on this site and found many solutions, but none of them really works for my project. Here are some of the suggested solutions:
set onclick="return false;" // but then how do I run the function in the code-behind?
use !IsPostBack in Page_Load // but the page still refreshes completely. I don't want Page_Load to be called at all.
Disable AutoEventWireup. // making this true or false doesn't make a difference.
Does anyone have a solution to this, or is it really impossible?
I would place the control inside of an Update panel.
To do so, you would also need a script manager above it, so something like this:
<asp:ScriptManager runat="server" ID="sm">
</asp:ScriptManager>
<asp:updatepanel runat="server">
<ContentTemplate>
<asp:button id="button1" runat="server" Text="clickme" onclick="function" />
</ContentTemplate>
</asp:updatepanel>
if a control inside the update panel does a postback, it will only reload the part of the page inside of the upate panel.Here is a link you may find useful from the MSDN site.
I think there is a fundamental misunderstanding here of how ASP.Net works.
When a user first requests your page, an instance of your Page class is created. The ASP.Net framework runs through the page lifecycle with this page instance in order to generate html. The html response is then sent to the user's browser. When the browser receives the response it renders the page for the user. Here's the key: by the time rendering is complete, your page class instance was probably already collected by the .Net garbage collector. It's gone, never to be seen again.
From here on out, everything your page does that needs to run on the server, including your method, is the result of an entirely new http request from the browser. The user clicks the button, a new http request is posted to the web server, the entire page lifecycle runs again, from beginning to end, with a brand new instance of the page class, and an entirely new response is sent to the browser to be re-rendered from scratch. That's how ASP.Net (and pretty much any other web-based technology) works at its core.
Fortunately, there are some things you can do to get around this. One option is to put most of your current Page_Load code into an if (!IsPostBack) { } block. Another option is to set up your method with the [WebMethod] attribute and make an ajax request to the method from the button. Other options include calling web services from custom javascript and ASP.Net UpdatePanel controls.
What works best will depend on what other things are on the page that user might have changed.
That is normal behavior for asp.net, you are actually causing a postback of the the page in order for the associated event to be called on the server.
I would suggest working with update panels but if you just need something to happen on the backend without it causing any major change on the web page, I would use jquery and a web service. The main reason for me is that update panels create huge viewstate objects.
Have a look here for asp.net ajax : http://www.asp.net/ajax
And here for an example of jquery and wcf : http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery
I have a similar problem. this answer helps me a lot. in your asp button.<asp:button id="button1" runat="server" Text="clickme" OnClientClick="return SomeMethod();" /> and in the SomeMethod which is a js method do your logic like manipulating your page then return false as the mentioned answer suggest.

ReportViewer 10, ASPX, Paging Issues

I have a small web project, the sole purpose of this project is to house and produce reports for a larger application.
I have a page "ReportManager.aspx" that simply has a ReportViewer (10.0) control on it.
<div style="width:auto;">
<form id="Form1" runat="server" style="width:100%; height: 100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:reportviewer id="ReportViewer1" runat="server" Width="100%" Height="100%" AsyncRendering="false" SizeToReportContent="true"></rsweb:reportviewer>
</form>
</div>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
return;
ReportViewer1.ProcessingMode = ProcessingMode.Remote;
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer");
ReportViewer1.ServerReport.ReportPath = "/SSRSReport";
ReportParameter param = new ReportParameter("effective_date", "4/22/2013");
ReportViewer1.ServerReport.SetParameters(param);
ReportViewer1.PageCountMode = PageCountMode.Actual;
}
</script>
Changing pages on the reportviewer control at runtime is producing an error.
The error is in:
Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=ViewerScript
at Line 3559, and looks like this:
// Remove the current STYLE element, if it already exists.
if (oldStyleElement != null)
headElement.removeChild(oldStyleElement);
headElement.removeChild(oldStyleElement); is the line where the error is showing htmlfile: Invalid argument. Break | Continue | Ignore.
Clicking "Continue" causes the report to render without any styling. Clicking "ignore" allows the report to render correctly.
I am running Windows 2012 Server, Visual Studio 2012 Premium and IE8. The same error occurs on Win 7 machines.
Any suggestions or known fixes?
My report is in an MVC app also. I actually fix this problem awhile ago. I think I fixed it by removing the following lines:
ReportViewer1.ProcessingMode = ProcessingMode.Remote;
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer");
ReportViewer1.ServerReport.ReportPath = "/SSRSReport";
ReportViewer1.PageCountMode = PageCountMode.Actual;
I changed the code to store all that data in the tag.
Another thing I did was add
ReportViewer1.ServerReport.Refresh() below ReportViewer1.ServerReport.SetParameters(param).
After I made those changes, everything started working.
Though suspicious it would help, I tried removing as much code-behind as possible like you suggested, but this did not fix it for me.
However, this was the solution in our app:
The .aspx page hosting the ReportViewer control was missing its <body> tag.
THAT'S ALL!!
Anti-climactic, I know, but apparently this causes IE8 (at least) to barf on
headElement.removeChild(oldStyleElement);
I'm curious if doing the same in your app brings the error back.
There are only a handful of resources I found via Google dealing with this problem, I can't believe this wasn't mentioned in any of them :-x

asp:linkbutton not functioning in production

I've got an asp:linkbutton as follows:
<asp:LinkButton ID="lb_new" runat="server" ForeColor="White">New Item</asp:LinkButton>
Protected Sub lb_new_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb_new.Click
ViewState("ItemID") = "0"
Dim myURL As String
myURL = String.Format("D002.aspx?e={0}&i={1}", ViewState("EventID"), ViewState("ItemID"))
Response.Redirect(myURL)
End Sub
Up until recently, it has functioned as it should. But for some reason, it has stopped working in production. As far as I can tell, it's not connecting to it's code-behind at all (I tried modifying it to simply change the text in one of the text boxes ont he page, and it fails that as well). Still works if I run the website through visual studio. But as soon I publish to our production server, it no longer works.
I'm stumped -- and still fiddling with it.
If anyone has experienced this, please share. Have been on this for a couple hours now, and am out of ideas.
Thank you!
UPDATE
The event handler has been suggested as missing by a couple of folks. This is actually handled in the code-behind by the 'Handles' clause (...Handles lb_new.Click).
Manually deleted the items in the production folder, then re-published. No joy.
Verified the files in the production folder are the new ones.
I created a brand new linkbutton -- it fails to connect to it's code-behind as well
I added an Onclick= to the mark-up. This shouldn't be necessary, considering the Handles clause in the code-behind. Regardless, the click still fails.
...still plugging away at it
UPDATE2
Removed the required field validators on the page, and it works. This does not make sense to me, because I had other controls on the page causing postbacks, and they still worked the whole time. Also, I had the fields that were being validated filled-in, so no reason (I can think of) that the validators would have been preventing the postback.
Now I just have to figure out how to do validation on the page without the required field validators.
...confused... :-)
Check the __EVENTTARGET and __EVENTARGUMENT variables on the post; these should match the values from the button that triggered the postback. That's at least the first clue...
Did you upgrade some third party DLL or upgrade from .NET 3.5 to .NET 4.0 or something like tha too?
HTH.
I was having this same problem and this thread pointed me to the answer (for me at least!). Just set the CausesValidation property of the linkbutton to false and the click event will fire ignoring the state of any validators on the page. I'm not doing anything in the click event or postback that requires any validation so it's fine for me to ignore it. If the same is true for you, this could well be your solution.
I think you need to define its "Click" event.
<asp:LinkButton ID="lb_new" runat="server" ForeColor="White" OnClick="lb_new_Click">New Item</asp:LinkButton>
(Edited)
For VB.NET: (Example From MSDN:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclick(v=VS.90).aspx)
<%# Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>LinkButton Example</title>
<script language="VB" runat="server">
Sub LinkButton_Click(sender As Object, e As EventArgs)
Label1.Text = "You clicked the link button"
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
Validation controls were preventing a postback -- or at least, removing those controls seems to have solved the problem This does not make sense to me, because I had other controls on the page causing postbacks, and they still worked the whole time. Also, I had the fields that were being validated filled-in, so no reason (I can think of) that the validators would have been preventing the postback. Anyway, thanks to everyone for all the ideas.

Resources