FreeTextBox javascript error is preventing PostBack on my DropDownList - c#

I have one page with 3 FreeTextBox controls on it. They are set up correctly, and I was using them normally until I needed to add a DropDownList control that would PostBack to the server, but I was surprised to see that the OnSelectedIndexChanged event would never trigger. If I was to do a Post with a button, or some other server-side control, then would the event be triggered. After much debugging I found the following Javascript error was being thrown every time I selected something different on my DropDownList control:
TypeError: FTB_API.MainContent_MainContent_FreeTextBox1 is undefined
The error seems pretty straight forward;
Firebug tells me this error comes from the following function:
function WebForm_OnSubmit()
{
FTB_API['MainContent_MainContent_FreeTextBox1'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox2'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox3'].StoreHtml();
return true;
}
I've tried several things without success. When I remove the FreeTextBox controls from my page, I have successful PostBacks. Any help would be appreciated.
Thanks.
EDIT 1: This is some of my markup
3 FreeTextBox set up like this:
<FTB:FreeTextBox ID="FreeTextBox3" JavaScriptLocation="ExternalFile" ButtonImagesLocation="ExternalFile" ToolbarImagesLocation="ExternalFile" runat="server" EnableHtmlMode="true" />
My DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Set the property AutoPostBack = true of your dropdown in markup page. This will make the post back when you change the dropdown element and OnSelectedIndexChanged get triggered.
Example :
<asp:DropDownList id="drpList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="event name" />

I have found the answer to the question in this thread: Hidden FreeTextBox bug on Firefox It seems for some reason that when the control is not visible or is hidden (I have tabs) it behaves this way. The answer is kind of a Hack, but it works. Thanks for the answers.

Related

Click on LinkButton in Gridview: why is the Grid reloaded most of the times but not always?

I´m trying to understand the annoying refreshment of the GridView/Repeater that occurs most of the times I click on a LinkButton within that element. If this is caused by the Postback, then why doesn't it happen all the times?
And if it is not caused always, do I might even get rid of it?
I usually do this to get detailed information on a cell clicked in a Grid or repeater:
<asp:LinkButton ID="ButtonSelect" runat="server" CommandName ="Select" CommandArgument = '<%# Eval("date") %>' Text='<%# Bind("TAG") %>' OnClick="GetDetails"/>
The data processed in the "GetDetails" method will then be displayed in some other element. There wouldn't be any need to refresh the Grid.
Is this the normal behavior for any LinkButton click in a GridView?
Martin
It's the normal action of a server button within a gridview. It will cause a postback. A postback will force the Page LifeCycle. The entire page will be recreated and Databinding may or may not occur depending on your cache options and programming.
"Fixing" that really depends what you are trying to accomplish. If it's simply to stop the "screen flicker" due to the postback, consider using AJAX calls or <asp:UpdatePanel> server controls.
If you are trying to work with JS on the clientside and just wish to defer the postback until later, convert the button to a template field and replace the <asp:LinkButton> with a basic html control <a href="javascript:void();" ...>, <button type='button'>, <input type='button' ..., etc...

Handling the submit action of two TextBoxes

I have an ASP.net page.
That has an Ajax Toolkit Tab Control.
That has tabs.
That have custom ascx controls I wrote.
I have a text box that perform a search action. It is declared like this:
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
Nothing fancy. This format has been working for months. There's no submit button. It just posts back when I hit enter. The problem appeared when I added a second custom control using the same type of feature. Now browsers don't postback when I type something in either of these textboxes and press enter.
It seems that browsers have a default way of handling one textbox in one form, but that behavior changes when the number reaches two.
Is there an easy way around this? I guess I can create a hidden submit button but it seems like there is probably a better way to deal with this when the functionality is in two separate custom controls.
Your feedback is appreciated!
Check this out: http://www.allasp.net/enterkey.aspx
The default behavior with no submit button seems to depend on the browser, and the behavior can indeed depend on the number of input controls.
I would add hidden "submit" button (e.g. style="display:none;") which should ensure that it always gets submitted.
The answer was a little different than I expected, but philosophically like my original idea that #jamietre reinforced.
I had to surround the controls with an <asp:Panel> tag with a DefaultButton attribute. A-like-a so:
<asp:Panel ID="ButtonPanel" runat="server" DefaultButton="btnSubmit">
<asp:Label ID="Label1" runat="server" Text="Course:"></asp:Label>
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtPrereq_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtPrereqSearch"
WatermarkCssClass="Watermark" WatermarkText="e.g., MATH201"></asp:TextBoxWatermarkExtender>
<asp:Button ID="btnSubmit" CssClass="InvisibleSubmit" runat="server" Text="Submit" OnClick="txtPrereqSearch_TextChanged"/>
</asp:Panel>

Checkbox not entering OnCheckChanged first time

<ajaxtoolkit:AccordionPane ID="accordianPaneAroundTheCheckbox" runat="server">
<Content>
<asp:UpdatePanel ID="updatePanelAroundTheCheckbox" runat="server" >
<ContentTemplate>
<div>
<asp:CheckBox ID="chkFoo" AutoPostBack="true" runat="server" OnCheckedChanged="DoBar"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</Content>
</ajaxtoolkit:AccordionPane>
We have something like the above. Extra stuff has been left off as it is a large page.
I place a breakpoint at the begining of Page_Load and DoBar. The first time I click the checkbox, the breakpoint on Page_Load is hit, but DoBar is not. The second time I click the checkbox, both breakpoints are hit.
Why could this be happening? I'm not dynamically generating the checkbox. It's ClientID is the same every time (no dynamically generated or ID'd containers). I've tried resubscribing to the event in the Page_Load, but it didn't hit the first time, and just hit it subsequent times twice.
Update I have tried removing the UpdatePanel completely. Not only does the whole page postback, which I don't want, but the event is still not entered. What can block/swallow an event call like that? Is there some exception deep in the bowels of a master page or framework call or something somewhere that I can't see?
Is that the correct source code you've pasted? I'm asking because there's no event called OnCheckChanged, there is however an OnCheckedChanged event. Using your code it works just fine when the markup for the checkbox looks like this:
<asp:CheckBox ID="chkFoo" runat="server" AutoPostBack="true" OnCheckedChanged="DoBar"/>
The checkbox isn't being set by anything on the server side. The form is populated by an ajax call to a PageMethod that pulls a databag from the server and populates the controls. The problem is that the server thought it was unchecked, but javascript checked it. When I unchecked it, the postback happened, and the viewstate still thought its previous state was unchecked, and the current state is now unchecked. Thus, the CheckedChanged event doesn't fire.
A thousand grisly deaths to Viewstate. I'm close enough to fix the issue now. Thanks for trying/looking.

Changing a control's style based on validation (ASP.NET)

I've got a very simple form with the following troubled snippet:
<asp:Panel class="normal" ID="Panel1" runat="server">
<strong><asp:Label ID="Panel1Error" class="error" Visible="false" runat="server"/></strong>
<label for="TextBox1"><em>*</em> Don't leave this blank</label>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="TextBox1RFV" runat="server"
ControlToValidate="TextBox1" ErrorMessage="This field cannot be blank."
Display="None" />
<--- other validators --->
</asp:Panel>
There are two things I want to do when the page fails validation:
Change the style of Panel1 (to one which shows different colors to indicate an error). I was able to do this by calling Page.Validate in Page_Load, then iterating over Page.Validators, getting each validator's parent control, casting it to a Panel, then setting .CssClass Doesn't seem like a superb solution, but it got the job done - is there a better way?
I want to take whatever validation error(s) are thrown and put them in the Panel1Error label, as well as set it to visible. This is where I am a bit baffled. I thought at first I could possibly specify the Label in which a validator writes its ErrorMessage, but I had no such luck. If I just toss the validator inside the Label, its formatting messes up the entire layout of the page, regardless of whether I directly assign it the 'error' CSS class or just leave it in the Label.
Just to clarify, in production, I would be doing this process for multiple Panels on a page, each with one form element, preventing me from calling the Panels explicitly and just saying Panel1.CssClass, etc.
I would recommend a javascript solution. ASP.NET injects a global js variable called Page_Validators, which is an array of all of the validator spans on the page. I wrote about this on my blog. It's a different solution, but it should give you enough insight to get started.
Use ValidationSummary controls with a ValidationGroup for each panel.
Seems fine if it worked.
Use a ValidationSummary control. Or you can inherit from the controls and override the render event.

ScriptManager.RegisterClientScript in a UserControl within a FormView inside an Async Panel

I'm having an annoying problem registering a javascript event from inside a user control within a formview in an Async panel. I go to my formview, and press a button to switch into insert mode. This doesn't do a full page postback. Within insert mode, my user control's page_load event should then register a javascript event using ScriptManager.RegisterStartupScript:
ScriptManager.RegisterStartupScript(base.Page, this.GetType(), ("dialogJavascript" + this.ID), "alert(\"Registered\");", true);
However when I look at my HTML source, the event isn't there. Hence the alert box is never shown. This is the setup of my actual aspx file:
<igmisc:WebAsyncRefreshPanel ID="WebAsyncRefreshPanel1" runat="server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsCurrentIncident">
<EditItemTemplate>
<uc1:SearchSEDUsers ID="SearchSEDUsers1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
Hello
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Button" />
</ItemTemplate>
</asp:FormView>
</igmisc:WebAsyncRefreshPanel>
Does anyone have any idea what I might be missing here?
Have you tried using RegisterClientSideScript? You can always check the key for the script with IsClientSideScriptRegistered to ensure you don't register it multiple times.
I'm assuming the async panel is doing a partial page past back which doesn't trigger the mechansim to regenerate the startup scripts. Perhaps someone with a better understanding of the ASP.Net Page Life Cycle and the CLR can fill in those blanks.
For me that works fine. resizeChartMid() is a function name.
ScriptManager.RegisterStartupScript(this, typeof(string), "getchart48", "resizeChartMid();", true);
Try this, i got the same issue
ScriptManager.RegisterClientScriptBlock(MyBase.Page, Me.[GetType](),
("dialogJavascript" + this.ID), "alert(\"Registered\");", True)
This worked for me!

Categories