AsyncPostBackTrigger works only the first time - c#

I have an update panel that includes a gridview.
This grid has a drop down list column.
Beta aspx code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" EnablePartialRendering="false" UpdateMode="Conditional">
...
<asp:GridView ID="Gv_Queue" runat="server">
<Columns>
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</Columns>
</asp:GridView
</asp:UpdatePanel>
I add the triggers of the DDL in the UpdatePanel by code:
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = dl.UniqueID; //dl is the Drop Down control
UpdatePanel1.Triggers.Add(trigger);
It works good at the fist selectedIndexChanged event... but the second time that the event is fired the trigger does not work correctly because a post back operation runs.
I already tried:
to change the AsyncPostBackTrigger to a PostBackTrigger but a missing
component exception is thrown.
change the updateMode in theUpdatePanel attribute to 'Always'.
Put another UpdatePanel in the ItemTemplate Column only for the
DropDown.

You have to re-create the trigger on every postback. You could add this code to the Load event of the DropDownList:
aspx:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" OnLoad="ddl_proprietà_OnLoad" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
codebehind:
protected void ddl_proprietà_OnLoad(object sender, EventArgs e)
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ((Control)sender).UniqueID; // sender is the DropDown control
UpdatePanel1.Triggers.Add(trigger);
}

I found a solution to my own question.
You were all rights: I naively forgot that the trigger did not work after the first event just because it must be recreate.
Strangely even the recreation of the trigger in the on_Load even did not resolve the problem.
I did the trick with another update panel like this:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged"
AutoPostBack="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_proprietà" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>

In my case the issue was because I had 2 controls on the page with the same Id. You may want to check this out if the previous responses did not resolve the issue.

Related

Event in Update panel is not fired after rebinding control after Postback

I have a Gridview together with some LinkButton inside of an Update Panel. The click events are fired correctly, the way I expect them to work. BUT: After adding an additional Header Row to the grid in code behind, I need to rebind the Grid after each postback. Since then the click causes a postback, but the method is not executed.
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="GridViewFollowUpMove" runat="server" AutoGenerateColumns ="false" OnRowDataBound="FollowUp_RowDataBound" OnDataBound="FollowUpMove_OnDataBound">
<Columns>
<asp:TemplateField HeaderText="Due date" ItemStyle-HorizontalAlign="left">
<ItemTemplate>
<asp:LinkButton ID="LB_DueDate" runat="server" OnClick="ActivateDueDate" CssClass="NoDeco" CommandArgument='<%# Bind("ISSUEID") %>'>
<asp:Label ID="LabelDueDate" runat="server" Text='<%# Bind("DueDate","{0:dd.MM.yyyy}") %>' Width="60px" Class="line"/></asp:LinkButton>
<asp:TextBox ID="TXTBX_DueDate" runat="server" Text='<%# Bind("DueDate") %>' Font-Size="11px" visible="false" OnTextChanged="TextChanged_DueDate" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillFollowUp();
}
FillFollowUp();
}
I solved this problem by moving the commands that create and customize the additional Header row to the OnRowCreated Event. As I read somewhere: When a postback occurs, the Gridview must be recreated on the server, and in this process ItemCreated is fired for each item, but ItemDataBound is not (since the control properties are pulled from ViewState).
So building these headers in DataBound is the wrong choice!

Why is the selectedindexchanged event firing only once in my code?

I have two drop down lists,the first one is ddlSubjectArea and below is it's source code:
<asp:DropDownList ID="ddlSubjectArea" runat="server" AutoPostBack="True" Height="20px"
OnSelectedIndexChanged="ddlSubjectArea_SelectedIndexChanged" Width="160px"
meta:resourcekey="ddlSubjectAreaResource1" class="notranslate">
</asp:DropDownList>
Here is the code for the second one:
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlCompetency1" runat="server" Width="150px">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlSubjectArea" runat="server" />
</Triggers>
</asp:UpdatePanel>
And this is the code for the selectedindexchanged event of the first dropdownlist.
protected void ddlSubjectArea_SelectedIndexChanged(object sender, EventArgs e)
{
tblSubjectMasterDLL.BindSubjectOnSubjectArea(ddlSubjectName, Convert.ToInt32(ddlSubjectArea.SelectedItem.Value));
bIsMultipleCompetency = tblSubjectAreaMasterDLL.IsMultipleCompetency(Convert.ToInt32(ddlSubjectArea.SelectedValue));
MultipleCompetencySetting();
}
The visibility of the second dropdownlist depends on the first dropdownlist's selected index.The selectedindexchanged event fires once but after that it does not fire.I have tried selecting different values every time but had no success.Where am I going wrong?
Another piece of information that I would like to provide is that even the page load event is getting called only once.Also if I remove the update panels,everything works normally.
I would like to add one more piece of information:If I set EnableEventValidation=false for the page,things start working as expected.I have also heard that setting EnableEventValidation to false is not recommended.

UpdatePanel trigger: Could not find an event named 'OnRowCommand'

I want the OnRowCommand event of the GridView to not perform a full postback on a ErrorLinkButton click. So I wrapped the control in an update panel and added an AsyncPostBackTrigger for OnRowCommand:
<asp:GridView ID="DataSourceMappingGridView" runat="server" DataKeyNames="Index" ClientIDMode="Static" OnRowCommand="DataSourceMappingGridView_RowCommand" OnRowDataBound="DataSourceMappingGridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="ErrorUpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DataSourceMappingGridView" EventName="OnRowCommand" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="ErrorTextLinkButton" CommandName="Errors" CommandArgument='<%#Bind("Index") %>' runat="server" Text='View Errors' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
But I get the following error:
Could not find an event named 'OnRowCommand' on associated control
'DataSourceMappingGridView' for the trigger in UpdatePanel
'ErrorUpdatePanel'.
The event is called RowCommand not OnRowCommand
Replace
EventName="OnRowCommand"
with
EventName="RowCommand"
and it should work

GridView within an UpdatePanel - Get Updated Textbox values on button click

IGNORE THIS:
I was rebinding the grid in the Onload method.
I have a grid view with some textboxes, which I have to wrap in a UpdatePanel Control. So it looks like this:
<asp:UpdatePanel ID="upDistribution" runat="server" OnLoad="upDistribution_OnLoad">
<ContentTemplate>
<asp:GridView ID="gvDistributions" runat="server" AutoGenerateColumns="false"
OnRowDataBound="gvDistributions_RowDataBound"
CssClass="TallCells ContrastTable MaxWidth LeftHeaders"
GridLines="Both" ShowFooter="True" style="">
<RowStyle HorizontalAlign="Left" />
<EmptyDataTemplate>
No pricing history data found.
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="PriceA">
<ItemTemplate>
<asp:TextBox ID="txtPriceA" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DistPrice">
<ItemTemplate>
<asp:TextBox ID="txtDistPrice" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
There is a button "Save" when I would like to get the value entered for the txtboxes. If there is no UpdatePanel around the GV, i can can easily get the txtbox values like:
((TextBox)gvDistributions.Rows[0].Cells[0].FindControl("txtPriceA")).Text
But when, the GridView is wrapped with UpdatePanel, the above statement returns the value that was set on the page load.
How can I get the value of the text box without getting rid of the updatePanel. I need the update panel, because the number of rows, and dates are dependent on another variable in another user control.
As per my understanding, you should use a trigger for the update panel which is something like below.
<asp:UpdatePanel ...>
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Hope this helps!!
Use your TextBoxes with their AutoPostBack property set True
<asp:TextBox ID="txtPriceA" runat="server" AutoPostBack="True"/>
I was rebinding gridview in the onLoadMethod

A control with ID could not be found for the trigger in UpdatePanel

I have an update panel that has UpdateMode of Conditional and ChildrenAsTriggers set to false. I only want a few controls to cause an asynchronous postback:
<asp:UpdatePanel ID="updPnlMain" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
// ...
<asp:Repeater ID="rptListData" runat="server">
<ItemTemplate>
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ItemTemplate>
</asp:Repeater>
// ...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
I am getting the following error when I try and load this page:
A control with ID 'btnAddSomething' could not be found for the trigger in UpdatePanel 'updPnlMain'.
Since my btnAddSomething control is in a repeater and might not be there right away it acts like it is nonexistent. How can I get around this?
Because your control is in the repeater control and it is out of scope to the Trigger collection. By the way you don't need to add trigger because your button control is already in the UpdatePanel, it will update when you click the button.
Edit: There is a solution if you really want to update your updPnlMain updatepanel. You can put in another updatepanel and put your button in that panel. e.g.
<asp:UpdatePanel ID="updButton" runat="server" UpdateMode="Conditional">
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ContentTemplate>
and then simply call the updPnlMain.Update(); method in btnAddSomething_Click event.
It will actually do what you are looking for :)

Categories

Resources