Showing posts with label textbox. Show all posts
Showing posts with label textbox. Show all posts

Monday, March 26, 2012

trigger is causing the user controls OnInit to fire

I have a TextBox inside an UserControl which triggers an update panel on TextChanged. Works really well, but I noticed a problem. When AJAX makes the call back to the server to execute the TextChanged event, it is actually firing the UserControl's overridden OnInit beforehand. Is this normal behavior? I would think that for a page, OnInit should only get called once - when the page is created. There is a good deal of initialization code in my OnInit that really should not be executed again if it does not have to. Is there a way to make the UpdatePanel trigger not fire OnInit?

AJAX postbacks are executed at the server like normal postbacks (the entire page lifecycle is processed from the beginning), so Page.IsPostback = True for AJAX postbacks.

Saturday, March 24, 2012

Triggering Dynamic Controls

Hi,

I got several Textboxes which are being created dynamically.

private TextBox CreateTextBoxes(int i)
{
TextBox tb = new TextBox();
tb.Width = 30;
tb.Height = 20;
tb.CssClass = "inputfield";
tb.ID = "TextBoxID" + i.ToString();
tb.Text = i.ToString();

Microsoft.Web.UI.ControlEventTrigger trig = new Microsoft.Web.UI.ControlEventTrigger();
trig.ControlID = this.FindControl(tb.ID.ToString()).ToString();
trig.EventName = "CheckChanged";
UpdatePanel1.Triggers.Add(trig);

return tb;
}

Throws an Error at FindControl:Object reference not set to an instance of an object.

When I don't use FindControl it compiles but the Event is never triggered. The dynamic Controls are wrapped in a static UpdatePanel1 via the designer.

Is that possible at all with Atlas yet?

The problem with this code is that you never added the dynamically created text box to any control collections. FindControl() works by traversing the controls collections, and control are only rendered (unless you call RenderControl() yourself) if they appear somewhere in the control collection tree for a page.

Actually, you have no reason in the code segment above to call FindControl(), because you have the class instance local to that scope. You would only use FindControl() to retrieve a control instance where you have no local variable assigned or had no way to retrieve the instance using other means.

The reason you never see the trigger is because the control you created in the code was never attached to anything that would have ended up visible on the client. If you added the control to the collection for after this code segment, you have no guarantee that the actual control ID generated once the page is rendered would be the same as you assume in this code, since control ID's actual values depend on their placement within the naming container "tree" starting from the page down.

My suggestion would be to never do anything that assumes a consistent control ID (and UpdatePanels depend on this for handling a variety of things on the client side) with a dynamic control until you have added it to a control collection that is attached to a page already. Then all the things that need to be wired to make your triggers (or commands or events) work will work.


The textbox method is called in a foreach loop, mostly about 4-6 textboxes are being created.

Is this maybe possible with the new Dynamic UpdatePanel featured in the new Atlas build?
The new UpdatePanel in the June CTP certainly does change the dynamics of the problem you described.

Triggering TextChanged event

I'm trying to get a textbox to trigger it's textchanged event and post back via atlas - my code is as follows:

<atlas:ScriptManagerID="sm1"runat="server"EnablePartialRendering="true"/>

<atlas:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<asp:TextBoxID="TextBox1"runat="server"OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

</ContentTemplate>

<Triggers>

<atlas:ControlEventTriggerControlId="TextBox1"EventName="TextChanged"/>

</Triggers>

</atlas:UpdatePanel>

It doesn't work! Is this something that should work? I've tried also doing it with a timer which does create a postback, but my textbox loses it's focus everytime.

I've tried looking for posts relating to this but the search functionality on these forums doesn't seem to allow you to restrict searches to specific forums any more.

I wonder if anyone else has tried this or has any suggestions?

Thanks

hello.

i might be wrong, but i think that the trigger should only be used to refresh a panel based on the change of a property or event of another control that is placed *outside* of the updatepanel. when i look at your code, i see that the textbox is inside of the panel. in this case, i think that you only need to set up the textchanged event (btw, don't forget to set the autopostback prop of the textbox to true or the event won't fire!)

I've tried my code both with and without a trigger, and with the text box outside and inside of the update panel - none of these work - in all cases it expects the Return key to be pressed to trigger the postback.

Has anyone got any ideas what I'm doing wrong here, or is this functionality not present?

Think we need ScottGu to the rescue?


hello again.

have you set the autpostback property to true on the textbox? if you don't set that property you won't get an automatic postback when the value of the control is changed.

Yes, autopostback is set - but it only triggers when I press return. I'd hope if would trigger the textchanged event whenever a key was pressed in the text box and cause an Atlas postback.

Also I see on Scott's examples that he is able to directly reference a control that's inside the control template - I had to use FindControl to get to mine - any idea why this is?

I'd be grateful if someone could try this and see if I'm doing something wrong - here's my code:

<formid="form1"runat="server">

<atlas:ScriptManagerID="sm1"runat="server"EnablePartialRendering="true"/>

<asp:TextBoxID="TextBox1"runat="server"OnTextChanged="TextBox1_TextChanged"AutoPostBack="True"></asp:TextBox>

<atlas:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<h1>

<asp:LabelID="Label1"Text="Waiting for first postback"runat="server"></asp:Label>

</h1>

</ContentTemplate>

</atlas:UpdatePanel>

</form>


hello again.

well, the textchanged event only fires when the textbox looses its focus. so, if you hit tab after changing the value of the textbox you should also get a postback.

regarding your second question about getting a control, can you give us more info on what you're trying to do?

thanks.

Wednesday, March 21, 2012

trouble setting focus on a textbox after I put the panel in a updatepanel

My webpage was working fine setting focus properly on a textbox but after I put a few panels in an updatepanel the focus no longer works. I put two panels in the same updatepanel box, each one has a button click that goes and retrives some info. Everythng works fine except focus isnt being set. Im using c# in Visual Studio 2005 professional

how do you handle the focus of the textbox and in what event? usually you can set the focus using $get(textboxClientID) during the pageLoaded event of Sys.WebForms.PageRequestManager

hth


Are you using YourScriptManager.SetFocu(YourControl) ?


ScriptManager.SetFocus($get(textBox.ClietnId))

Let me know if this helpful to you

Trouble with $addHandler and return values.

I'm using $addHandler to wire up a keydown event to a TextBox control, like such:

function pageLoad() {
$addHandler($get('TextBox1'), "keydown", TextBox1_KeyDown);
}
function TextBox1_KeyDown(eventArgs) {
if (eventArgs.keyCode == 13)
return false;
}

If I explicitly specify OnKeyDown attribute of the TextBox, this works. However, when I add it using $addHandler, it acts as though the return value isn't being passed back through. I set a breakpoint on the conditional and verified that it is correctly returning false, but the browser submits anyway.

Is there any way to make a "KeyDown" handler using $addHandler that'll pass the function's return value through to the element?

Hi,

did you try with evt.preventDefault() ?


I wasn't familiar with that method. I just tried it like this:

function pageLoad() {
var evt = new Sys.UI.DomEvent($get('TextBox1'));
evt.preventDefault();
}
It didn't seem to have any effect either way. Am I doing that wrong?

Hi,

sorry, I don't understand what you're trying to do in the previous snippet. I was referring to the first snippet you posted, which could be modified as follows:

$addHandler($get('TextBox1'),'keydown', function(evt) {
if(evt.keyCode === 13) {
evt.preventDefault();
}
});
The preventDefault method should prevent the default action for a DOM event from being performed.
That's perfect. Thanks!