Some weird stuff is happening, I am converting an application that used to use javascript to open another web page in a tiny window for data input to use a ModalPopupExtender.
It seems to work fine, but in the OK event, when I do txtData.Text (the textbox in my modal popup), it comes back with a comma before the data, so if you type "Rabbit", it comes back as ",Rabbit".
Also when I use it multiple times, in another place where I might click to show it, and type "Fish", it starts coming back with stuff like ",Rabbit,,Fish"
I don't know why or how to stop it from doing this... any ideas?
Same here. No clue for why it happens. Every postback initiated by buttons within the panel adds commas and previous values to all text fields.
Hooking into TextBox.Text setter revealed that the corrupted data comes from postdata collection. So it means that just before postback the ModalPopupExtender corrupts the data.
P.S. I'm not using UpdatePanel, but regular Panel so there are no triggers associated with buttons.
Updated: Solution found.
The problem seems to go away when rolling back to May's release of AjaxToolKit (http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326).
Just sharing this solution to everyone on this problem. Try not to use asp control instead use html.
*<input type="text" id="txtID" runat="server" class="myClass" />*
this works fine for me.
Thanks,
I also found a forum indicating that it may be standard html behaviour for when there are multiple controls on the form with the same name. This in mind (and assuming there is a bug in the ajax controls) The way I coded around it was to add in my Page_Load the following kind of statement for each of my textboxes.
string[] vals = txtValue.Text.Split(Convert.ToChar(","));
txtValue.Text = vals[vals.Length - 1];//It appears my latest value was always in the last item
Since the form load happens before the button event I sort out my fields before they get to the event that deals with their values.
I had a similar problem, having a jQuery Dialog inside an UpdatePanel. As I could read on different sites, the problem is caused by duplicates inside the DOM tree.
In the end I found a very simple solution. I assign the dialog to the div, then open or close it by JavaScript and then run this little peace of code to remove the duplicates:
var count = $(".dialog").length;
for (i = 1; i < count; i++) {
$(".dialog").first().remove();
}
EDIT: it turned out not to be SO simple. In the end, my code looked like this:
In document ready (and also asynchronous page calls):
function AddDialog() {
var dlg = $(".dialog").dialog({ autoOpen: false });
dlg.parent().appendTo($("form:first"));
var targetSelector = ".myDialog"; // watch out: can't use ID here!
if (mustOpenDialog) {
$(targetSelector).last().remove(); //-- remove last copy
var dlg = $(targetSelector).dialog({ autoOpen: true });
var count = $(targetSelector).length;
for (i = 1; i < count; i++) {
$(targetSelector).last().remove();
}
}
if (mustCloseDialog) {
$(targetSelector).dialog("close");
var count = $(targetSelector).length;
for (i = 1; i < count; i++) {
$(targetSelector).first().remove();
}
}
}
In my complete code, mustOpenDialog and mustCloseDialog are set in codebehind.
I had the same problem with previous values coming back comma separated. It seemed that my ok button was inside the update panel and I had it in the triggers section aswell. Removing the button from the triggers section of the updatepanel solved the problem.
best regards - Tobias
My answer was similar to Smarty's. My page looked like this...
<UpdatePanel>
<GridView>
<Button> <-- These buttons fire modal popup programmatically.
<Button>
<Button>
</GridView>
<ModalPopup>
<HiddenField> <-- Dummy target of modal popup.
</UpdatePanel>
The fix was to change it to this...
<UpdatePanel>
<GridView>
<Button> <-- These buttons fire modal popup programmatically.
<Button>
<Button>
</GridView>
</UpdatePanel>
<ModalPopup>
<HiddenField> <-- Dummy target of modal popup.
I had to register each button as a postback control with the scriptmanager on the gridview rowdatabound event. The down side is that I have more full postbacks. But, it solved the problem.
This is pretty late reply, but I am documenting it here so that other may benefit.
My Scenario
Open user control inside jquery dialog on button click. This user control had update panel inside it and few textboxes. First time open the dialog, it worked like charm. On subsequent clicks to open the dialog, I noticed a weird behavior. I had few textboxes inside the user control(inside update panel). On any partial post back, the text of textboxes changed to current text, current text. If the value of textbox was fish, then upon any partial postbacks its changed to fish, fish.
The reason for this was I was using jquery to open the dialog. I also appended the dialog to form upon the click of the button.
dlg.parent().appendTo($('form:first'));
So on subsequent clicks multiple user controls where appended to DOM and hence while building the post back data, there existed more than one control with same id and hence there values where appended using "," - comma.
So my solution was simple, while closing the dialog, I just removed it from DOM
I got hint from following link. Posting it here for future reference
here
For some reason it doesn't seem to happen if the textbox is set to ReadOnly.
I'm thinking there could be a workaround by displaying an editable textbox to the user, catching the keystrokes to it, and updating a readonly textbox that is hidden from the user.
Still a bit messy, but I can't roll back to May's release because there's another bug in that release with the ComboBox that I need to avoid!
UPDATE:
As a bit of background, I have a user control (ascx) inside my modal popup because I need to reuse it. The ascx has to handle the user's input itself (the containing page doesn't know what's going on inside the control) so when the user clicks a button I do a callback and process the data. If a successful result is returned to the client callback function then I simulate a click of what the containing page thinks is the "OK" button which is actually invisible to the user.
I've changed my code to add a hidden, readonly textbox and copy the text from the original textbox into the new one every time the text changes.
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
becomes
<asp:TextBox runat="server" ID="txtName" onchange="document.getElementById(this.id + 'RO').value = this.value"></asp:TextBox>
<asp:TextBox runat="server" ID="txtNameRO" ReadOnly="true" style="display:none;"></asp:TextBox>
then when passing values back in the callback instead of getting the value of txtName, I use txtNameRO.
I don't think this will help if you're doing a postback, but you could add a callback before the postback like I have. Hopefully this will help someone anyway!
I had this issue and lost quite a bit of time on it and found that it was caused by an extra tag hiding out that I forgot to remove when changing markup.
Make sure on the .aspx page all tags line up correctly.
<div>
**Lots of code here**
</div> <-- That guy owes me a vacation!
</div>
I have successfully implemented a complete hack based on Jen's response. I use an asp:HiddenField to hold the value I want to post back, and I populate it with a pure HTML input box
<asp:HiddenField ID="txt" runat="server"/>
<input type="text" onchange='document.getElementById("<%= txt.ClientID %>").value = this.value;' />
This is lighter weight than Jen's solution as you're still only posting back one server control.
BTW, this is a very significant bug in the Ajax Toolkit. You can vote and comment on it here:
CodePlex Issue
Pretend you're in Chicago. Vote early, vote often.
For me the solution was to get the modal poup out of the main update panel. Once I did that the commas went away. If you truely need an update panel inside of the modal popup, create one of it's own inside that panel.
I did not need to roll back to previous version of the toolkit.
I have been trying to solve this for a day now and some very helpful person in my office has come up with a terrific solution!
We moved the ModalPopUpExtender and subsequent pop up panel controlled by the extender to ouside of the outermost UpdatePanel on the page, created a fake button and gave the ID of the fake button to the TargetControlID property of the ModalPopUpExtender.
<asp:Button runat="server" ID="dummyButton" CausesValidation="false" Style="display: none" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="dummyButton" CancelControlID="CancelPopUp" PopupControlID="PanelID" BackgroundCssClass="modalBackground" />
Inside my forms/page where the PopUp extender and panels used to be, I created an OnClick event on the button that we used to use to trigger the ModalPopUpExtender.
<asp:ImageButton ID="ButtonID" runat="server" Text="Add" ImageUrl="~/Images/Add-32-1-small.png" OnClick="LoadPopUp" />
On my button that triggers the ModalPopUpExtender, I have created an OnClick event called "LoadPopUp" - in LoadPopUp in the code behind I simply put ModalPopUpExtender1.Show();
protected void LoadPopUp(object sender, EventArgs e)
{
ModalPopupExtender1.Show();
}
I had the same problem except I am not using the AjaxControlToolkit or update panels. My problem was solved by moving the <form> tag from the ASP.net master page to the page where the textboxes are. May help someone.
has same problem and i found the solution here: http://ajaxcontroltoolkit.codeplex.com/workitem/26259
The problem is ajax last version, just INSTALL year 2009 ajax version and modalpopup will work fine with your actual code.
Tuve el mismo problema y encontré la solución en el link que indico arriba.
El problema viene con las versiones recientes de ajax, simplemente instala la version del año 2009 y el modalpopup funcionará bien con tu código actual.
The solution is to get the modal popup out of the main update panel. Once Do that the commas will go away. If you truly need an update panel inside of the modal popup, create one of it's own inside that panel.
Related
First time I have used the site. I appreciate the help I have gotten from the users before and hope to contribute as well as ask. Now, for the question.
I am developing a static toolbar that rests on the bottom on the webpage. The purpose is to display a couple of values to a user purchasing something and for the toolbar to update the information to reflect the choices. There is currently a box on the page that does this, but it rests at the top and the user would have to continuously scroll to see options appear, price changes, etc.
The values update just fine with the price in that box when certain check box buttons are clicked that auto-refresh the page (due to these check boxes changing pieces of the code). However, some things that can effect the price require a button on the page that 'recalculates' to be clicked. The information on this page is put on the page with a controller. The toolbar I am creating is a separate control.
What I am trying to do is have a button within the toolbar that will mimic the actions of the purchase button that recalculates. I tried doing this:
Code on control page for toolbar:
<input type="button" id="buttonID" class="buttonClass" runat="server" onclick="__doPostBack('<%= btnIDOtherControl.ClientID %>','OnClick');" value="Recalculate" />
Code on control with the purchase info:
<asp:ImageButton ID="btnIdOtherControl" runat="server" ImageUrl="~/image.jpg"
AlternateText="Recalculate" OnClick="recalcFunction_Click" />
That is just the current iteration. Before I did not have the 'OnClick' piece of the __doPostBack, I had ''. I also tried using a bit of jQuery. There are quite a few questions about firing an ASP.net button from javascript, but none seemed to work.
What happens is I have gotten it to a point where clicking the recalculate button in the toolbar actually DOES refresh the page, but none of the values change. If I click the recalculate button from the purchase page (where it exists normally), the value in the toolbar does not change even though the values on the page do. If I hit the recalculate button from the toolbar AFTER recalculating from the main page, the toolbar values reflect the regular values. Any other normal use of the page (such as the checkbox that auto-refreshes) will change the values correctly in both places.
If more info is needed just let me know.
Appreciate the help ahead of time!
This has to be done by using javascript or jQuery.
Use the code below:
$("#buttonID").click(function(){
$("#btnIdOtherControl").click();
});
This will trigger the button on other control to be triggered.
PS: Make sure that the ids are correct once the page is rendered in html as visual studio will append the ids of the button in the control to be (id of the control)_(actual id of the button) i.e. If on your page you have referenced control as
<user:control id="control" runat="server">
The id of your button (buttonID) on html will be control_buttonID, so you will need to use that (control_buttonID) in jQuery.
So your code will be
$("#control_buttonID").click(function(){
$("#control1_btnIdOtherControl").click();
});
On you other control make a public property with set only and call your button's click event in it, Then on your working control you have to call only that public property of other control. it should be like this :
public void PublicPropertyCallingEvent
{
set
{
Button1_ClickEvent(null,null);
}
}
You have to access this property only wherever you need.
I'm having one hell of a time trying to figure out why I can't get the LinkButton to PostBack when I have an image with an ImageMap associated with it.
Let me describe the system a little, so you can get a basic understanding of what's happening. The LinkButton has onClick set to call window.shoModalDialog, which obviously opens a modal window where the user can select various options an an ImageMap, save, and close the window. When the window closes, a PostBack is supposed to occur so that the main landing page gets updated with the new options selected.
To add complexity, the link button needs to have an image that shows what points were selected in modal window (the image is a smaller version of the previous window). So what I've done is add an image map to the image in the LinkButton like so:
<asp:Panel ID="pnlC2R1" runat="server">
<asp:LinkButton ID="HyperLink1" runat="server">
<img ID="SomeImage" alt="Some image"
runat="server" src="~/Images/SomeImage.jpg" usemap="#SomeMap" />
</asp:LinkButton>
</asp:Panel>
The map is a regular HTML map (not the ASP.NET one, though I'm sure it's rendered the same, but I digress). Also, I'm using ImageMapster to highlight the points that have been selected, but that doesn't cause any issues as far as I can tell.
If I remove the usemap attribute, a PostBack occurs, if I add it back in, it doesn't.
Is the ImageMap overriding the events that are supposed to occur under normal conditions for the LinkButton? Is there a way to make the ImageMap do a PostBack like the `LinkButton' would under normal circustances?
I tried using the ASP.NET ImageButton control, but I couldn't figure out how to get the ImageMap on it, if that's even possible.
Any help would be greatly appreciated, and please let me know if I need to clarify anything, I'm fairly certain half of what I wrote probably doesn't make sense. :D
UPDATE
The issue had nothing to do with the ImageMap itself. It had to do with the OnClientClick closing the modal dialog before the OnClick event could be called.
Use the asp:ImageMap instead
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagemap.aspx
this is very hard to explain and give code based on it. So thank you now if you even try to help
I have a form with a update panel. This posts the form and validates it. Returning a errorMessage string of any invalid field such as "Field xyz is a required field, Field abc needs to be a quantity"
When I enter incorrect date it posts back fine and displays the text in a div at the bottom of the page which the update panel is aimed at to update. (and only this it should be changing)
This works but the second time I hit say (say i enter invalid data twice) the Complete form will disappear leaving me with just the banner of the site and the error message that is returned.
Ive tried commenting out the entire of the code post back so that the button doesn't actually do anything but it still has the same problem(of course this time it wouldnt have a error message created)
So i can only think it is something to do with the updatepanel itself and how it works but i have been staring at this for ages and have no more ideas!
Very difficult to answer this without some example code. Have a look at what your codebehind is doing to your page and be aware that within your update panel, your postback is actually an AJAX callback. Your page is executed again but only the contents of the update panel are re-renderd and only the same contents are supposed to be updated on the client side.
Often, when I have issues on the second or subsequent postback it is to do with initialisation code running when I don't want it to.
My advice is to look into where you are checking if (Page.IsPostback) and if (Page.IsCallback) and play around with these. Perhaps your need to protect your serverside code that handles the form within the following block:
if (Page.IsCallback)
{
}
I found the issue (after about 5 hours) but don't understand it. The updatepanel that was disppearing had the updatepanel I was using outside of it
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnTabs" />
</Triggers>
</asp:UpdatePanel>
<asp:PlaceHolder runat="server" ID="phldActionButtons" />
The placeholder had inside it the control with my update panel I was using.
The closing updatepanel you can see was the content of the form
By simply moving the placeholder inside of the contenttemplate it seems to have resolved the issue. (not sure why it would hide/remove a whole updatepanel which only relation to it was that it was the form it was validating on the postback!
I just struggled with a similar problem. After about 5 hours, I decided to try this:
UpdateMode="Conditional"
for my UpdatePanel and it stopped causing a second postback!!!
I'm looking to create a custom date picker with code examples from several sources.
Is the code to display/hide an ASP.NET control when a user clicks a button usually done with JavaScript or ASP.NET code? By display/hide, I mean within the page not a popup window.
Please provide a simple example. (If ASP.NET, VB example preferred over C#)
The answer is, it depends. Do you want the date picker show/hide to trigger a postback and thus some code on the server, or do you want it to act purely on the client?
If you want it to act purely on the client, then, modify the markup for your button:
<asp:Button runat="server" ID="myButton" OnClientClick="ShowHideCalendar()" Text="myButton" />
<script language="javascript" type="text/javascript">
var calendarVisible = false;
function ShowHideCalendar()
{
if (calendarVisible)
{
// Code to *SHOW* calendar here
// Show the DIV it's contained in, pop the window with it in, etc..
}
else
{
// Code to *HIDE* the calendar here
}
}
</script>
The key bit is the "OnClientClick" property of the asp:Button control.
Its best practice to do such thing asynchronously, rather than having a full postback that refreshs the entire page.
That means that you have two options:
Update an UpdatePanel in which your
control is placed. That gives you
the benefit of only re-rendering the
content in the UpdatePanel.
Use
clientside scripts to toggle the
control. You also need to perform a
callback, that tells your codebehind
that you just toggled the visibility
to asure your code is in the same
state as the webpage displaying it.
I'd prefer using the second one.
I have a few controls that inherit from ASP.NET buttons and use onserverclick.
If the user clicks twice, the button fires two server side events. How can I prevent this?
I tried setting this.disabled='true' after the click (in the onclick attribute) via javascript, but that blocks the first postback as well.
See this example for disabling control on postback. It should help you do what you're trying to achieve.
http://encosia.com/2007/04/17/disable-a-button-control-during-postback/
You don't necessarily want to show the button disabled on postback. You want to make sure they don't accidentally submit twice. So disabling or hiding the button as a result of a server-side action is already too late in the game. By this point the 2nd request is already on it's way. You need to either do it with javascript or make sure your server side code won't run twice.
In case of an updatepanel and a button inside a FormView-Template I use the following approach:
// Using that prm reference, hook _initializeRequest
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestBuchung);
// Abfangen von Mehrfachklicks auf Buttons für asynchrone Postbacks im Updatepanel
function InitializeRequestBuchung(sender, args) {
var arrButtonIds = ["ButtonInsert", "ButtonUpdate"];
// Get a reference to the PageRequestManager.
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack() & jQuery.inArray(args.get_postBackElement().id, arrButtonIds) > -1) {
args.set_cancel(true);
}
}
This cancels the following postback if an async postback is currently still active. Works perfectly.
Someone else said this somewhere on here a few days ago, and I concur - use javascript to simply hide the button instead of disabling it; you could show a "spinner" image in its place, which lets the user know what is going on.
Instead of hiding, what I have done is swapping buttons using javascript. Show another greyed out image on the click of the first button.
Set the Button property UseSubmitBehavior to false. Then create an OnClientClick function that disables the button.
It would look something like this:
<script type="text/javascript">
function disableFunctn(button){
button.disabled = true;
}
</script>
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="disableFunctn(this);"/>
fastest cheapest way:
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="this.disabled=true;"/>
You can also try for example btnSave.Enable = false; when the button is hit and before the processing for the button is done in the Click Event routine. If you need it to be reset to allow it to be enabled have a separate button that resets the button for reuse.
Another method is to set the button with verification so that the user is asked if they want to Save, it should pop up both times.
Yet another method would be to flag the first occurrence then set a popup for the second to verify a second or subsequent usage.