Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Wednesday, March 28, 2012

Ajax update panel is resetting text box values that were calculated with javascript

Dear all,

I have some text boxes which hold numbers which are calculated from other text boxes using javascript. This works fine.

My problem is that I have put some these boxes (as well as many other controls etc) inside an Ajax Update Panel and as soon as the panel updates, it resets my textboxes back to their default value. This does not happen after a postback without the update panel.

Can anyone tell me why this is and how I may fix it?

Thanks for your help.

It sounds like the calculation might be taking place on the client-side after the partial postback has already begun. What triggers the calculation? What triggers the partial postback of the UpdatePanel?


The clientside calculation has already taken place and is visible on screen. This is done my an onchange event on the textboxes. The AJAX partial postback in the update panel is done afterwards only on the press of a button.

Thanks


Are the changes visible when the postback begins or just when it ends? With UpdatePanels, client side changes that happen during the partial postback will be lost when the async postback completes. For instance, if someone hits enter in one of the TextBoxes, would the partial postback initiate before the client side calculation completes?

Another common problem is code running in Page_Load that resets the TextBoxes each postback. You might consider stepping through the entire partial postback with the debugger and make sure that no unexpected code is being run.

If it's none of that, post some code. I'll take a look at it for you.


The changes are visible when the postback begins. The whole thing works perfectly if it is not in an update panel There is nothing in the page_load to reset anything.

The page/code is large to I have put an example below. Put a 1 in the first text box and tab and the second textbox will display a converted amount. Click the button to postback and the values will stay. Put all the controls in an update panel and the postback resets my calculated amount.

I'm so stuck here. Really appreciate your help.

thanks

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<headrunat="server">

<title>Example</title>

<scriptlanguage="javascript"type="text/javascript">

function convertMeasures()

{

convertedWeight=document.getElementById("<%=MetricWeight.ClientID %>");totalWeight=document.getElementById("<%=Weight.ClientID %>").value;

lbsinkilos=2.20462262;

convertedWeight.value=totalWeight/lbsinkilos;

}

</script>

</head>

<body>

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

<div>

<asp:TextBoxID="Weight"runat="server"onchange="convertMeasures();"Style="position: relative"></asp:TextBox>

<asp:TextBoxID="MetricWeight"runat="server"Style="position: relative"></asp:TextBox><br/>

<br/>

<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Style="position: relative"

Text="Postback Nothing"Width="137px"/>

</div>

</form></body>

</html>


I just tried using the Infragistics Update Panel and it works fine. The values are retained after postback. Unfortunalty I'm not allowed to use infragistics in this application. :-(

thanks


Using this code, I wasn't able to reproduce.

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Example</title
<script language="javascript" type="text/javascript">
function convertMeasures()
{
convertedWeight=document.getElementById("<%=MetricWeight.ClientID%>");
totalWeight=document.getElementById("<%=Weight.ClientID%>").value;
lbsinkilos=2.20462262;
convertedWeight.value=totalWeight/lbsinkilos;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="sm1" />
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate>
<asp:TextBox ID="Weight" runat="server" onchange="convertMeasures();" />
<asp:TextBox ID="MetricWeight" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Postback Nothing" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Try that code and see if you can reproduce the problem that you're having.

Hello

ok - I have found what it is that causes the problem, although not the answer. The problem only occurs when I make the MetricWeight textbox readonly(as I have been to stop users altering the result of the calculation). Code below. Strangly this now seems to happen even if there is no Ajax. Do you know how I can stop this?

Thank you for your time

<%@.PageAutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"Language="C#" %>

<%@.RegisterAssembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

Namespace="System.Web.UI"TagPrefix="asp" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<headid="Head1"runat="server">

<title>Example</title>

<scriptlanguage="javascript"type="text/javascript">function convertMeasures()

{

convertedWeight=document.getElementById("<%=MetricWeight.ClientID %>");

totalWeight=document.getElementById("<%=Weight.ClientID %>").value;

lbsinkilos=2.20462262;

convertedWeight.value=totalWeight/lbsinkilos;

}

</script>

</head>

<body>

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

<div>

<asp:ScriptManagerID="sm1"runat="server">

</asp:ScriptManager>

<asp:UpdatePanelID="up1"runat="server">

<contenttemplate>

<asp:TextBoxID="Weight"runat="server"onchange="convertMeasures();"/>

<asp:TextBoxID="MetricWeight"runat="server"BackColor="LightGray"ReadOnly="True">0</asp:TextBox>

<asp:LabelID="Label1"runat="server"Style="position: relative"Text="Label"></asp:Label><br/>

<br/>

<asp:ButtonID="Button1"runat="server"Text="Postback Nothing"/>

</contenttemplate>

</asp:UpdatePanel>

</div>

</form>

</body>

</html>


That'd do it. From theTextBox.ReadOnly documentation:

The Text value of aTextBox control with theReadOnly property set totrueis sent to the server when a postback occurs, but the server does noprocessing for a read-only text box. This prevents a malicious userfrom changing aText value that is read-only.


Probably the easiest workaround is to remove the ReadOnly attribute from the TextBox and add: onfocus="this.blur();"

Alternatively, since the values are still posted to the server, you could reassign each TextBox's value in each postback, using MetricWeight.Value = Request["MetricWeight"].


Thanks for your help. I also used the OnKeyUp event instead of the OnChange to make it work a little better too.

Thanks again!

AJAX Update Panel and DataGrid

I write code to populate a datagrid with a Datatable stores in a session variable, I put some data in text boxes and click ADD button it stores that data in table and assign that table to a session variable. FINE.

When I click a datagrid item to Edit. Occasionally it says "Index is out of bound...". It is not always and not always with every row of datagrid. I click on another row to edit it works fine.

I do not know what is actually going inside AJAX. because of using AJAX my ADD and EDIT functionality do not refreshes the page but update the content as required.

Any body know that what should I do?

Regards,

Ather Ali Shaikh

I had a similar problem. I found that on every command I had to recreate the dataview (what I was using) where I used the info from the dataview then things were fine but if I didn't I would get out of bound errors as well as other weird messages. I wasn't trying to use the edit feature of the GridView but just displaying info when a link in a the GridView was clicked so you may also run into things I've not yet found. Anyway, hope this helps some.

Hi,

I do not use the Edit template of the datagrid, what I am doing is using template columns contains Edit link button and on capture is command click event on datagrid on Command event handler.

Saturday, March 24, 2012

Ajax timer update does not work when it updates a text box with some special characters

The Ajax time seems to have some trouble updating a panel that contains a text box with some special characters. I managed to replicate the problem using a very simple page that is inlcuded below. The problem is that the second update causes an error 500 from the development server of Visual Studio.

The page just has an update panel with a single multi-line text box. The code for the timer update assignes the text box the string "<br>\n".

Does anyone know how to solve this problem?

The Page is:

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
The code for the update is:

public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { }protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text = "<br>\n;"; }}

Regards,

Ivan

By the way, I do realize that the problem is with special characters that trigger the ASP.NET security validations. Changing the text in the Text Box to HTML encoded is not an option, because in this text box the user should be entering and editing HTML code.

AJAX Timer bug when it updates a panel containing a single Text Box with the characters "&

The Ajax timer seems to have a problem updating a panel what contains a text box that has the characters "<br>\n". Attached is the source code for a very simple page a single multi-line text box where the timer code-behind just assigns the textbox the string "<br>\n". When run, the second update will cause an 500 error from the server.

This error does not happen when the text is something else, say "Hello World".

I assume that the problem is that the sequence "<br>\n" in the text box causes the timer Java Script to break somehow. Anyone knows oh to solve this problem? I am using Ajax 1.0 and the Visual Studio Development Server.

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

The code behind for the time update is:

protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text ="<br>\n;"; }

Ivan.

I am sorry, the code for the post got mangled but the editor. The code behind for the time update should be:

public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { }protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text = "<br>\n"; }}

Apparently, the string <br> is detected as a potentially harmfull string. One solution is to HTML encode the string. However, I do need to display the string <br> in the text box, not <br>. The user must be able to edit the HTML code that we will use.

Ivan.


Apparently, the string <br> is detected as a potentially harmfull string. One solution is to HTML encode the string. However, I do need to display the string <br> in the text box, not <br>. The user must be able to edit the HTML code that we will use.

Ivan.

Wednesday, March 21, 2012

AJAX TabPanel ScrollBars

Hello,

I use AJAX Tabs to display text and image content in 4 tabs (fixed height, no scrolling). I'd like to add 5th tab to display a text which is pretty long so it's supposed to be scrollable. Whatever I do, I'm not able to make scrollbars apper in the tab. The text just flows down the page with no limits. I've tried to set fixed height for both TabContainer and TabPanel together with ScrollBars attribute set to all possible values. It simply doesn't work for me. Where is the catch?

 <atk:TabPanel ID="tabComments" runat="server" HeaderText="Nové komentá?e" ScrollBars="Both" Height="100px">
<ContentTemplate>
<table>
<asp:Repeater ID="rptComments" runat="server" DataSourceID="sqlDS_DefaultComments">
<ItemTemplate>
<tr>
<td><%#Eval("ItemName")%></td>
<td><%#Eval("ItemOriginalName")%></td>
<td><%#Eval("RatingInserted")%></td>
</tr>
<tr>
<td colspan="3"><%#Eval("RatingText")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</ContentTemplate>
</atk:TabPanel>

Thanks for any help,
Radoslav

Add a DIV to your TabPanel and set the CSS overflow (or overflow-x and/or overflow-y) to be auto along with a fixed height.

-Damien


It works! Thanks a lot.Smile
Radoslav