Gridview outside UpdatePanel not showing - c#

I have an ItemCommand event in a button found in a listview.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="systemsView" runat="server"
onitemcommand="systemsView_ItemCommand">
<ItemTemplate>
<asp:Button ID="btnView" runat="server" Text='<%#Eval("SYSTEM_DESC") %>' CommandArgument='<%# Eval("ROW_ID")%>'
class="systemButtonStyle" />
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
When the itemCommand is fired I want to bind another gridview which is not inside the update panel. The gridview is not showing when the event is fired. Any ideas how I could solve this problem please?

Since UpdatePanels only perform a PostBack on controls within their ContentTemplate, all controls that are expected to be updated by a Partial PostBack must be housed within the same UpdatePanel's ContentTemplate.
If you need to update something outside of an UpdatePanel with an action that occurs within it's Partial PostBack, you will have to rethink your use of the UpdatePanel.
Two solutions:
Add the other GridView to the UpdatePanel's ContentTemplate.
Remove the UpdatePanel entirely and use the normal PostBack.

Related

Why is my DropDownList in asp.net not refreshing?

I have a simple DropDownList on my asp.net webpage:
It looks like this:
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="FriendList" runat="server"
EnableViewState="False" Height="30px" Width="10%"
OnTextChanged="FriendListSwitch" AutoPostBack="True">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSend" />
</Triggers>
<--!The button is a stub, I actually need to load it from the codebehind-->
</asp:UpdatePanel>
Then I have the codebehind like this:
void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
{
FriendList.Items.Add(new ListItem(String.Format("{0}", item.Jid.User), ""));
FriendList.DataBind();
}
While debugging I can see that 'FriendList' is being filled, and also that 'btnSend' is being clicked.
Yet my DropDownList is not refilling itself, it just stays empty, why is it not refreshing?
Edit: I'd rather remove the button and replace it with
void OnRosterEnd(object sender)
{
UpdatePanel5.Update();
}
But then I keep getting
The Update method can only be called on UpdatePanel with ID 'X' before Render.
Solved, the DataBind() only worked inside a Page_Load for me.
The button 'btnSend' should be inside the update panel
try using OnSelectedIndexChanged event instead of OnTextChanged
If you need the button to update the dropdown then it needs to be in the update panel
If you need the dropdown list to be the trigger then asyncpostback trigger needs to have the control id of the dropdown list

Right order to add/remove controls to updatepanel on update

I've been trying to add/remove controls to an updatepanel when updating it. Those controls are dynamically filled depending on the info in the page session.
The Updatepanel_Load event is triggered correctly but the controls I've changed won't show properly. They'll only show after a full postback!
Now I know that you need an onInit event to add / alter controls on the page but is this also necessary for an updatepanel? Can someone please explain the right order to do this?
ORDER RIGHT NOW:
Button click
LoginProcedure over ajax
OnInit
UpdatePanel1_Load (Generates controls)
onInit.
So no controls are added / altered until full post back. What is the correct order / method to add / alter controls within an updatepanel without a full postback?
add async postback to your updatePanel like:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Init..."></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBoxTrigger" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>

PostBackTrigger on multiple LinkButtons in an update panel

I have a search form in an updatePanel which retrieves a list of users in a grid in the same UpdatePanel. The name of each user is a commandLink. I want to make the commandLinks as PostBackTriggers.
But when I do it I get an error at the pageLoad time that the controlId does not exist and its true because the grid of users does not render at the load time but through an ajax call.
Any ideas on how can I make the multiple command buttons in a grid retrieved through ajax call as post back triggers?
When adding the items to the grid, within the ItemDataBound event handler, you should register the postback for each specific control (the static identifiers in your HTML declarations are essentially placeholders - not all things repeated in the grid can actually have the same ID). You do this using the ScriptManager.RegisterAsyncPostBackControl method:
The RegisterAsyncPostBackControl method enables you to register Web
server controls as triggers so that they perform an asynchronous
postback instead of a synchronous postback. When the
ChildrenAsTriggers property of an UpdatePanel control is set to true
(which is the default), postback controls inside the UpdatePanel
control are automatically registered as asynchronous postback
controls.
As stated above, using ChildrenAsTriggers is a possibility, too, but this is commonly set to false for more stringent management.
I have found the solution. Here is the code on asp
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
<asp:GridView ID="gvSearchResult" runat="server" OnRowCommand="gvSearchResult_RowCommand"
OnRowDataBound="gvSearchResult_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDetail" runat="server" CommandArgument='<%# Bind("CNIC") %>' CommandName="Detail">
<asp:Label ID="lblName" Text='<%# Bind("Employee_Name") %>' runat="server</asp:Label>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"Height="25px"Width="30%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Ihad to place OnRowDataBound="gvSearchResult_RowDataBound" on gridView and that function looks like below. So I had to register the iterative control in Scriptmanager as PostBackControl in RowDataBound event of GridView.
protected void gvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if ((e.Row.RowType == DataControlRowType.DataRow))
{
LinkButton lnkbtnDetail = (LinkButton)e.Row.FindControl("lnkbtnDetail");
ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkbtnDetail);
}
}
catch (Exception ex)
{
}
}

How do I add an UpdatePanel and its trigger programmatically?

I'm adding a variable number of update panels in Page_Init.
I already have a script manager in my master page.
The problem is that when I try to add a trigger like:
AsyncPostBackTrigger trig2 = new AsyncPostBackTrigger();
trig2.ControlID = ddl22.UniqueID;
trig2.EventName = "SelectedIndexChanged";
up2.Triggers.Add(trig2);
where ddl22 is a DropDownList, the event never seems to trigger the UpdatePanel.
In the UpdatePanel I have another DropDownList the data of which i want to change when the trigger happens.
The funny thing is that in the master page I have a timer. This timer is only supposed to trigger the UpdatePanel in the master but it seems to trigger all of my update panels. However, even when it triggers the update panel in the child page, the second DropDownList does not change its data.
The data is databound to the DropDownList in the UpdatePanel in page_init. It is bound to an objectdatasource which uses the selected item in the first DropDownList as a parameter to determine what data it should bind.
Did you set AutoPostBack="True" for your drop down list? I suspect this is the issue.
Also set your update panel mode to conditional-UpdateMode="Conditional" so that i does not affect other update panels.
Try this,
In Source Code,
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="ddl2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl2_SelectedIndexChanged"></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ddl2" />
</Triggers>
</asp:UpdatePanel>
You can manually call the UpdatePanel to refresh without specifying a trigger on a postBack event. Firstly, set the UpdateMode property to 'Conditional', then you can just call an update on your updatepanel from code behind like
MyUpdatePanel.Update();

Search using Update Panels

Is it possible to use an UpdatePanel that has like a few text boxes, and a search button, and then perhaps another UpdatePanel that has a gridview in it to return the results of what was searched. When the user clicks search it hides the boxes, and displays the gridview. Can I do this with UpdatePanels? I am using c# for my coding. Or should I be doing this another way?
You only need one UpdatePanel in that case and setup a Trigger to your search Button.
Put only the controls that will be refreshed in your UpdatePanel.
Example:
<asp:TextBox ID="txtSearchCriteria" runat="server" />
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="grdSearchResults" runat="server">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Implement the btnSearch_Click function to execute your search and bind the results to the GridView. The UpdatePanel will handle the ajax call and replacing of the HTML that the GridView will produce.
You want to keep as much out of the UpdatePanel as possible and only include what will actually change because it is transmitting that HTML with each update so it's a waste of resources if you are not actually doing anything to those controls with each action. That is why a trigger is best to be used in this case which will hook the UpdatePanel to the Click event outside of the UpdatePanel scope.
Read up more on UpdatePanel and how triggers work on MSDN.
If I understand the question correctly, you can do that with two <asp:Panel> controls inside the <UpdatePanel>. One panel for the textboxes, and the other for the gridview. You set which panel to show in the codebehind, depending on whether you're expecting your user to enter search criteria, or review the search results.
Yes you can.
You also could use just one update panel. Since you the Search Form (could be in a Panel) and the GridView inside the UpdatePanel.

Categories

Resources