OnTextChanged event is not firing - asp.net

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.

Related

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

updatepanel working in terms of request and response

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..

Update mode property of UpdatePanel

Following is my ASPX code.
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblid" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnid" runat="server" Text="Button"/>
</div>
</form>
I have one update panel control and have included single lable control inside the update panel control. I have Button and Lable outside the update panel control and during page_load I update the the text value of both the lable control as follows.
protected void Page_Load(object sender, EventArgs e)
{
lblid.Text = DateTime.Now.ToString();
Label1.Text = DateTime.Now.ToString();
}
I have set Update mode property to 'Conditional' so that when Button control outside the Update panel is clicked by user, It should not change the text value of lable inside the ipdate panel. But it update and diplay the changed value of label text inside the update panel. My understanding is that when we set update mode property to 'Conditional', content inside the update panel is not updated(or rendred on client side) when there is a postback due to control outside the update panel then what is happening in my case.
Clicking the button outside of UpdatePanel is causing the entire page to postback. An UpdatePanel can't prevent the contents inside it from being refreshed in the case the when entire page is being posted-back.
if you have only one updatepanel it makes no sence
see this example where it does make sense.
try playing around with the second updatepanel by changing it from Conditional to Always
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblid" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnid" runat="server" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
CS:
protected void Page_Load(object sender, EventArgs e)
{
lblid.Text = DateTime.Now.ToString();
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
}

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