Showing posts with label defaultbutton. Show all posts
Showing posts with label defaultbutton. Show all posts

Wednesday, March 21, 2012

defaultbutton, defaultfocus and UpdatePanel

Looks like defaultbutton and defaultfocus properties of the asp.net <form> do not work with UpdatePanel. Is there a workaround?

<

scriptrunat="server">protectedvoid Button1_Click(object sender,EventArgs e)

{

Label1.Text =

"You clicked the button1";

form1.DefaultButton =

"Button2";

}

protectedvoid Button2_Click(object sender,EventArgs e)

{

Label1.Text =

"You clicked the button2";

}

</

script>

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><atlas:ScriptManagerID="sm1"runat="server"EnablePartialRendering="true"/><formid="form1"runat="server"defaultbutton="Button1"defaultfocus="TextBox1"><atlas:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Button"/><asp:ButtonID="Button2"runat="server"OnClick="Button2_Click"Text="Button"/><asp:LabelID="Label1"runat="server"Text=""></asp:Label></ContentTemplate></atlas:UpdatePanel></form>

</

body>

</

html>

hello.

the only i've used with success was to handle the propertychanged event of the pagerequestmanager and check for the _inPostBack...if you search this forum, you'll see a similar thread with 15 to 20 posts that talks about this issue.


I am having the same problem and i am not able to locate the article with the search request that you have mentioned.

Can you please provide it to me please. This is the last hurdle for me before the release :(

Thanks a lot in advance.

Try this. It solved my problem

protectedvoid Page_Load(object sender,EventArgs e)

{

Page.Form.DefaultFocus = nameTextBox.ClientID;

Page.Form.DefaultButton = submitButton.UniqueID;

}

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

DefaultButton in Firefox

I added a DefaultButtion to a normal asp panel to handle enter key event. This works fine in IE, but has problem in FireFox.

This asp panel is in anupdate panel.

When user presses enter key in FireFox, this does not trigger OnClick function of the button, it only triggers client side function OnClientClick. My submit button code is in server side OnClick function. So when user presses the enter key, nothing is saved.

I debugged the javascript, traced to following function:

function WebForm_FireDefaultButton(event, target) {
if (event.keyCode == 13 &&
!(event.srcElement &&
event.srcElement.tagName.toLowerCase() == "textarea")) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
} else {
defaultButton = document.all[target];
}
if (defaultButton && typeof defaultButton.click != "undefined") {
defaultButton.click(); myapplication continues. But web application which does not use Ajax stops here and posts back. ?
event.cancelBubble = true;
if (event.stopPropagation) {
event.stopPropagation();
}
return false;
}
}
return true;
}

Is this problem related to update panel. I used the approach inhttp://forums.asp.net/thread/1685533.aspx. (UseSubmitBehavior="False" OnClientClick="ResetDefault" ).But that does not work.

Any suggestion will be much appreciated.

Any luck with this issue? Sounds like I have the same issue as you and I also tried the ResetDefault function with no luck. I reset the __defaultFired variable (i think that's the name of it) inside the page_prerender using the registerclientstartupscript method with no luck. I noticed that the the enter button does cause a postback, it just doesn't fire the button that it is suppose to be firing. If you look at the Request.Forms collection the button is missing from it. If you look at the same collection after clicking the button with the mouse it is there. I hope someone can shed a little light on this matter. I am going to try to create a simple page to reproduce the issue.

DefaultButton in a Panel inside an ajax UpdatePanel

The DefaultButton does not act properly on browsers other than MS IE. I guess that the WebForm_FireDefaultButton is the main problem. When I override this function by one that i wrote the problem becomes solved.

Could you post the fix you made please? Right now I'm about to pull out the update panels on my page because I must have default button working under firefox.

Thanks


I would also like to know the fix, and where you put the fix at. Thanks!

DefaultButton and the ValidationSummary Control

Scenario:

I have an asp:panel control with the defaultbutton attribute set to a button within the panel. The button is part of a validation group that includes a requiredvalidator control associated with a textbox (within the panel). I also have a validationsummary control that is part of the validationgroup and also has its ShowMessageBox attribute set to true. The validation summary displays correctly when I click on the button, but when I hit enter on the keyboard I get the text value of the required validator to show up but the validationsummary popup message does not appear. I posted the code below. Any clues?

<asp:UpdatePanel ID="upCustomerEdit" runat="server">
<ContentTemplate>
<asp:Panel ID="panCustomerEdit" runat="server" DefaultButton="btnSave">
<table>
<tr>
<td>Some label:</td>
<td>
<asp:Textbox ID="Textbox1" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="valTextbox1" runat="server" ControlToValidate="Textbox1" ValidationGroup="valGroupSave" EnableClientScript="true" Text="*" Display="Static" ErrorMessage="Textbox 1 value is required" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="Server" Text="Save" ValidationGroup="valGroupSave" CausesValidation="true" />
</td>
</tr>
</table>
</asp:Panel>
<asp:ValidationSummary ID="valSummary" runat="server"
DisplayMode="BulletList"
ShowMessageBox="True"
ShowSummary="False"
ValidationGroup="valGroupSave"
HeaderText="Errors on Page:" />
</ContentTemplate>
</asp:UpdatePanel>

The exact same problem plagues me also. The ValidationSummary does not show up when the enter key is being pressed.

Has anyone yet found a solution for this one?Sad

sincerely stawrogin


See if this post helps you out at all:http://forums.asp.net/p/985791/1798911.aspx

-Damien


Validation controls, which includes theBaseCompareValidator,BaseValidator,CompareValidator,CustomValidator,RangeValidator,RegularExpressionValidator,RequiredFieldValidator, andValidationSummary control are not compatible with UpdatePanel

http://weblogs.asp.net/scottgu/archive/2007/01/25/links-to-asp-net-ajax-1-0-resources-and-answers-to-some-common-questions.aspx

You can download compatible version of validators from here

http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx


Thanks for your suggestions. It is not about the UpdatePanel. In fact i don't use an UpdatePanel. It's about the ValidationSummary not being shown.

Take this simple scenario (code below): A Loginform with username, password, and a Login-Button. The defaultFocus is on the username, the defaultButton is set to the Login-button. Additionally there are two RequiredFieldValidators and a ValidationSummary. When clicking the Login-Button all controls get evaluated and the ValidationSummary is being shown. When hitting enter, only the Text of the RequiredFieldValidator is shown but not the ValidationSummary. I want the Validation to be the same whether anyone clicks the button or hits enter. But how?


<form id="form1" runat="server" defaultfocus="txtUserName" DefaultButton="btnLogin">
<div>
<div>
<h2>Login</h2>
</div>
<div>
<asp:Label ID=lblUserName runat=server AssociatedControlID="txtUserName" CssClass="w-117">Benutzername</asp:Label><asp:RequiredFieldValidator ID=reqUser runat=server Text=" *" ControlToValidate="txtUserName" ErrorMessage="Erforderliche Eingabe Benutzername." SetFocusOnError=true></asp:RequiredFieldValidator>
<asp:TextBox id="txtUserName" runat=server CausesValidation=true CssClass="w-153" />
</div>
<div>
<asp:Label ID=lblPassword runat=server AssociatedControlID="txtPassword" CssClass="w-117">Passwort</asp:Label><asp:RequiredFieldValidator ID=requPassword runat=server Text=" *" ControlToValidate="txtPassword" ErrorMessage="Erforderliche Eingabe Passwort." SetFocusOnError=true></asp:RequiredFieldValidator>
<asp:TextBox id="txtPassword" runat=server CausesValidation=true CssClass="w-153" TextMode=Password />
<asp:Button ID=btnLogin runat=server Text="Login" />
</div>
<asp:ValidationSummary ID=valSummaryLogin runat=server DisplayMode=BulletList ShowSummary=true/>
</div>
</form>


I have a similar issue. I have a Validation Summary out side of a GridView that has a Range Validator inside of it. The Range Validator will fire correctly with either pressing "Enter" or clicking the "Update" button (set as DefaultButton). However the Validation Summary will not update when I press "Enter", only when I click on the "Update" button.

Anyone else have an idea?

DefaultButton and DefaultFocus

Hi

I have a code, when i use DefaultButton and DefaultFocus.

When i put UpdatePanel to the Page, DefaultButton and DefaultFocus work only on first load of page. After callback it is dosn't work. Anybody know where is the problem?

Btw. I use MasterPage in this code then i set DefaultButton and DefaultFocus in PageLoad

Page.Form.DefaultButton = this.Button1.UniqueId;

Hi,

I also faced the same issue. I think the asp.net provided default button has some issue regarding this

I worked with BinaryIntellect DefaultButton server controls which works as expected. You may try it from here: http://www.binaryintellect.com/products

Cheers,
Murugan.G

I encounted the same problem before, Finally, I put me.textbox1.focus in Page_Load, that OK, you may try it.