I need to Update a Dropdowlist according to the selected value(s) in a CheckBoxList avoiding the page to reload, I am using an UpdatePanel.
The dropdwonlist should always have an AutoPostback, it should always trigger my "SearchEvent"
I used an UpdatePanel and an AsyncPostBackTrigger on my CheckBoxlist to call the function in code behind to refresh the dropdowlist.
It works only one time. After the first call, I cannot trigger the CheckBoxlist SelectedIndexChanged, instead I get the error :
"Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 404"
My dropdownlist also lose the PostBack, nothing happens.
The View ASPX :
<td>
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:CheckBoxList ID="IsActiveFilterCheck" runat="server" AutoPostBack="true" AppendDataBoundItems="true" CausesValidation="False">
<asp:ListItem Text="Active" Value="1" />
<asp:ListItem Text="Closed" Value="0" />
</asp:CheckBoxList>
</td>
<td>
<asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="IsActiveFilterCheck" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<br /><br />
<asp:DropDownList ID="StatusFilterList" runat="server" DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
My code behind :
private void Page_Load(object sender, System.EventArgs e)
{
RegisterCallbacks();
if (!IsPostBack)
{
Initialize();
}
}
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
CheckProfil();
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SearchButton.ServerClick += new System.EventHandler(this.SearchButton_ServerClick);
this.StatusFilterList.SelectedIndexChanged += new System.EventHandler(this.SearchButton_ServerClick);
this.IsActiveFilterCheck.SelectedIndexChanged += new System.EventHandler(this.IsActiveFilterCheck_Changed);
this.Load += new System.EventHandler(this.Page_Load);
}
protected void IsActiveFilterCheck_Changed(object sender, EventArgs e)
{
// Update the Status Filter List
StatusFilterList.Items.Clear();
if (IsActiveFilterCheck.Items.Cast<ListItem>().Where(li => li.Selected).ToList().Count != 1)
{
BindListInsertAll(StatusFilterList, ExpectedStatusCollection.Instance.GetAll());
}
else
{
IList statusSelected = ExpectedStatusCollection.Instance
.GetAll().Cast<ExpectedStatusDo>()
.Where(exp => IsActiveFilterCheck.SelectedValue == "1" ? exp.Id != 5 && exp.Id != 6 : exp.Id == 5 || exp.Id == 6)
.ToList(); // Only Dead and Signed
// Update the Status
BindListInsertAll(StatusFilterList, statusSelected);
}
UpdatePanel1.Update();
//FindByFilter();
}
When I change the IsActiveFilterCheck checkboxlist I should update only the StatusFilterList as shown in the function IsActiveFilterCheck_Changed, without reloading the full page.
This StatusFilterList should always be able to call/Trigger the event SearchButton_ServerClick
Right now It works only one time when the IsActiveFilterCheck is fired my dropdowlist is updated but after when I click anywhere I get the error and I can't trigger the IsActiveFilterCheck
Take a look to this :
AsyncPostBackTrigger works only the first time
People usually recreates the trigger to work after first time.
Related
I have the following ASP.Net code:
code.aspx:
<asp:UpdatePanel ID="upMain" runat="server">
<ContentTemplate>
<table>
<tr>
<td>DropDownList One</td>
<td>
<asp:DropDownList ID="ddlOne" runat="server" AutoPostBack="true"
OnSelectedIndexChange="ddlOne_SelectedIndexChanged" />
</td>
<td>DropDownList Two</td>
<td>
<asp:DropDownList ID="ddlTwo" runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
code.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlTwo.Visible = false;
}
}
protected void ddlOne_SelectedIndexChanged(object sender, EventArgs e)
{
ddlTwo.Visible = true;
}
What I Expect:
This code is supposed to make ddlTwo visible upon ddlOne's selected index changing.
What Actually Happens:
Upon changing the index of ddlOne, the ddlOne_SelectedIndexChanged function runs (test with debug) and the ddlTwo.Visible = true; runs too but the and the property is changed as I step through the process but the moment the function is over and I want to see my results (i.e. the visible ddlTwo control), there's no result.
If anyone can spot the issue, please let me know.
Thank you!
try
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlOne"
EventName="SelectedIndexChanged" />
</Triggers>
inside update panel.
Change this line
OnSelectedIndexChange="ddlOne_SelectedIndexChanged"
to
OnSelectedIndexChanged="ddlOne_SelectedIndexChanged"
and it should work.
It seems like my update panel isn't updating my form after I click an item in my listview. When I pass by breakpoint in my load method it seems to input everything properly and yet...
.aspx
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlConfig" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
.aspx.cs
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
//get the object
//...
//Assign the value to the DropDownList
this.ddlConfig.Items.FindByText(Configurations.Find(d => d.ID == ConfigurationID).Name).Selected = true;
}
I used something similar in my project and perhaps there's several objects getting selected in the dropdownlist at the same time ? If so, try something like this
//Assign the value found to the selectedValue of the ddlConfig
this.ddlConfig.SelectedValue = this.ddlConfig.Items.FindByText(Configurations.Find(d => d.ID == ConfigurationID).Name).Value;
I have 2 textbox on my page and a Button. When I click on a button, the text from the textbox is emailed. But, then when i refresh the page and no content is there, then also i get an email.
protected void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(TextBox2.Text))
{
//email logic
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
//do nothing
}
}
Here, on clicking the button, i get an email but then when i refresh the page, even though there is no data, then also it goes inside the loop and i get an email.
How do i stop this ?
Do the following in your Page_Load event and keep your TextBoxes and Button in an <asp:UpdatePanel>. Then the page will not ask to re-submit each time you refresh the page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
}
UPDATE
Keep the controls within an UpdatePanel as follows
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Send mail" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
You need to use the the POST > Redirect > GET pattern. Here is explanation link
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 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.