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.
Related
Using ASP.NET 4.0
Bit of a strange one here, my code works but I don't know why!
So I have some HTML like so:
<asp:Repeater runat="server" ID="uxMyRepeater" ClientIDMode="Predictable">
<ItemTemplate>
<asp:Button runat="server" Text="Submit" />
<asp:HiddenField runat="server" ID="uxIsVisibleHiddenField" Value="0" />
</ItemTemplate>
</asp:Repeater>
And the back end:
Protected Sub uxMyRepeater_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles uxMyRepeater.ItemCommand
uxIsVisibleHiddenField.Value = "1"
End Sub
So for some reason this works, usually I would expect to have to declare uxIsVisibleHiddenField in uxMyRepeater_ItemCommand like so:
Dim uxIsVisibleHiddenField As HiddenField = DirectCast(e.Item.FindControl("uxIsVisibleHiddenField"), HiddenField)
But in this particular case it works without the declarative statement. Can anyone shed any light on why it would do this?
Please note this is sample code only, not my actual code.
EDIT
Forgot to mention there is an UpdatePanel around each RepeaterItem, removing this causes Visual Studio to give me an error that'd I'd expect: 'uxIsVisibleHiddenField' is not declared. It may be inaccessible due to its protection level.
This could only happen if you have a control with the same ID that sits outside of the repeater. You won't have ID clashes because the repeater is a naming container.
Do you have any AlternatingItemTemplate ? It might be declared in that particular area and remained unnoticed.
After a lot of debugging the only thing I can say is that when I have an UpdatePanel inside the Repeaters ItemTemplate I don't need to declare the controls inside the ItemTemplate when accessing them in the DataBind event, very strange. Taking out the UpdatePanel causes complier errors so the UpdatePanel must be doing some auto hook-up between the Repeater and the controls.
Thanks for all your suggestions.
I'm trying to learn about caching, and in particular, partial caching using controls.
My website is running slow on certain pages, so caching as much as possible will be helpful.
Having run a number of experiments from code I have found on SO and various other Google results, I am running into an issue with dynamically added controls.
I have set up a simple page, containing this code:
<%# Page Language="VB" Debug="true" %>
<%# Register TagPrefix="controls" TagName="control" Src="~/test/control.ascx" %>
<script runat="server">
Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Label2.Text = "Present Time: "
Label2.Text += DateTime.Now.ToString()
End Sub
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">Output Caching</h2>
<asp:Label ID="Label2" runat="server"></asp:Label>
<controls:control ID='control1' runat='server' />
'------------------------------------------
<hr />
<div id='dyn2' runat='server' />
</div>
</form>
</body>
</html>
The control control.ascx looks like this:
<%# Control Language="VB" ClassName="control" %>
<%# OutputCache Duration="60" VaryByParam="r" %>
<script runat="server">
Sub Page_Load() Handles Me.Load
controlContent.InnerHtml = "Control time: " & DateTime.Now.ToString()
End Sub
</script>
<div id="controlContent" runat="server"></div>
This works well, and gives me a "live" time in the page, whilst the cached control shows me a time which is only updated after 60 seconds has passed, as per the OutputCache declaration.
I can see how I can use this for any application when I need to cache a part of a page and that part is explicitly entered into the page with a <controls> tag. The varyByParam option is useful to me too. (I've yet to investigate varyByCustom!)
However, in some cases I am loading a control into a page, programmatically based on specific needs.
In this case, I use code like this:
Dim theResult As test_control2 = CType(LoadControl("~\test\control2.ascx"), test_control2)
dyn2.Controls.Add(theResult)
This is programmatically adding my second test control, imaginatively entitled control2.ascx into the div with id "dyn2".
With no cache directive header in the control, or it's code-behind,l everything works fine, but I can't cache it (unless I cache the entire page).
However, if I add the cache header as per the control code above, I get this error:
Unable to cast object of type 'System.Web.UI.PartialCachingControl' to type 'test_control2'.
Googling doesn't seem to help me much with this, and investigating the PartialCachingControl types has lead me into further problems!
Can someone tell me what should be doing to enable me to cache these controls?
If it matters, I am coding in VB.net and also using .NET 2.0, so any advice on limitations on this platform would be appreciated too, if applicable.
Ah ha! Finally found another question on SO that helped
How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties
Basically, I was using the wrong Type when loading the control changin:
Dim theResult As test_control2 = CType(LoadControl("~\test\control2.ascx"), test_control2)
dyn2.Controls.Add(theResult)
to
Dim theResult As PartialCachingControl = DirectCast(LoadControl("~\test\control2.ascx"), PartialCachingControl)
dyn2.Controls.Add(theResult)
Sorted it!
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.
I have a gridview control on my asp.net page(vb.net). I also have a "cancel" button, that when pressed, is supposed to clear the gridview of it's current contents.
However whenever the cancel button is pressed, it just reloads the page and the gridview is still there with the same data that I wanted clear.
Based on suggestions that I found on stackoverflow, I set the datasource to nothing, but that is not working.
Here is my code for the cancel button:
Private Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
gvQuizReport.DataSource = Nothing
gvQuizReport.DataBind()
End Sub
Any suggestions would be welcome!
Thanks
You might try:
gvQuizReport.Columns.Clear()
though as #Leniel Macaferi said, hiding the gridview is a possible solution as well.
since you have shown some interest in updatepanels, here is some starter code in case you are unfamiliar:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
If you already have a scriptmanager on your page you don't need to add another (you will get an error). if you have any problems implementing the updatepanel, feel free to post another question, there are plenty of people to help you with it
The code you showed should "clear" the results, but if you do not want anything displayed, you would hide the gridview by using gvQuizReport.Visible = False;
If you really want to clear without reloading the page, you could just use client side script to hide the grid object.
jquery hide
I have an ASP.Net web site and on one of the pages I'm using a repeater to render several iterations of a UserControl, that UserControl contains a second UserContol that has two text boxes that my User must enter information into. I want to be able to have my user push a button and add another instance of the second UserControl (with the two textboxes) to my original UserControl, so that the user has 4 textboxes displayed on the screen for the first UserControl. The problem I am seeing if I try to add the second UserControl to a given iteration of the first, is that the page postback causes any other of these second user controls to be deleted from the page.
Does anyone know of a way to do this using JQuery? I've had three posts that describe how to solve this problem using server side dynamic controls, and/or AJAX, but we've decided to focus on a JQuery solution because this server side mechanism is too costly in terms of resources for us.
I've been working on the suggestion by Zincorp below, and now have the JQuery working to clone a textbox, but having trouble using the server side Request.Form collection to iterate over the controls. Can anyone give adivce on how to iterate over the Request.Form collection?
OK, I think the problem with iterating over the controls using the Request.Form.AllKeys collection turned out to be that I was using an HTML Textbox, rather than an ASP TextBox Control. Apparently the Request.Forms.AllKeys collection only contains ASP controls, not HTML controls.
The problem I am seeing now is that when I clone the control in JQuery, and then submit my page with the submit button, the 2 controls have the same ID, and so are combined (I think) by http into one ASP TextBox Controls containing both values, with a comma delimiter (e.g.- 40,20). Anyone know how to get a new ID assigned to the cloned ASP TextBox?
Here is the updated markup in a sample ASP.Net web appliction:
<%# 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="ProjectDisplay" Text="Project" />
<asp:TextBox ID="ProjectValue" runat="server" ></asp:TextBox>
<div id="mydiv" ></div>
<br />
<br />
<br />
<input id="AddProject" type="button" value="Add Project" />
<br />
<br />
<asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
</div>
</form>
</body>
</html>
<script language="jquery" src="js/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" >
$(document).ready(function() {
$("#AddProject").click(function() {
var x = $("#ProjectValue").clone();
x.appendTo("#mydiv");
});
});
</script>
And here is the updated server side code where I'm trying to iterated over items in the Request.Form collection to get information from it:
public partial class _Default : System.Web.UI.Page
{
protected void Submit_Click(object sender, EventArgs e)
{
foreach (string s in Request.Form.Keys)
{
object x = Request.Form[s];
}
}
}
Before choosing a solution for the problem, consider the problem first:
You effectively need to:
1) Duplicate controls/markup on the client-side
2) Obtain these values on the server-side on a postback
3) For each of the added "user controls" on the client-side, add them as children of the first user control on the server side.
I'm not going to give the code, but here are some ideas:
1) Use jQuery's .clone() method (http://api.jquery.com/clone/) to duplicate the markup being rendered by the usercontrol containing the textboxes. Perhaps wrap them in a div without runat="server" to so that you can easily obtain it by ID. You'll probably need to then recursively iterate through the children in the cloned element and append a # to them to avoid any conflicting identifiers. (eg. nameTextBox_1, nameTextBox_2, etc.)
2) On a postback, use the Request.Form collection on the server side to obtain the textbox values. Iterate through it and snag all of the items whose keys start with "nameTextBox_".
3) For each added "user control" on the client side, create the actual user control on the server side, assign to it the values entered in the textboxes, and then add it to the child controls of the first one. This way the state is maintained upon returning to the user.
The short answer is that you would have to add the controls before the ViewState is initialized.
The video on this site has a nice guide on adding dynamic controls. http://www.asp.net/%28S%28wnmvzu45umnszm455c1qs522%29%29/learn/ajax-videos/video-286.aspx
This is probably not a ViewState issue. That may be the first place to look, but after that you need to make sure that your dynamic controls are actually being CREATED on each load.
Generally speaking, the ViewState is only responsible for restoring STATE to existing controls (hence the name). It is not responsible for recreating controls.
I believe you are experiencing a viewstate problem. Your dynamic controls are not being persisted. If you haven't already, read Dave Reed's excellent article Truly Understanding Viewstate. Pay particular attention to the section "5. Initializing dynamically created controls programmatically" which applies to the trouble you are experiencing.