I am having a strange issue associated with AsyncFileUpload control. after the upload, I am updating the page by calling__doPostBack function from ClientUploadComplete event handler. it works fine first time, but next time I try to upload the file, it refreshes the page first before uploading, then does the upload and refreshes the page again. not sure why refresh page is being called twice once before the upload and once after the upload. I have a simplified version of this code which has this bug. any clues please why it is happening?
Markup:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Javascript:
<script type="text/javascript">
function AsyncFileUpload1_ClientUploadComplete() {
var btnRefreshData = $get("<%=Button1.ClientID%>").name;
__doPostBack(btnRefreshData, "");
}
</script>
Code-Behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Upload complete";
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
System.Threading.Thread.Sleep(3000);
}
looks like no one can answer this question. I still haven't figured out why this happens, but I put a workaround by adding a flag in session when the upload is complete and check that flag before refreshing the data on the page. this way the data refresh won't happen before the upload. thanks.
Related
I'm trying to test something for a component I'm building. Here's what I'm trying to do.
Upload file to a server without posting back or refreshing the page.
I decided to use ajaxfileupload control.
The file uploads but after the SaveAs Method is called in the backend, the whole page is getting refreshed for some weird reason.
Here's the code .aspx Page:
`
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now %>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<ajaxToolkit:AjaxFileUpload runat="server"
ID="AjaxFileUpload1" OnUploadComplete="UploadComplete" />
</ContentTemplate>
</asp:UpdatePanel>
`
Here's the backend Code:
`
protected void UploadComplete(object sender, AjaxFileUploadEventArgs e)
{
AjaxFileUpload1.SaveAs(Server.MapPath("/" + e.FileName));
}
`
The DateTime.Now block is to keep track of refreshes/postbacks.
I have a simple webform with an ajax call, which executes flawlessly, however when I put the same code into an existing project, the ajax call refreshes the page. The project was .net 2.0 converted to .net 4.0. Here is the simple code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick"></asp:Timer>
<asp:TextBox ID="TextBox1" runat="server"
Height="118px" TextMode="MultiLine" Width="468px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Save"
onclick="Button1_Click" /><br /><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" Height="120px"
TextMode="MultiLine" Width="466px"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
</asp:UpdatePanel>
<div>
</div>
</form>
</body>
</html>
cs code:
protected void Button1_Click(object sender, EventArgs e)
{
Save();
}
private void Save()
{
//Add the save function here ex store the text to DB
//Here we only move between the two textboxes to show that it works
TextBox2.Text = TextBox1.Text;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Save();
}
Update. I found the issue, this line:
xhtmlConformance mode="Legacy"
was added to webconfig during conversion from .net 2.0.
Commenting this line fixes the ajax refresh.
I made a dialog box showing wrong password or username error but this doesn't close.
how to close this dialog box without refreshing the page,Code is like
<asp:Panel ID="errorMsg" runat="server" Visible="false">
<asp:Label ID="msg" Text="" runat="server"></asp:Label>
<asp:Label ID="errorHead" Text="Something is really Wrong :" runat="server"></asp:Label>
<asp:button ID="try" OnClick="try_Click" runat="server" Text="Try Again" />
</asp:Panel>
Code Behind :
protected void try_Click(object sender, EventArgs e)
{
errorMsg.Visible = false;
}
errorMsg.visible is set to true when wrong input is made but after making it again false it doesn't close.
The OnClick event will fire a PostBack refreshing the page.
You could try using javascript:
<script text="text/javascript">
function hide(id){
if(id)
document.getElementById(id).style.display = "none";
}
</script>
<asp:Button ID="try" OnClientClick='hide("<%=errorMsg.ClientId%>")'
runat="server" Text="Try Again" />
Or you could simply wrap the dialogbox with an Update Panel. The Update Panel allows you only to refresh a part of your website.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="errorMsg" runat="server" Visible="false">
...
<asp:Button ID="try" OnClick="try_Click"
runat="server" Text="Try Again" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
I use the below code to auto refresh the page every 60 seconds via the AJAX tools in VS2010. Works perfectly.
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled" UpdateMode="Conditional">
<ContentTemplate>
ASP.NET/HTML Code
<p>
<asp:Button ID="Button2" runat="server" Text="Click here" OnClick="Button2_Click" /> to disable the pages automatic refresh.</p>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="60000">
</asp:Timer>
</asp:View>
<asp:View ID="View2" runat="server">
etc.
</asp:MultiView>
I want to include a button on the asp.net page to cancel the auto refresh.
I tried to include the below but when I clicked the button, it didn't work. The below is the Code Behind for an OnClick event for a Button. The asp.net code is in the above code.
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Interval = 0;
}
Where am I going wrong? Is this even a way to do this or do I need to go another route in order to allow the user to cancel the auto page refresh?
Thanks to PeterJ I have found the solution. I modified the code and since I clicked it the page has not refreshed. The issue was with my code behind for the button OnClick event. I had:
Timer1.Interval = 0;
When I should have had:
Timer1.Enabled = false;
All I wish to do is simply click a button and the text in a textbox is automatically added as an item in the listbox. Shouldn't this be straight forward? Whilst debugging, the item is added and I can see the text by watching ListBox1.Items[0], but nothing is displayed in the web page. I had the same problem which i did not solve, in a console application! Can some one please guide me to what I am doing wrong?
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem(TextBox1.Text));
}
Many thanks
Edit:
In a past project, I used the DataSource property, which worked perfectly. I have never yet managed to use the add Items! May be there is some sort of refresh or update?
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="295px"></asp:ListBox>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
Looks like your listbox is outside of the update panel. Pop it inside the update panel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="AddItem" />
</ContentTemplate>
</asp:UpdatePanel>
You have to move the ListBox into the UpdatePanel, otherwise it will not be updated.
The reason for that is, that ASP.NET is sending the whole HTML of the UpdatePanel back to the client. Since the ListBox is not part of the UpdatePanel, it won't be updated.