Need help with updatePanel - c#

My content page
<asp:updatePanel id="Panel1" runat="server" Visible="true">
<ContentTemplate>
<div>
blah blah
</div>
</ContentTemplate>
</asp:updatePanel>
<asp:updatePanel id="Panel2" runat="server" Visible="false">
<ContentTemplate>
<div>
yada yada
</div>
</ContentTemplate>
</asp:updatePanel>
Code file ..The following code is at the end of Submit button click event :-
Panel1.Visible = false;
Panel2.Visible = true;
Now earlier I was using asp:Panel..then it was working fine..like Panel 1 would hide and Panel 2 would show up instead..it was AFTER I changed asp:Panel to asp:updatePanel that things got screwed up...now the Submit button just won't work !!
What's gone wrong suddenly ?? I changed it to updatePanel so the page doesn't refresh..isn't this how we implement this thing ???
<asp:updatePanel id="Panel1" runat="server">
<ContentTemplate>
<div>
<p>
Type ur name
<asp:TextBox ID="name" runat="server">
</asp:TextBox>
</p>
<asp:Button ID="btn" OnClick="btn_Click" runat="server"
Text="Submit" />
</div>
</ContentTemplate>
</asp:updatePanel>
<asp:updatePanel id="Panel2" runat="server" Visible="false">
<ContentTemplate>
<div>
Thank You!
</div>
</ContentTemplate>
</asp:updatePanel>

At http://msdn.microsoft.com/en-us/magazine/cc163413.aspx#S3 , you can read
Multiple UpdatePanels
A page can host several UpdatePanels. By default, when one UpdatePanel on a page updates, the other UpdatePanels on the page also update. Sometimes that’s what you want, but more often than not, you don’t need every UpdatePanel updating in response to other UpdatePanels.
You can be selective about which UpdatePanel instances update (and when) by setting the UpdateMode property of each UpdatePanel control on the page to "Conditional." Then, when one UpdatePanel updates and calls a server-side event handler, call UpdatePanel.Update on the other panels you want to update. This reduces the load on the server by reducing the number of controls that render, and it reduces the volume of data in the response because UpdatePanels that don’t update don’t add anything to the response.

If you set Visible="false" on an UpdatePanel, it won't be rendered to the client at all. Therefore, if you're doing an Ajax postback, the client isn't going to be able to make the invisible UpdatePanel visible, because it just isn't there.
Think of UpdatePanels just as markers, showing which bits of your page you want to update on an Ajax postback. For your scenario, I think the easiest solution would be to use both UpdatePanels and Panels. Also, because the two things you're updating (the two Panels) are right next to each other, there's no need for two separate UpdatePanels:
<asp:updatePanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel id="Panel1" Visible="true" runat="server">
blah blah
</asp:Panel>
<asp:Panel id="Panel2" Visible="false" runat="server">
yada yada
</asp:Panel>
</ContentTemplate>
</asp:updatePanel>
Then in the code-behind, change the Visible property on the Panel controls.

I do not know where your Submit button is, but maybe try updating those panels with:
Panel1.Update();
Panel2.Update();

Related

Refresh ASP.NET ListView from Event handler

So, I have an event that is fired always when a text file is updated. This is the event handler:
private void FileWasChanged()
{
this.runsList.Items.Clear();
runningModels = RunsFile.ReadFile(Constants.active_runs_loc, Constants.run_file_name);
this.runsList.DataSource = runningModels;
this.runsList.DataBind();
this.updatePanel.Update();
}
Within this method I am clearing the ListView (runsList), reading the contents of the file and then binding the new data to the ListView. The updatepanel is then updated. But that does not happen, the page stays static until I press F5. Here's the layout:
<section>
<asp:ScriptManager ID="scriptManager1" runat="server" ></asp:ScriptManager>
<asp:UpdatePanel id="updatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ListView ID="runsList" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:Label Text="<%# Container.DataItem %>" runat="server" />
</li>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="runsList"/>
</Triggers>
</asp:UpdatePanel>
</section>
So I am trying to give the user of the web site real time information from the text file. For example, when I change a line in the text file, that line should be added to the web page. The event itself works fine (tested with break points and console project), but for some reason the async update does not do anything.
I tried to look for similar problems, but everyone is using a button for the asyncpostbacktrigger. However, I am very new to C# so I may be misunderstanding how UpdatePanel and ListView DataBind work.
Any help appreciated!
Cheers,
Tetsii
Edit: The FileWasChanged is a handler that is called from FileSystemWatcher OnChanged event. The handler updates the view if called from Page_Load or an async button trigger.

ASP.NET UpdatePanel not working, it refreshes the whole page

I'm new in using UpdatePanel, I Have 2 DropDownList:
DropDownList_1 and DropDownList_2
where DropDownList_2 content will be dependent to DropDownList_1 selected value, here is my code:
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList_1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_1_SelectedIndexChanged" DataSourceID="businessgroup" DataTextField="BusinessGroupName" DataValueField="Id"></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList_1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:DropDownList ID="DropDownList_2" runat="server" DataSourceID="jobposition" DataTextField="JobPositionName" DataValueField="Id"></asp:DropDownList>
CS:
protected void DropDownList_1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT DISTINCT * FROM [JobPosition] WHERE BusinessGroupID ="+DropDownList_1.SelectedValue;
DropDownList_2.DataBind();
}
its working as stated above but what I doesn't want in my output is the whole page refreshes, I also tried removing AutoPostBack="true" in my DropDownList_1 but it stops working, what am I doing wrong in here? Thanks!
EDIT:
I also tried moving the DropDownList_2 inside UpdatePanel's ContentTemplate but still the whole page is refreshing.
Here is how you should do:
If you want to refresh the second drop-down, than the second drop-down should be inside an update panel
Set AutoPostBack="true" only for the second drop-down
Set UpdateMode="Conditional" for the update panel (otherwise they will refresh every time)
Set the AsyncPostBackTrigger of the panel to point to the first drop-down SelectedIndexChanged event
Set ChildrenAsTriggers="true" for the Update panel
:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList_1_SelectedIndexChanged" DataSourceID="businessgroup" DataTextField="BusinessGroupName" DataValueField="Id"></asp:DropDownList>
<asp:DropDownList ID="DropDownList_2" runat="server" AutoPostBack="true" DataSourceID="jobposition" DataTextField="JobPositionName" DataValueField="Id"></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList_1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
The controls should reside in the same update panel, it's simpler this way.
I found the fix, thanks to Cristina for reminding me to check the console errors. And just for reference to anyone who is having the same problem here is what I've done:
I have missing Ajax libraries, so I imported the MicrosoftAjax.js and MicrosoftAjaxWebService in this folder Scripts>WebForms>MSAjax.
After I imported the necessary Ajax library I've got this console error:
MicrosoftAjaxWebForms.js:6 Uncaught
Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: 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.
What I did is add EnableEventValidation="false" inside this Ajax page, in <%Page %> directive.
And after that I have no longer full page reload and all is working now on how I wanted.
If your panel still posts back after you've set it up as per guidelines, check that you have not set ClientIDMode="Static" either for the specific control performing the callback or by causing the ClientIDMode to default to static inherited from either in web.config, the Page or parent containers.
Search your solution for ClientIDMode="Static" and either change this for inherited controls including the one triggering the postback, or explicitly set ClientIDMode="Predictable" or ClientIDMode="AutoID" for each of your controls that trigger a postback.
While you are working with update panel you have to register your events while refreshing the page for that you have to use Microsoft's PageRequestManager
to re-subscribe every update.
You have to initialize your event in the document.ready(function(){})
of the javascript.
Ex: var test=Sys.WebForms.PageRequestManager.getInstance();

set read only property to the ajax tab panel in asp.net

I have to set ajax tab panel as in read only property as so its controls also must be have read only property. How can i do that.
You can place the ajax tab panel inside a panel or div and you can disable that panel...
<asp:Panel ID="PnlContact" runat="server">
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" >
<ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="Contacts">
<ContentTemplate>
<asp:Panel ID="InnerPanel" runat="server">
</asp:Panel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
</asp:Panel>
Here you can make the Enabled property of TabPanel1 from code behind..
TabPanel1.Enabled= false;
Are you looking anything like this .
http://codeverge.com/asp.net.ajax_control_toolkit/how-to-enable-or-disable-asp.net/21378

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 :)

Update panel does not make a postback for user control

The update panel does not make a postback when calling Update !
<ajax:TabPanel ID="EmployeesTab" runat="server">
<ContentTemplate>
<asp:UpdatePanel runat="server" ID="MyUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<GW:MyUserControl ID="MyUserControlId"
runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajax:TabPanel>
private void PopulateEmployees()
{
MyUserControlId.EntityId = SelectedEntity.Id;
MyUpdatePanel.Update();
}
Any help!
The UpdatePanel has known problems when inside an AjaxControlToolkit templated control.
This is an old article, but the issue still exists in the current version of the AjaxControlToolkit: http://blogs.sitepoint.com/atlasupdatepanel-template-really-darned-kewl/
The suggested workaround is to wrap the TabPanel in the UpdatePanel rather than vice-versa.

Categories

Resources