I have an ASP.Net 4.5.1 web application that has a page with a GridView and a DetailsView. The two controls are linked, so that when a Select button is clicked in the GridView, the DetailsView displays the data for that row. The DetailsView record has an Edit and a Delete button. This all works fine. However, I don't want to add a New button at the bottom of the DetailsView along with the Edit and Delete buttons. I don't see any reason why my user should have to select an existing record before entering a new record into the dataset.
So - I've created an ASP button at the top of the page called btnNew. What this button is supposed to do is open the DetailsView and change its mode to Insert. I have two buttons in the DetailsView FooterTemplate, btnInsert and btnCancel. They are not visible by default, but I make them visible when btnNew is clicked.
Here's the problem: when I click on btnNew the first time, the DetailsView opens in Insert mode and btnInsert and btnCancel display properly. HOWEVER, if I click on btnCancel (the DetailsView and two buttons disappear, all good) and then click on btnNew again, the DetailsView appears, but btnInsert and btnCancel don't. If I click btnNew a second time, they show up. If I move the line in btnNew_Click (see below) that changes the DetailsView mode to Insert to the bottom of the method, then when I click on btnNew the first time, I don't see btnInsert and btnCancel, but when I click on it a second time, they show up. When I walk through the code in Debug mode, btnInsert and btnCancel are found, and their Visible property is set to True, but they aren't displayed.
I've simplified my code down to a page with just a DetailsView (no GridView) with no databinding, with no Master Page, with no Ajax controls, but the problem behavior persists. Below is this simplified code. First my markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleDefault.aspx.cs" Inherits="ElectronicCaseFilingHistory.SimpleDefault" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnNew" Text="Add New" OnClick="btnNew_Click" BackColor="#F8F8F8" BorderColor="#9BE8E8" ForeColor="#3C3C3C" />
<asp:DetailsView runat="server" ID="dvFilingDetail" AutoGenerateRows="false" >
<Fields>
<asp:TemplateField HeaderText="Attorney" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label runat="server" ID="lbAttorneyName" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbAttorneyName" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Court" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label runat="server" ID="lbCourt" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbCourt" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<FooterTemplate>
<asp:Button runat="server" ID="btnInsert" Visible="false" Text="Insert" />
<asp:Button runat="server" ID="btnCancel" Visible="false" OnClick="btnCancel_Click" Text="Cancel" />
</FooterTemplate>
</asp:DetailsView>
</div></form></body></html>
Now the code behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ElectronicCaseFilingHistory
{
public partial class SimpleDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNew_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.Insert);
Button insert = (Button)dvFilingDetail.FindControl("btnInsert");
Button cancel = (Button)dvFilingDetail.FindControl("btnCancel");
if (insert != null)
insert.Visible = true;
if (cancel != null)
cancel.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.ReadOnly);
dvFilingDetail.DataBind();
}
}
}
I've tried adding an InsertItemTemplate in addition to an EditItemTemplate, but that has no effect. I also added CommandName=New to btnNew and added an ItemCommand event to the DetailsView to try to do it that way, but the ItemCommand method was never hit.
What am I doing wrong?
After changing DetailsViewMode to Insert you have to call DataBind(); as well.
protected void btnNew_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.Insert);
dvFilingDetail.DataBind();
Button insert = (Button)dvFilingDetail.FindControl("btnInsert");
Button cancel = (Button)dvFilingDetail.FindControl("btnCancel");
if (insert != null)
{
insert.Visible = true;
}
if (cancel != null)
{
cancel.Visible = true;
}
}
Related
I've not found answers that matched my circumstances, so I'm posting an answered question hoping it will help others.
I was getting the error
Invalid postback or callback argument. Event validation is enabled
using in configuration or <%#
Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback
events originate from the server control that originally rendered
them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
at System.Web.UI.WebControls.HiddenField.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.WebControls.HiddenField.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I've got a Databound ListView (with a few 100s rows), with buttons on each row. The buttons bring a popup. The popup has dropdownlists and other controls doing asynchronous postbacks. I need to make sure I do asynchronous postbacks to avoid refreshing my big table.
I get the error when I click the button on one row, then change a dropdownlist inside the popup which fires a postback (selected item changed). Boom.
Here's the markup for a reduced sample, without popup and javascript at all! It still exhibits the problem. Click twice on a button in a row to get the error.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestPopupAsynchPostback.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptMgr" runat="server" ScriptMode="Debug" EnablePartialRendering="true"
EnableScriptGlobalization="true" EnableScriptLocalization="true" EnablePageMethods="true"/>
<asp:ObjectDataSource ID="ListDataSource" runat="server" SelectMethod="List" TypeName="TestPopupAsynchPostback.Default" />
<asp:Label runat="server" ID="PageLabel"></asp:Label>
<asp:ListView ID="EL" runat="server" DataSourceID="ListDataSource" OnItemDataBound="EntityList_OnItemDataBound">
<LayoutTemplate>
<table border="1">
<tr id="itemPlaceholder" runat="server" enableviewstate="false">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" id="DefaultRow" enableviewstate="false">
<td>
<asp:Label ID="Lbl" runat="server" EnableViewState="false" />
</td>
<td>
<button runat="server" type="button" id="ReportingButton" enableviewstate="false" onserverclick="ReportingButton_OnClick" causesvalidation="false">click</button>
</td>
</tr>
<%-- Fix part 1: Change SpecialRow visible = true--%>
<tr runat="server" id="SpecialRow" visible="false" enableviewstate="false">
<td>
<asp:Label ID="Lbl2" runat="server" EnableViewState="false" />
<asp:HiddenField runat="server" ID="fn_hid" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</form>
</body>
</html>
Here's the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace TestPopupAsynchPostback
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
PageLabel.Text = DateTime.UtcNow.ToString() + " IsPostBack:" + IsPostBack + " IsInAsyncPostBack:" + (sm == null ? "" : sm.IsInAsyncPostBack.ToString());
}
protected override void Render(HtmlTextWriter writer)
{
ScriptManager sm = ScriptManager.GetCurrent(this.Page);
if (sm != null)
{
foreach (ListViewDataItem row in EL.Items)
{
HtmlButton reportingButton = row.FindControl("ReportingButton") as HtmlButton;
if (reportingButton != null)
sm.RegisterAsyncPostBackControl(reportingButton);
}
}
base.Render(writer);
}
public IList<string> List()
{
return (new string[] { "ONE", "TWO"}).ToList();
}
protected void ReportingButton_OnClick(object sender, EventArgs e)
{
//Do something useful here, for now, just a postback event
}
protected void EntityList_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
Label lbl = e.Item.FindControl("Lbl") as Label;
Label lbl2 = e.Item.FindControl("Lbl2") as Label;
lbl.Text = lbl2.Text = e.Item.DataItem.ToString();
HtmlTableRow specialRow = e.Item.FindControl("SpecialRow") as HtmlTableRow;
if (e.Item.DataItemIndex%2 == 0)
{
HiddenField fn_hid = e.Item.FindControl("fn_hid") as HiddenField;
fn_hid.Value = "test1";
specialRow.Visible = true;
}
//Fix part 2: set SpecialRow Visible = false in code behind
//else
// specialRow.Visible = false;
}
}
}
I initially thought my javascript was at fault as I am modifying things with it. However, the process of creating a sample page has helped me find the problem.
It turns out it has nothing to do with the javascript or the popup. It's to do with a HiddenField contained inside a TR HtmlControl (asp.net server side control) with Visible=false IN THE MARKUP. I then use the code behind to set Visible=true when I need to in OnItemDataBound event.
This is what is causing the error for me: It seems that because the container (the TR SpecialRow) is Visible=false, I guess something is not rendered. I then come along in OnItemDataBound, decide that this row must be shown, and set Visible=true and set the value of my HiddenField. Boom.
If I remove the markup for the hidden field and don't set its value, no crash.
So it's not just the visibility of the TR on its own, it's the HiddenField too.
My fix: don't set Visible=false in the markup, and change OnItemDataBound to set Visible=false when required.
In other words: I was defaulting to hide things in markup and use code behind to show them. I changed this around and show things by default in markup and hide them using the code behind.
I've added the fix in the markup and code above.
I have a page with a listbox and a user control placed inside an update panel like this:
<%# Register TagPrefix="uc1" TagName="CMessage" Src="~/Controls/CMessage.ascx" %>
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ToolkitScriptManager1" EnableScriptGlobalization="true" EnableScriptLocalization="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lbox" runat="server"></asp:ListBox>
<asp:Button ID="btnDelete" OnClick="btnDelete_Click" runat="server" Text="Delete selected item" Enabled="True"></asp:Button>
<uc1:CMessage ID="CMessage1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
The page codebehind is like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbox.Items.Add("test1");
lbox.Items.Add("test2");
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
CMessage.MessageConfirm("Delete item?", "Yes", "No", DeleteItem);
}
protected void DeleteItem()
{
lbox.Items.Remove(lbox.SelectedItem);
CMessage.Message("Item deleted succesfully!");
}
The user control is like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CMessage.ascx.cs" Inherits="Controles.CMessage" %>
<table id="tableMessage" runat="server" style="display: none" class="modalPopup">
<tr>
<td>
<asp:Label ID="lb5" runat="server" Text="Message"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnOk" runat="server" Text="Ok" OnClick="btnOk_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
</td>
</tr>
</table>
<asp:Button ID="btnOKError" runat="server" Text="OK" style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="ModalPopupMessage" runat="server" TargetControlID="btnOKError" PopupControlID="tableMessage" OkControlID="btnOKError" CancelControlID="btnOKError"></ajaxToolkit:ModalPopupExtender>
The usercontrol codebehind is like this:
public partial class CMensaje : UserControl
{
public delegate void FunctionButtonPressed();
private FunctionButtonPressed FunctionButtonPressedOk
{
get { return (FunctionButtonPressed)Session["FunctionButtonPressedOk"]; }
set { Session["FunctionButtonPressedOk"]= value; }
}
protected void btnOk_Click(object sender, EventArgs e)
{
ModalPopupMenssage.Hide();
if (FunctionButtonPressedOk!= null)
{
FunctionButtonPressedOk();
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ModalPopupMessage.Hide();
}
public void Mensaje(string message)
{
lbMessage.Text = message;
FunctionButtonPressedOk= null;
btnCancel.Visible = false;
ModalPopupMessage.Show();
}
public void MessageConfirm(string message, FunctionButtonPressed FunButtonOkx)
{
lbMessage.Text = message;
FunctionButtonPressedOk= FunBotonAceptarx;
btnCancel.Visible = true;
ModalPopupMensaje.Show();
}
}
Everything works, the popup is shown, the function to call from the page is passed to the usercontrol to trigger it if the user presses ok and triggers correctly etc. But in that last function DeleteItem() the changes done to the page (in this example the item removed from the listbox and the notification message launched) doesnt work.
Its like the updatepanel doesnt refresh. I cant call manually UpdatePanel1.Update() because i have the updatemode as always. In theory in this part of the pagecycle it should refresh the pages with the changes...
I tried adding in the user control pageload the OK button as a PostBackTrigger of the UpdatePanel with no avail and as an AsyncPostBackTrigger.
Keep in mind that this is an slimmed down version of the page and the user control so people can focus on the problem at hand so if you need any more details ask me...
The code flow is like this:
User clicks in the delete item button and triggers the
btnDelete_Click() in the page (first postback)
btnDelete_Click() calls the usercontrol CMessage.MessageConfirm passing DeleteItem as
argument
At the usercontrol MessageConfirm() shows the modalpopup and saves
the argument function DeleteItem() in order to call it later(end of
first postback)
After displaying the modalpopup the user clicks its Ok button and
triggers the btnOk_Click() in the usercontrol (second postback)
At the usercontrol btnOk_Click() calls the DeleteItem() function
that was saved previously
At the page DeleteItem() removes the item from the ListBox (lets say
i dont call the second message to simplify, this would be the end of
the second postback, and the update panel hasnt refreshed :S)
I think i have isolated the problem, moved all the controls and functions from the usercontrol to the page, the problem persisted, but if i called the DeleteItem() function directly instead of his delegate it worked (keep in mind that in both cases the breakpoint at DeleteItem() triggered so the function code was executing correctly):
protected void btnOk_Click(object sender, EventArgs e)
{
ModalPopupMenssage.Hide();
/*if (FunctionButtonPressedOk!= null)
{
FunctionButtonPressedOk();
}*/
DeleteItem(); //page updates correctly! why?
}
Is there some incompatibility with delegates?
In the end i solved it changing the following lines:
if (FunctionButtonPressedOk!= null)
{
//FunctionButtonPressedOk();
Page.GetType().InvokeMember(FunctionButtonPressedOk.Method.Name, BindingFlags.InvokeMethod, null, Page, new []{sender, e});
}
Why it works this way?Whats the difference internally?
I've an asp repeater which has some fields inside an ItemTemplate. Each item in the repeater has an "add to cart" asp:ImageButton and an invisible asp:Label as well. The code looks like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="addToCart">
<HeaderTemplate>
<table id="displayTable">
</HeaderTemplate>
<ItemTemplate>
<td>
<!-- fields like name, description etc in the repeater are present; i've omitted to show them here-->
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
</td>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
When a particular ImageButton in the repeater is clicked, I'm trying to display "added to cart" as the text of its corresponding Label, and make the clicked ImageButton Visible=false. I've tried using the OnItemCommand function for the ASP:Repeater. The method is "addToCart":
<>
void addToCart(Object Sender, RepeaterCommandEventArgs e)
{
Cart cart = new Cart();
cart.instrument_id = //id of product from repeater based on user click
String userName = Membership.GetUser().ToString();
cart.user_name = userName;
cart.quantity = 1;
var thisLbl = (Label)e.Item.FindControl("addedToCartLabel");
var thisImg = (ImageButton)e.Item.FindControl("addToCartImg");
try
{
database.Carts.InsertOnSubmit(cart);
database.SubmitChanges();
thisImg.Visible = false;
thisLbl.Text = "Added to Cart!";
thisLbl.Visible = true;
}
catch (Exception ex)
{
thisImg.Visible = false;
thisLbl.Text = "Processing failed;please try again later";
thisLbl.Visible = true; ;
}
}
The aspx page is populated properly. However, when I click on any of the ImageButtons in the repeater, I get the following error:
Server Error in '/mysite' Application.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback
or callback data for validation.
Can someone help me with this?
May I suggest doing this with client-side javascript rather than a server-side call?
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
becomes
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" onclick="javascript:function() { this.this.style.display='none'; document.getElementById(this.parentNode.firstChild.id).style.display='block'; }" ImageUrl="hi.jpg" Width="75px" Height="50px" />
I am firing an event from an ascx control in order to update some controls in the container to which the ascx control belongs.
The ascx control is displayed via a modal popup extender. When I click a button inside the ascx, I fire an event to which the container containing the ascx control is subscribes.
The event delegate is fired and the expected logic is run in the container's code behind, the problem is that any changes made to controls inside not the container aren't updated despite the event logic having been processed. The expected changes are not reflected on the page when the results of the postback is rendered.
Are there any pitfalls I should know of?
The markup for the container
<asp:Panel ID="panelTreeViewAttributesTitle" runat="server">
<asp:Label ID="someLabel" runat="server" Text="Hello Governor" />
<asp:LinkButton ID="LinkButtonEdit" runat="server" Text="(Edit)" />
<ajax:ModalPopupExtender BackgroundCssClass="modalBackground" Enabled="True"
ID="btnEdit_ModalPopupExtender" PopupControlID="modalPanel" runat="server"
TargetControlID="LinkButtonEdit" />
</asp:Panel>
<asp:Panel ID="modalPanel" runat="server" CssClass="modalPopUp" Style="display: none">
<xxx:customControl runat="server" ID="myCustomControl" />
</asp:Panel>
The code behind for the container
protected void Page_Load(object sender, EventArgs e)
{
myCustomControl.Updated += eventCaptured;
if (IsPostBack) return;
...
}
void eventCaptured(object sender, EventArgs e)
{
someLabel.Text = "Goodbye Governor";
}
The custom control
<script type="text/javascript">
function Update() {
var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
if (ajaxManager != null)
ajaxManager.ajaxRequest("");
}
</script>
<asp:Panel ID="panelEditPanel" runat="server">
<asp:Label ID="lblCMA" runat="server" Text="Call me Arnooold." />
</asp:Panel>
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClientClick="Update()" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
The custom control's code behind
public event EventHandler Updated;
protected void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
//Some DB backend logic
UpdateFinished();
}
private void UpdateFinished()
{
if (Updated == null) return;
Updated(this, null);
}
Maybe the button click is causing a partial post back instead of a normal, full-page post back. If this is the case, the entire life cycle would run on the server side (including your event handler) but only part of the HTML would be updated on the client side when the response comes back to the browser. "someLabel" could be outside the region that gets updated on the client side.
Ok, so I'm struggling with using asp:formview.
I've got the formview up and running and I've added the 'Edit' button.
<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging" >
<ItemTemplate>
// (..) some code here which outputs some data
<asp:Repeater runat="server" id="repScore">
<ItemTemplate>
<span class="item"> Some output here</span>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:Repeater>
<EditItemTemplate>
Test test, anything??
</EditItemTemplate>
</ItemTemplate>
</asp:FormView>
I've tried thefollowing solutions in the code behind - none of them works:
protected void fwHotelDetails_ItemCommand(object sender, FormViewModeEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
fwHotelDetails.ChangeMode(e.NewMode);
}
}
and this:
protected void fwHotelDetails_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
{
fwHotelDetails.ChangeMode((FormViewMode)e.NewMode);
}
Clicking the Edit button only gives me the following error message:
The FormView 'fwHotelDetails' fired event ModeChanging which wasn't handled
What more needs to be done?
This page is a great reference for FormView controller: http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/FormViewClass.aspx
Update: I've updated code to refelct Phaedrus suggestion.
Current status is that even after clicking Edit button, the content from ItemTemplate is loaded.
You have to specify which method handles the ModeChanging event. This event is raised when a FormView control attempts to switch between edit, insert, and read-only mode, but before the mode actually changes.
<asp:FormView OnModeChanging="fwHotelDetails_ModeChanging" />
The second parameter of your method signature is 'DetailsViewModeEventArgs' it should be 'FormViewModeEventArgs'.
void fwHotelDetails_ModeChanging(Object sender, FormViewModeEventArgs e)
{
}
Just Simply write code in formview's Item_Command
protected void formview_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
formview.DefaultMode = FormViewMode.Edit;
formview.DataBind();
}
if (e.CommandName == "Cancel")
{
formview.DefaultMode = FormViewMode.ReadOnly;
formview.DataBind();
}
}