How to fire TextChanged event without using AutoPostBack property - asp.net

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.

Related

Disable Postback for Linkbutton

Im using ASP.NET LinkButton.
I have a LinkButton which calls code behind from onclick:
on aspx:
<asp:LinkButton ID="LinkTest" runat="server" onclick="LinkTest_Click" OnClientClick="return false;" >LinkButton</asp:LinkButton>
on aspx.cs:
protected void LinkTest_Click(object sender, EventArgs e)
{
//code here
}
As you can see I have set OnClientClick="return false;" so that it wont do a postback.
The postback does not happens. But the code behind is not fired.
I want the code behind to fire and I want no postback when the linkbutton is clicked.
How can I do this?
Thanks.
Remove the OnclientClick attribute of the linkbutton. Try using an UpdatePanel like:
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkTest" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:label id="Label1" runat="server" Text="Initial Label" />
</ContentTemplate>
</asp:UpdatePanel>
Also, you will need to add a ScriptManager in your page.
Add the linkbutton inside updatepanel,
remove onclientclick attribute of the linkbutton:
Page:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblName" runat="server" Text="Old Value"></asp:Label>
<br />
<asp:LinkButton ID="lnkbtChange" runat="server" OnClick="lnkbtChange_Click">Change</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
CodeBehind:
protected void lnkbtChange_Click(object sender, EventArgs e)
{
lblName.Text = "New value";
}

AsyncPostBackTrigger in a Repeater outside UpdatePanel

I am trying to get a cbxSupplement trigger updatepanel refresh, but am not sure if I am using a wrong EventName or it is just impossible to do it with CheckBox. If I replace CheckBox with Button, it works fine.
<asp:Repeater ID="repSupplements" runat="server">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="repSupplements" EventName="CheckedChanged" />
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>
Since CheckBox controls inside repeater are not available at design time you should register them with ScriptManager.RegisterAsyncPostBackControl method. This method requires ScriptManager either on page or on master page.
Create a handler for Repeater.OnItemCreated event and there register newly created CheckBox. The code is following (note that CheckBox should have AutoPostBack property set to true):
<asp:Repeater ID="repSupplements" runat="server"
OnItemCreated="repSupplements_ItemCreated">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" AutoPostBack="True" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
protected void repSupplements_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var control = e.Item.FindControl("cbxSupplement");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
This should do what you want.

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

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