updatepanel working in terms of request and response - asp.net

I have 2 sets of asp:TextBox and asp:Button controls with one set inside an update Panel and another outside..
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button Text="text" runat="server" ID="btn1"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button Text="text" runat="server" ID="btn2"/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
codebehind
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = TextBox2.Text;
}
Now My question is, how exactly the Updatepanel is working?
Actually I thought, on btn1 click only the contents of UpdatePanel are sent to server as request and response comes from server to only that updatepanel.
So, I entered some text in TextBox2 and clicked btn1.. but surprisingly TextBox1 text got changed to as that of TextBox2..
Then I thought, the entire page is sent to server as request and response comes only to that updatePanel and is updated accordingly..
Now I checked both the requests(ie., Request from btn1 and btn2) in Google Chrome Developer tool.. again to my surprise, there is difference in Content-Length of the request..
If there are more controls outside the update panel then the Content-Length would be more with the request from btn2
Could someone put some light on this..

Ok.. the thing here is that even though you have a UpdatePanel in the page , when a postback occurs all the contents are posted back to the server. This behaves like a normal page without an UpdatePanel. That's the reason you have an option UpdateMode for the UpdatePanel...
It is UpdateMode="Always" and UpdateMode="Conditional".
Because you did not set anything here it is Always by default, because of which the whole page was posting back...
To see the changes you were looking for after adding the UpdatePanel you need to set it to conditional and add the elements as AsynPostbackTriggers ...
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn1" />
</Triggers>
<ContentTemplate>
<asp:Button Text="text" runat="server" ID="btn1"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button Text="text" runat="server" ID="btn2"/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Now when you click btn1 only contents inside the UpdatePanel are posted back and can be seen in the response.
If btn2 is clicked only the contents outside the UpdatePanel are seen in the response..

Related

change control outside UpdatePanel

In short: I want to enable a button i.e. outside UpdatePanel when I click on a button from inside UpdatePanel.
Problem: The button enables but tags i and asp:Localize are not showing in the rendered code after button click from UpdatePanel updPnlOfferings
Detail:
I have a DataList inside UpdatePanel which contains delete button for every item in the list. delete button is firing an event on server which deletes the record and disables a button (i.e. outside of the UpdatePanel.
As someone suggested I kept my button(which need to be disabled) in UpdatePanel below
<asp:UpdatePanel ID="updPnlCourse" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton ID="lnkDeleteCourseDetails" runat="server" OnClick="lnkDeleteCourseDetails_Click">
<i class="icon-remove-sign"></i>
<asp:Localize ID="Localize6" runat="server" Text="<%$ Resources:CBLabels, LBL_DeleteDetails %>" />
</asp:LinkButton>
<asp:ConfirmButtonExtender ID="lnkDeleteCourseDetails_ConfirmButtonExtender" runat="server"
ConfirmText='Are you sure you want to delete this course?' Enabled="True" TargetControlID="lnkDeleteCourseDetails">
</asp:ConfirmButtonExtender>
</ContentTemplate>
</asp:UpdatePanel>
The Main UpdatePanel which is firing the event is below
<asp:UpdatePanel ID="updPnlOfferings" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlDSOfferings" runat="server">
<asp:DataList ID="dtlstOfferings" runat="server" OnItemCommand="dtlstOfferings_ItemCommand" EnableViewState="true">
</asp:DataList></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
on dtlstOfferings_ItemCommand event I coded this
lnkDeleteCourseDetails.Enabled = true;
lnkDeleteCourseDetails.ToolTip = "";
lnkDeleteCourseDetails_ConfirmButtonExtender.Enabled = true;
updPnlCourse.Update();
This is a known problem as specified in this link.
The solution is to put all your content that are there inside the LinkButton, within a place holder like below, or make all the child controls as server controls.
<asp:LinkButton ID="lnkDeleteCourseDetails" runat="server" OnClick="lnkDeleteCourseDetails_Click">
<asp:PlaceHolder runat="server">
<i class="icon-remove-sign"></i>
<asp:Localize ID="Localize6" runat="server" Text="<%$ Resources:CBLabels, LBL_DeleteDetails %>" />
</asp:PlaceHolder>
</asp:LinkButton>

How to fire TextChanged event without using AutoPostBack property

I have one master page and one web form selected with that master page.
In this web form I take two text boxes and one label.
Code I wrote in C# file [aspx.cs] as follows :
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = "Welcome " + TextBox1.Text;
}
I don't want to keep AutoPostBack propery because when I press tab button then its value fires and appears to the label so that I don't want AutoPostBack property.
From internet i see the code for updatepanel and scriptmanager.
In updatepanel i write trigger tag but also it is not working.
my ASPX file :
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName ="TextChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
so how to fire TextChanged event.

How can I prevent all update panels reloading when I click a button in one of them?

I've got a webform with two update panels:
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release">
</asp:ScriptManager>
<p>
<asp:UpdatePanel ID="upl1" runat="server">
<ContentTemplate>
<asp:Button runat="server" ID="btn1" Text="Button1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upl2" runat="server">
<ContentTemplate>
<asp:Button runat="server" id="btn2" Text="Button2" />
</ContentTemplate>
</asp:UpdatePanel>
</p>
If I click btn1, then the load event of upl2 is triggered, as well as upl1.
I would expect only upl1 to be loaded, since this is the update panel that contains the button.
Why isn't this happening, and how can I make it happen?
One pratice I always use when I work with Update Panels, is to set the properties: ChildrenAsTriggers="false" UpdateMode="Conditional"
And refresh the Update Panel only when I want:
protected void btn1_Click(object sender, EventArgs e)
{
// some logic
// ....
upl1.Update();
}
Try applying these properties and see if it solves your problem.
I had a similar problem with a button triggering an UpdatePanel, and using ChildrenAsTriggers="false" UpdateMode="Conditional" didn't help.
I ended up using the following to force the button to do a regular post-back:
ScriptManager1.RegisterPostBackControl(btn1);

OnTextChanged event is not firing

I have a TextBox inside my .Aspx page:
<ajax:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCity" AutoPostBack="true" OnTextChanged="txtCity_TextChanged"
Width="90%" runat="server" ></asp:TextBox>
</ContentTemplate>
</ajax:UpdatePanel>
Code behind:
protected void txtCity_TextChanged(object sender, EventArgs e)
{
lblMessage.Text = "you have typed:" + txtCity.Text;
}
And for lblMessage [on the same .Aspx page]:
<ajax:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server" Text="" ></asp:Label>
</ContentTemplate>
</ajax:UpdatePanel>
But when I am typing in the TextBox. lblMessage is not updating.
How to rectify this?
It sounds like you're thinking that the OnTextChange event is fired while you are typing in the text box. This is not true. OnTextChange is a server-side event and only fires when the page (or panel) is posted back. Typing into a text box on a page does not post the page back and so this event will only fire once you submit the form.
What you would actually want to do in this case, is to use some JavaScript with the onkeypress JavaScript event to update the label text as things are typed into the TextBox. JavaScript is run on the client and doesn't require you to post back the page in order for it to run.

Make update panel component change visibility of item outside of the update panel

I have a repeater and the items are editable through an edit button which opens a FormView in edit mode. The formView is initially invisible and the repeater visible. Once edit is pressed the repeater goes invisible then the formview becomes visible with the item to edit.
Once changes have been made the user presses update. This sets the formview invisible and the repeater visible.
The problem is the formview goes invisible but the repeater doesn't become visible. This I think is caused by the fact the formview is within an update panel and the repeater isn't? Only the items in the update panel are being altered on clicking update because it is only a partial page update.
I can't put the repeater within the update panel because there is a requirement that the public view doesn't use javascript.
Does anyone know how I could make the repeater reappear?
<asp:Repeater id="resultsRepeater" runat="server" DataSourceID="vehiclesDataSource" >
<ItemTemplate>
<asp:Label id="makeLabel" runat="server" Text='<%# Eval("Make") %>' />
<asp:Button id="editButton" runat="server" Text="Edit" CommandArgument='<%# Eval("Id") %>' OnClick="EditButton_Click" />
</ItemTemplate>
<asp:Repeater>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Panel id="insertUpdatePanel" runat="server" Visible="false">
<asp:FormView id="editformview" runat="server" DataKeyNames="Id" Datasourceid="VehiclesEditDataSource" >
<EditItemTemplate>
<uc:VehiclesEdit ID="VehiclesEdit" runat="server" />
<asp:Button id="updateButton" runat="server" OnClick="Update_Click" />
</EditItemTemplate>
</asp:FormView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
protected void EditButton_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = false;
insertUpdatePanel.Visible = true;
}
protected void Update_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = true;
insertUpdatePanel.Visible = false;
}
This might help. I had a similar problem and this worked for me. I simply used ScriptManager to register the button (even iterated by row) as a postback control like this:
ScriptManager.GetCurrent(Page).RegisterPostBackControl(updateButton)
This caused a full postback and allowed me to set the visibility of a panel outside the update panel. I hope it works!
REVISED: Add a PostBackTrigger to your UpdatePanel to force a full post-back when your UpdateButton is clicked. This will hide your UpdatePanel and reveal your Repeater again. See final code below:
For more info refer to: https://stackoverflow.com/questions/2545508/how-do-i-force-full-post-back-from-a-button-within-an-updatepanel
<asp:UpdatePanel ID="updatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="updateButton" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:FormView ID="editformview" runat="server" DataKeyNames="Id" DataSourceID="VehiclesEditDataSource">
<EditItemTemplate>
<uc:vehiclesedit id="VehiclesEdit" runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:Button ID="updateButton" runat="server" OnClick="Update_Click" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

Resources