Determine when Postback occurs because of a textbox being changed - c#

I've got a usercontrol that has a <asp:TextBox> with autopostback set to true. When the user types text in and moves off the textbox, asp.net fires a postback event. However, it isn't handled specifically by the textbox, but is instead simply a postback - the page_load fires and then life goes on.
What I need is a way to know that this textbox fired the event. I then want to fire my own custom event so that my main page can subscribe to it and handle the event when the textbox changes.
At first I thought I could capture the sender - I was assuming that the sender would be the textbox, but it appears to be the page itself.
Anyone have any thoughts?

I think you're missing something, so I want to explain it step by step :
your textbox should be something like that :
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
and in your codebehind you shhould have an event like that :
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string str = TextBox1.Text;
}
NOTE : if you want to attach your event in code-behind you can do this by :
TextBox1.TextChanged +=new EventHandler(TextBox1_TextChanged);
but if you talking about custom controls, you should implement IPostBackDataHandler interface to raise events.

Is this i dynamically created usercontrol or textbox? If so you have to recreate that control in the page_load event and the event should be fired on the textbox as normal.

The page load sender will always be ASP.default_aspx because the page is what is calling that event.
You can hook up to the even OnTextChanged where that sender would be the textbox that has changed or caused the post back.
Keep in mind the page load will always fire before any of the events on the controls.

This is potentially something that could be overlooked, but do you actually have an event coded for the textbox's TextChanged event? If you have it set to autopostback but never actually created an event for it, you're only going to get your page load and other related events.

As alluded to in other answers you need to handle the 'OnTextChanged' event.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
TextBox txt = (TextBox)sender;
string id = txt.ID;
switch(id)
{
case "TextBox1":
break;
}
}

Related

WebForms TextBox Lost Focus Event

I'm trying to add an event handler to a TextBox control that is called when the text box loses focus. Is this possible?
I know WinForms has a LostFocus event. I'm looking for something similar in ASP.NET WebForms.
You can Use OnBlur
<asp:TextBox id="TextBox1" runat="server" onblur="Javascript:alert('1234');" />
Or
EDIT:
Server side
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("onblur", "alert('hi User')");
}
}
I think you are going to have to use javascript for this and the event you will need to search and look up would be onBlur() I believe. If you need an example, I might be able to scrounge one up for you. But here are some links..
W3Schools
Stack
Java Page
Hope these help.
This would be the blur event, and it's a client-side event on the resulting input element instead of a server-side event on the TextBox control.
For example, let's say you have a TextBox with the ID of "TextBox1", and let's assume jQuery is included in your project template (because it probably is), then in your client-side code you might have something like this:
$('#<%= TextBox1.ClientID %>').blur(function () {
// code to respond to the event
});
This would execute any code in that function whenever the input for that TextBox control loses focus. Notice the use of a small block of server-side code to output the server-side control's client-side ID.

How to set method to call of usercontrol link

I've created a user control that displays the header (not < head >, I mean the title, datetime page is created and so on) on each page on my website. In that user control I also have one link. A link that will be displayed on the page IF (user = admin).
Currently this link is a pure link, and I have no problems to change the "navigateUrl" to the correct value for each page. (Each page includes this user control, and from each page I set the value for navigateUrl.)
But!
On some of my pages I use a linkbutton instead of a hyperlink. But then I have to add that linkbutton from the page.aspx instead of usercontrol.ascx
My problem is that, I want to change the hyperlink in my user control to a linkbutton instead, so I can call methods with that link. (Method is on page.aspx, not within the user control).
What method to call differ from page to page, so I want to set which method to call each time I include the user control.
If I have inside my user control
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
And for now I set values within the usercontrol by:
mainPageHeader1.headTitle = "text";
How do I set what method to call for the linkbutton?
Update
In the markup of your UserControl, specify the handler for the click event
<asp:LinkButton OnClick="LinkButton1_Clicked" runat="server" ID="LinkButton1" />
Declare a custom event in your usercontrol
public class MyUserControl
{
public event System.EventHandler LinkButtonClicked;
//add handler for your LinkButton
protected void LinkButton1_Clicked(object sender, EventArgs e)
{
//Raise your custom event here that can be handled in any page that use your control
LinkButtonClicked(sender, e);
}
}
In your page.aspx, add handler for your custom event
protected void MyUserControl2_LinkButtonClicked(object sender, EventArgs e)
{
//handle the event here
}
Update
In your page where you add your control,
<custom:MyUserControl ID="MyUserControl2" runat="server"
LinkButtonClicked="MyUserControl_LinkButtonClicked" />
That's all
Update
Subscribing for the event in code-behind did the work. I have not figured out why it didn't work from the markup.
From the Page_Load of the page in which the UserControl is, do this
MyUserControl2.LinkButtonClicked += new EventHandler(MyUserControl_LinkButtonClicked);
and it should work.
Checks to see if the event has been subscribed to Either codebehind (eventhandler += EventHandler(sender, e) or the aspx markup OnClick="EventHandlerMethodName") - this would be null if it wasn't subscribed to somewhere

How do I make an ASP.Net GridView postback on an onBlur event?

I have an ASP.Net GridView, with two columns ("ID" and "name"). The name column is editable via an <asp:TextBox>. What I want to do is have the grid trigger its update event when the user clicks away from an editable cell.
I know I can attach an onBlur event handler to the TextBox to retrieve the value when the user clicks away. However, I don't know how to trigger the GridView's update event programmatically through JavaScript. Any suggestions?
On your TextBox set AutoPostBack="True". This will cause a postback when the client change event fires, which is probably what you want rather than blur. Attach a TextChanged handler to the textbox and you can call your grid update method from there.
you can get the row index for the textbox that triggered the event like this:
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
GridViewRow gridRow = ((GridViewRow)((TextBox)sender).NamingContainer);
Console.WriteLine(gridRow.RowIndex);
}
You could call the submit method for your Form object from the onBlur event.
myForm.submit();
You could set AutoPostback="True" on your Textbox and in the Codebehind save the new valuein the Event Handler.

There has to be a way to get my data bound at the correct time

Say aspx page called theParent has a DataGrid control named theDataGrid and a UserControl named theUserControl , and theUserControl has a button named theUcButton .
Yes, I know, very imaginative naming.
When theUcButton is clicked , a session variable is changed.
This session variable is a select parameter for the datasource of theDataGrid.
However, because of the order of stuff called in the page lifecycle, when theUcButton is clicked and a postback is generated ,
theParent controls are loaded before theUserControl resets the session variable, and theDataGrid does not show the new data until the next postback .
How to I get what I want to work?
I am using 3.5 if that matters.
Here is example code for your user control to raise an event.
public partial class WebUserControl : System.Web.UI.UserControl{
public event EventHandler Updated;
protected void Button1_Click(object sender, EventArgs e)
{
//do some work to this user control
//raise the Updated event
if (Updated != null)
Updated(sender, e);
}}
Then from your .aspx page, you'll deal with the new event just like usual
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnUpdated="WebUserControl1_Updated" />
Code behind:
protected void WebUserControl1_Updated(object sender, EventArgs e)
{
//handle the user control event
}
You can declare an event as a member of the UserControl class, raise it, and handle it in the Page class.
You can also use other events like Page.PreRender() to pick up things after the user control's events.
The easiest way would be to hold off on the databind until the later in the lifecycle, such as the Page.PreRender event as joelt suggested. If you do the bind then, you should have the session variables.

User control page_load event invoked before the Button click event of the aspx page

I want to assign property value on button click in aspx page and want to pass the value to the usercontrol and than bind data according to the Property value.
But the problem is before button click event is fired the page_load of user control is invoved ? is the any way to call the page_load of user control again on button click or is there anyother alternative to do it ?
Thanks
Utilizing Page_Load from a UserControl makes the control very dependant on the page life cycle and thus not very flexible. A better way would be to add a public method to the control that you call from the button OnClick event. That method would then perform the data binding.
Kinda like this:
//MyPage.aspx
void Button_OnClick(object sender, EventArgs e)
{
MyUserControl.DataBind(MyTextBox.Text);
}
//MyUserControl.ascx
public void DataBind(string value)
{
UpdateView(value);
}
This is kludgy, but if you have to you can always call the Page_load event manually after the button fires.
A better approach would be: depending on what code needs to fire after the button_click event, you can move it to another event handler, like the OnPreRender method.

Categories

Resources