Showing posts with label presses. Show all posts
Showing posts with label presses. Show all posts

Wednesday, March 21, 2012

DefaultButton in UpdatePanel

ASP.Net 2.0 has a nice feature. You can - in a panel - define a DefaultButton. When you have focus in a TextBox (on the panel) and presses RETURN, you click the DefaultButon.
When using an UpdatePanel (ScriptManager:EnablePartialRendering="True) this also works fine, but... only once.
Does somebody knows a workaround or a solution or am I doing something wrong?

<atlas:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:PanelID="Panel1"runat="server"DefaultButton="Button1">
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"OnClientClick='alert("click")'Text="Button"/></asp:Panel>
</ContentTemplate>
</atlas:UpdatePanel>

hello.

well, this is a side-effect bug related with the way the code used by the asp.net works. when you add a default button, you get this on your panel:

THe problem happens on the WebForm_firedDefaultButton. Inside, the method checks for a global variable called __defaultFired which initially is set to false. the 1st thing the method does is check the value of this var. if it is set to true, it doesn't do anything since it thinks you're re-submitting the form. if you think only about ASP.NET, this makes sense, right?

unfortunatelly, when yuo use atlas you start getting the behavior you've described. for now, i think that you can override the prerender method of your page and add this:

protected override void OnPreRender (EventArgs args)
{
base.OnPreRender(args);
this.ClientScript.RegisterStartupScript(this.GetType(), "resetBt", "__defaultFired = false;", true);
}

By injecting this instruction, you're resetting the state of the _defaultFired variable so that you're able to start another request when you press the enter key.


btw, what's wrong with the feedback center? i'm trying to add a bug and can't pass from the searh page :(

Thank you for your reply Luis. It works!


Sorry to say but then i am having the same problem and not bale to get it work :(

Can you please help ?

This is a known issue in the current CTP and we are considering fixing it in a future release.

Thanks,

Eilon


the prerender solution doesn't work at my neither :(

maybe it depends on which CTP you're using? are there any other solutions available right now?


protected override void OnPreRender(EventArgs args)
{
base.OnPreRender(args);
this.ClientScript.RegisterStartupScript(this.GetType(), "resetBt", "__defaultFired = false;", true);
}

works in IE but not in Firefox

always working Workaround or Fix would be great


Does anyone have a workaround for this that will work in Firefox? I'd really rather not have to yank out all of my updatepanels, but this is about the only option I see right now.

Thanks


hello.

i really hanve't checked how the current version implements this functionality. i suggest that you use a new static methods of the scriptmanager class instead of using the clientscriptmanager class to register the startup scripts.


Has anyone figured out a way to get the the default button working with firefox in an updatepanel?

Did'nt want to start a new topic!

Thanks


hello.

if i recall correctly, the approach mentioned on this thread doesn't work only in IE (ie, it should work without any problems in firefox)

After struggling with this for a while between browsers andAtlas ajax.net I chose a dumb method and implement this in code, w/o using the DefaultButton attribute. I set this on Page_Load for any control collection. In fact ASP.NET should have a default button setting for just about anything that is clickable - for example the button on an panel extender.

publicstaticvoid SetDefaultButton(WebControl button,ControlCollection cc)
{
foreach (System.Web.UI.Control wcin cc)
{
SetDefaultButton(button, wc.Controls);
if (wcis System.Web.UI.WebControls.WebControl)
{
if (wcis System.Web.UI.WebControls.TextBox)
{
TextBox tb = (TextBox)wc;
SetDefaultButton(button, tb);
}
}}}publicstaticvoid SetDefaultButton(WebControl button,TextBox tb)
{
if (tb.TextMode ==TextBoxMode.MultiLine)
return;

tb.Attributes.Add(

"onkeydown","if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + button.UniqueID +"').click();return false;}} else {return true}; ");
}


The OnPrerender() solution above didn't work for my app.

I had this issue in IE and Firefox.In IE the first time the user pressed enter the default button fired, but after that it won't fire. In FireFox is was just the opposite, the first time it wouldn't fire and then it fired fine after that.

The fix I came up with (after seeing the OnPrerender() exmaple) is to set the __defaultFired variable appropriately in the pageLoad() function which AJAX calls after the page is completely loaded. This works for IE, Firefox and Safari.

<script type="text/javascript" language="javascript">
function pageLoad()
{
if (navigator.userAgent.indexOf("Firefox")!=-1)
{
__defaultFired = true;
}
else
{
__defaultFired = false;
}
}
</script>

I put this on my master page and now all my default buttons fire properly now.

Anyone know why Firefox has the reverse logic for __defaultFired?


I am having the same problem. :( DefaultButton works only in IE, not in Firefox. Can anyone on the Asp.net Ajax team comment on whether this will be fixed for the release version?

Thanks

Default Button Issue

Hi Everyone

I have a asp Panel with few textboxes and two ImageButtons - imgbtnSave and imgbtnCancel

Now what i want that whenever the user presses enter button as long as he is inside that asp Panel, the default button (say I want "imgbtnSave" as Default Button) works.

Note : - I am using my own JavaScript code for DateValidation. So if i use inbuilt defaultButton property of asp Panel then DateValidation fails. I cant change the DateValidation function now as my project is on deliverey stage. So i want JavaScript Code for Default Button

Thanx in advance

you want to set focus to ImageSave button

you can do like this

write the function in javascript

<script type="text/javascript" language="javascript">
function setFocus()
{
document.getElementById("<%= Button.ClientID %>").focus();


}
</script>

call this function like this

protected void ButtonAddCategory_Click(object sender, EventArgs e)
{

ClientScript.RegisterStartupScript(typeof(Classname), "Focus", "setFocus();", true);

}

give me the feedback

-----------

mark as Answer if you feel


Hi Manoj

Thanx for your valuable time....

The solution you have given works in the condition where we have one textbox associated with a single Button.

Now suppose i have Panel inside which i have 60 textBoxes and One Imagebutton

And Other Panel inside which i have 20 textboxes and 2 ImageButton.

Now what should i do???
Hope u understand now

Bye and Take Care

Thanx in advance


Hi DeepakTake a look at the following code:

1 private void SetDefaultButton(TextBox control, Button btButton)
2 {
3 control.Attributes.Add("onkeydown",
4 "if(event.which || event.keyCode)"
5 +"{"
6 + "if ((event.which == 13) || (event.keyCode == 13)) "
7 + "{"
8 + "document.getElementById('" + btButton.UniqueID +"').click();"
9 + "return false;"
10 + "}"
11 +"}"
12 +"else"
13 +"{"
14 + "return true;"
15 +"};" );
16 }
17
18 private void SetDefaultButton(TextBox control, ImageButton btButton)
19 {
20 control.Attributes.Add("onkeydown",
21 "if(event.which || event.keyCode)"
22 +"{"
23 +"if ((event.which == 13) || (event.keyCode == 13)) "
24 +"{"
25 +"document.getElementById('" + btButton.UniqueID +"').click();"
26 +"return false;"
27 +"}"
28 +"}"
29 +"else"
30 +"{"
31 +"return true;"
32 +"};");
33 }
34
35 protected void Page_Load(object sender, EventArgs e)
36 {
37 SetDefaultButton(this.TextBox1,this.Button1);
38 SetDefaultButton(this.TextBox2,this.Button2);
39 SetDefaultButton(this.TextBox3,this.ImageButton1);
40 }
41
42 protected void Button1_Click(object sender, EventArgs e)
43 {
44 this.Label1.Text ="Button 1 was clicked";
45 }
46
47 protected void Button2_Click(object sender, EventArgs e)
48 {
49 this.Label1.Text ="Button 2 was clicked";
50 }
51
52 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
53 {
54 this.Label1.Text ="Image button was clicked";
55 }
56

And on the .ASPX page, I have the following the following controls:

1 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
2 <br />
3 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
4 <asp:Button ID="Button1" runat="server" Text="Button 1" OnClick="Button1_Click" /><br />
5 <br />
6 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
7 <asp:Button ID="Button2" runat="server" Text="Button 2" OnClick="Button2_Click" /><br />
8 <br />
9 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
10 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/Save.png" OnClick="ImageButton1_Click" />

I have placed three textboxes, and each has an associated button.  The third textbox has an imagebutton
next to it. Enter something in the textbox, and press the enter key. It executes the associated click event
for each of them.
The trick over here is to add a small piece of JavaScript code.  I have formatted the code so that you can
understand what it actually does. The trick here is that since I need to make sure it works for IE and Firefox,
I have identify the ENTER KEY (key code 13), using both event.keyCode and event.which (for FireFox), to
fire its respective click.
And to make this implementation easily replicatable, I have turned this JavaScript add-on as a generic function,
which is called from the Page_Load method (as shown in this example). It is overloaded to handle both ordinary
buttons, and imagebuttons.
Hope this helps.
In case you find this suitable, please don't forget to mark it as the answer.
Happy coding!
MANOJ KUMAR SHARMA
www.manojkumarsharma.com