I have a very simple user control like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="tebimir.sections.WebUserControl1" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And I have a page ajax.aspx like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ajax.aspx.cs" Inherits="tebimir.ajax" %>
<%# Register Src="~/sections/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<uc1:WebUserControl1 runat="server" ID="WebUserControl1" />
</div>
</form>
</body>
</html>
And this is Button1 click code:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
I want to update my label text to current date time without refreshing page but every time i click on button page is refreshed. why?
Do you have any other triggers in your user control because I don't see a closing tag for </asp:UpdatePanel>.I've created a brand new ASP.NET web forms web application and created the following user control and it works just fine(without refresh):
User control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="StackOverflowClean.WebUserControl1" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Page that calls the user control:
<%# Register Src="~/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<uc1:WebUserControl1 runat="server" id="WebUserControl1" />
</div>
</form>
</body>
</html>
Related
I have a very simple application. I am trying to upload the file using File upload control of ASP.net. Below is my entire .cs code and .aspx code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestFileUpload1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), uploadedFile.FileName));
//listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
}
}
}
}
}
My .aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test web site</title>
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>
</form>
</body>
</html>
whenever I try to upload a file, I get "False" for UploadImages.HasFiles.
Above is full working example.
As soon as I remove one of these script tags :
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
my code starts working and I get "true" for UploadImages.HasFiles when I try to upload a file.
I am using .net framework 4.7.2
I need to keep these two script tags in my code because of the GUI and this is an old application where these tags are used in all the pages.
I also tried to wrap the control in a update panel and that didn't work either. below is the changed .aspx page. although, I want my original code to work. I don't want to use ajax, but I just tried to use it because it is suggested as one of the solution
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>
<%--<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>--%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test web site</title>
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="uploadedFile" />
</Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>
</form>
</body>
</html>
below is the image of false value that I am getting in code behind:
Any help will be highly appreciated.
All I had to do is put data-ajax ="false" in form tag and that fixed the issue.
<form id="form1" runat="server" data-ajax ="false">
Use a update panel and wrap your controls inside it. Then add the button controlID as a trigger (be sure its a PostBackTrigger) to the update panel. To test be sure to put a break point on the uploadFile_Click event so you can step through and see the values..
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="btnSignaureOfRequestor" />
</Triggers>
<ContentTemplate>
<table>
<tr> <td class="tdText" colspan="4">
<asp:FileUpload ID="fileUpload" runat="server" Width="50%" />
</td>
</tr>
<tr>
<td> <asp:Button ID="btnSignaureOfRequestor" runat="server" Text="Submit Request" Visible="true" OnClientClick="return confirm('Are you sure you want to continue?');" OnClick="btnSignaureOfRequestor_Click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Try the below, removed allow multiple and add update mode conditional (see confirm case/syntax of these changes as I'm just writing it in notepad)
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="UploadImages" />
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="uploadedFile" />
</Triggers>
</asp:UpdatePanel>
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>
I have moved to visual studio 4.5 and at first my update panels worked just fine but one day they just seemed to stop altogether even if I start a new project.
Code Behind:
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = "Post Back";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Testl.Text = DateTime.Now.ToString();
UpdatePanel1.Update();
}
}
Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Label1" runat="server" ></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="Testl"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
even this code causes a post back and I cannot for the life of me figure out why. Here are my references in case I may be missing one I do not realize.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
I have it running in a scriptmanager in my masterpage.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="UFODataRepositoryWebApplication.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
<meta charset="utf-8" />
<title></title>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server" id="MainForm">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<header>
<div class="content-wrapper">
<div class="float-right">
<div id="MainLogin_Div">
<div class="fb-login-button" data-show-faces="true" data- width="400" data-max-rows="1"></div>
<asp:UpdatePanel ID="MainLogin_UpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Panel ID="UnLoggedMain_Panel" runat="server" Visible="true">
<p>
<asp:LinkButton runat="server" PostBackUrl="Login.aspx" CssClass="NotLoggedMenu"
ID="MainLogin_Link">Login</asp:LinkButton>
<span style="color: #7E7F7F;">or </span>
<asp:LinkButton runat="server" PostBackUrl="Register.aspx" CssClass="NotLoggedMenu"
ID="MainRegister_Link">Become a member</asp:LinkButton>
</p>
</asp:Panel>
<asp:Panel ID="LoggedMain_Panel" ClientIDMode="Static" runat="server" Visible="false">
<span style="color: #CACCCB;">Welcome: </span>
<asp:LinkButton ID="Welcome_LinkButton" ClientIDMode="Static" runat="server" PostBackUrl="~/UserPanel.aspx"></asp:LinkButton>
<asp:LinkButton ID="MainLogout_LinkButton" runat="server" OnClick="MainLogout_User"> Logout</asp:LinkButton>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ul>
</div>
</div>
</header>
<div id="body">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</div>
</form>
</body>
</html>
So that should be literally all the code I have. I intentionally took out the inherits at the top of the page for this post.
I have a RadioButtonList with AutoPostBack=true that is within a Repeater. The entire Repeater is surrounded by an Update Panel. However, the RadioButtonList is causing a full postback to occur.
I have tried other scenarios where the UpdatePanel is also within the repeater as well and still get the same issue.
The following code shows the issue.
The first Radio Button is outside of an Update Panel and should cause a full postback. This works correctly.
The second Radio Button is within its own Update Panel and should cause a partial postback. This works correctly.
The third Radio Button is within a Repeater that is within an Update Panel. This should cause a partial postback, however, it is causing a full postback.
The fourth Radio Button is within an Update Panel that is within a Repeater. This should also cause a prtial postback, however, it is causing a full postback.
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptTest1.DataSource = Enumerable.Range(1, 1);
rptTest1.DataBind();
rptTest2.DataSource = Enumerable.Range(1, 1);
rptTest2.DataBind();
}
}
</script>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="script1" EnablePartialRendering="true" runat="server" />
Date1: <%=System.DateTime.Now.ToString() %>
<asp:RadioButtonList ID="rbl1" AutoPostBack="true" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:RadioButtonList>
<br /><br />
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
Date2: <%=System.DateTime.Now.ToString() %>
<asp:RadioButtonList ID="rbl2" AutoPostBack="true" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:RadioButtonList>
<br /><br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" id="upd1" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rptTest1" runat="server">
<ItemTemplate>
Date3: <%=System.DateTime.Now.ToString() %>
<asp:RadioButtonList ID="rbl3" AutoPostBack="true" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:RadioButtonList>
<br /><br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Repeater ID="rptTest2" runat="server">
<ItemTemplate>
<asp:UpdatePanel runat="server" id="upd2" UpdateMode="Conditional">
<ContentTemplate>
Date4: <%=System.DateTime.Now.ToString() %>
<asp:RadioButtonList ID="rbl4" AutoPostBack="true" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:RadioButtonList>
<br /><br />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
I need to find a way of having an AutoPostBack RadioButton within a repeater that does NOT cause a full postback as the page itself is large and the resulting postback causes a large flicker to the end user.
I am using ModalPopupExtender from AjaxToolkit for asp.net. I am trying to have a ModalPopupExtender triggered with different buttons. The problem is that unless I am using the TargetControlID the popup opens and quickly closes within under a second. I need this popup to be accessible
by several different buttons with the same panel being used everytime.
The code below should replicate the problem nicely, on my actual application it almost works fine. Even content being updated with the chosen panel of the popup except that it closes after about 1/2 sec when i call .show() from OnClientClick;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
//Function to Hide ModalPopUp
function Hidepopup() {
$find('AjaxPopupHi').hide();
}
//Function to Show ModalPopUp
function Showpopup() {
$find('AjaxPopupHi').show();
}
</script>
</head>
<form id="form1" runat="server">
<asp:LinkButton ID="lnk" OnClientClick = "Showpopup()" runat="server" Text="hi"></asp:LinkButton>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:Button ID="Button_dummy" Style="display: none" runat="server" Text="Button" />
<ajaxToolKit:ModalPopupExtender ID="mpe" runat="server" BehaviorID="AjaxPopupHi" TargetControlID="Button_dummy" PopupControlID="pnl"
CancelControlID="close" />
<!--BELOW panel does not remain OPEN :/-->
<asp:Panel ID="pnl" runat="server" CssClass="popupPanel">
<div>
Hi!!!
</div>
<asp:Button ID="close" runat="server" Text="Close" />
</asp:Panel>
</form>
Thanks
You can use like this
OnClientClick = "return Showpopup()"
function Showpopup() {
$find('AjaxPopupHi').show();
return false;
}
You must use return in you OnClientClick combining your code
<asp:LinkButton ID="lnk" OnClientClick = "return Showpopup()" runat="server" Text="hi">
</asp:LinkButton>
and your javascript function should be like
function Showpopup() {
$find('AjaxPopupHi').show();
return false;
}
It is very late but lets hope this helps someone who is looking for the answer.
When you click, a postback occurs and the page reloads. If you use updatepanel then your popup message will remain after page load. See this example below:
Aspx :
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Popup.aspx.cs" Inherits="Popup_example_Popup" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
//Function to Hide ModalPopUp
function Hidepopup() {
$find('AjaxPopupHi').hide();
}
//Function to Show ModalPopUp
function Showpopup() {
$find('AjaxPopupHi').show();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="udpOutterUpdatePanel" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lnk" OnClientClick="Showpopup()" runat="server" Text="hi"></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="Button_dummy" Style="display: none" runat="server" Text="Button" />
<ajaxToolkit:ModalPopupExtender ID="mpe" runat="server" BehaviorID="AjaxPopupHi" TargetControlID="Button_dummy" PopupControlID="pnl"
CancelControlID="close" />
<asp:Panel ID="pnl" runat="server" CssClass="popupPanel">
<div>
Hi!!!
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="close" runat="server" Text="Close" OnClick="close_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</form>
</body>
</html>
Code behind :
using System;
public partial class Popup_example_Popup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void close_Click(object sender, EventArgs e)
{
mpe.Hide();
}
}
This is the code I use for my UpdatePanel.
The FileUploadAsync.HasFile() is always null.
I am wondering what is wrong with my asp.net page ...
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
<asp:UpdatePanel ID="UpdatePanelAddFiles" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LkUpload" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="LkUpload" runat="server" OnClick="LkUpload_Click" Visible="false">Upload</asp:LinkButton>
<ajaxToolkit:AsyncFileUpload runat="server" ID="FileUploadAsync" />
</ContentTemplate>
</asp:UpdatePanel>
Have you tried taking the fileupload outside the update panel? I've had that problem before.
I got this to work, try it out and let me know.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="futest.aspx.cs" Inherits="erpweb.futest" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="smTest" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upTest" runat="server">
<ContentTemplate>
<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" runat="server"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
codebehind:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.HasFile)
{
//do save process here
}
}