I have this problem:
Add in UpdatePanel a TextBox with RequiredFieldValidator.
Then at the Postback add values to DB and redirect to an other Page.
My problem is why when I add one MyControl and click GO button, ValidationSummary don't stop the postback, and why at the second click the ValidationSummary fire?
The second problem is why VisualStudio don't find MyControl class(Underline it with red) but when I compile result is no error?
This is the code:
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Validators</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upAdd" runat="server">
<ContentTemplate>
<asp:Button ID="cmdAdd" runat="server" Text="Add" onclick="cmdAdd_Click" CausesValidation="false" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:UpdatePanel ID="upValidators" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="phValidators" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmdAdd" />
</Triggers>
</asp:UpdatePanel>
<hr />
<asp:Button ID="cmdGo" runat="server" Text="Go" onclick="cmdGo_Click" />
</div>
<asp:ValidationSummary ID="valSum" HeaderText="Errors:" ShowSummary="False" ShowMessageBox="True" EnableClientScript="True" DisplayMode="BulletList" runat="server"></asp:ValidationSummary>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
ViewState["ItemsAdded"] = m_oItems;
foreach (int m_oItem in m_oItems)
{
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
m_oItems.Add(m_oItems.Count);
ViewState["ItemsAdded"] = m_oItems;
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
protected void cmdGo_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
//Add To DB
Response.Redirect("http://www.google.it/");
}
}
MyControl.ascx
<asp:TextBox ID="txtLanguage" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqrLanguage" runat="server" ErrorMessage="Language not set" ControlToValidate="txtLanguage"></asp:RequiredFieldValidator>
<br />
Thank you very much
Damiano
Related
When using UpdatePanel, the AsyncPostBackTrigger is not firing on the server side. Originally, I thought it was because I'm using a MasterPage and the ScriptManager was not loading correctly so I created a test template but it still won't fire. However, the UpdateProgress does appear. This button is being used to export a report from Crystal Reports. Below is my following code:
Client Side:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<%# Register Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script src="cr/crystalreportviewers13/js/crviewer/crv.js" type="text/javascript">
</script>
<script>
</script>
<form id="form1" runat="server">
<div>
<p>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
</p>
</div>
<div>
<asp:Label ID="test" runat="server"></asp:Label>
<asp:RadioButtonList ID="rblFormat" runat="server" RepeatDirection="Horizontal" Width="130px" >
<asp:ListItem Text=" Excel" Value="Excel" />
<asp:ListItem Text=" PDF" Value="PDF" />
</asp:RadioButtonList>
<asp:ScriptManager runat="server" ></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="btnExport" CssClass="btn btn-primary" Text="Export" runat="server" CausesValidation="false"
OnClick="btnExport_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnExport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div>
<font color="red"> Generating Download</font>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</form>
</body>
</html>
Server-Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using static System.Net.Mime.MediaTypeNames;
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
ReportDocument crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.CrystalReportViewer1);
scriptManager.RegisterPostBackControl(this.btnExport);
crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("cr/missing_dob.rpt"));
crystalReport.SetDatabaseLogon("test", "test");
CrystalReportViewer1.ToolPanelView = ToolPanelViewType.None;
CrystalReportViewer1.ReportSource = crystalReport;
}
protected void btnExport_Click(object sender, EventArgs e)
{
ExportFormatType formatType = ExportFormatType.NoFormat;
switch (rblFormat.SelectedItem.Value)
{
case "PDF":
formatType = ExportFormatType.PortableDocFormat;
break;
case "Excel":
formatType = ExportFormatType.Excel;
break;
}
crystalReport.ExportToHttpResponse(formatType, Response, true, "Report");
}
}
}
PostBackTrigger works but the UpdateProcess does not appear. I can't seem to get both working. I appreciate any help!
I am writting a program (web site) which uploads images on the server,displays them and saves them in a folder. I want to add a progress bar which will be connected to the image's file size while it is being filled.Here is my Default.aspx file
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<br />
<br />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<output id="images"></output>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gvPhotos" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField ="Text" HeaderText="filename" />
<asp:ImageField DataImageUrlField="Value" ControlStyle-Height="250" ControlStyle-Width="250" HeaderText="Photo" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Loading....<br />
<br />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<p>
<asp:Label ID="lblSuccess" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
This is Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<ListItem> sia = new List<ListItem>();
if(FileUpload1.HasFiles)
{
foreach(var file in FileUpload1.PostedFiles)
{ string[] fileName = Directory.GetFiles(Server.MapPath("~/Images/"));
file.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), file.FileName));
Response.Write(file.FileName + " - " + file.ContentLength + " Bytes. <br />");
sia.Add(new ListItem(file.FileName, "~/Images/" + file.FileName));
}
lblSuccess.Text = string.Format("{0} files have been uploaded successfully.", FileUpload1.PostedFiles.Count);
}
gvPhotos.DataSource = sia;
gvPhotos.DataBind();
}
}
Please help me in inserting a jquery bar in my Default.aspx. Thank you in advance
I need to redirect page using a button within a update panel. but when I try this It didn't work for me. can anyone help me on this??/. Please Find My Code Below.
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label" Width="345px"></asp:Label><br />
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="500">
</asp:Timer>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="344px" OnClick="Button1_Click" /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
<br />
<br />
</div>
</form>
</body>
My Code Behind Code
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
public void imgBtn_Click(object sender, EventArgs e)
{
Response.Redirect("~/HotelResult.aspx");
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/book-btn.png";
PlaceHolder1.Controls.Add(testbtn);
}
}
The problem here is that ImageButton is added to the page only after timer tick. If you want server-side handlers to be invoked for such dynamically added controls, you have to added the to the page on every postback. Here is how it can be done using key view state:
protected void Page_Load(object sender, EventArgs ea)
{
bool? addHotelResultButton = ViewState["AddHotelResultButton"] as bool?;
if (addHotelResultButton.HasValue && addHotelResultButton.Value)
{
AddHotelResultButton();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
ViewState["AddHotelResultButton"] = true;
AddHotelResultButton();
}
protected void AddHotelResultButton()
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/heroAccent.png";
PlaceHolder1.Controls.Add(testbtn);
}
I would do it like this :
Put your button inside your updatePanel and disable it
Put it as a trigger inside the <triggers> tag
When the tick event come, enable your button
I have an UpdatePanel with a GridView. This GridView has a template column that is a textbox. The problem is that the textchange event does not fire for the textbox.
Where am I wrong?
Here is the code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="updatepanelgridview.WebForm1" %>
<!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="scrManager" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updPnl" runat="server">
<ContentTemplate>
<asp:GridView ID="grdNumber" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtNumber" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlNumber" runat="server" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
<asp:ListItem>For</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
And here is the codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace updatepanelgridview
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Rows.Add();
dt.Rows.Add();
dt.Rows.Add();
dt.Rows.Add();
grdNumber.DataSource = dt;
grdNumber.DataBind();
}
}
protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Here i use dropdownlist in second column which works proper but my textbox textchange event not getting fir please suggest what i am doing wrong?
Set the update mode property of update panel to Always.
<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">
In the code behind file under TextBox1_TextChanged event, you can get the latest value using sender.text property.
If you set updateMode to conditional, in that case you need to add Triggers.
--------------------- This is the code I tried and its working------------------
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:ScriptManager ID="scrManager" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updPnl" runat="server" >
<ContentTemplate>
<asp:GridView ID="grdNumber" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtNumber" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlNumber" runat="server" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
<asp:ListItem>For</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Rows.Add();
dt.Rows.Add();
dt.Rows.Add();
dt.Rows.Add();
grdNumber.DataSource = dt;
grdNumber.DataBind();
}
}
protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
var value = (sender as TextBox).Text;
}
}
I'm using this code in a page:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="timer" Interval="4000" runat="server" OnTick="timer_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlAlarm" runat="server" CssClass="pnlAlarm" ClientIDMode="Static">
<div id="Alarm">
<asp:Label ID="lblContent" runat="server" Text="Updating" CssClass="AlarmLogo"></asp:Label>
ClientIDMode="Static" />
</div>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer" />
</Triggers>
</asp:UpdatePanel>
and in code behind I use this simple code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["nima"] = 1;
}
}
protected void timer_Tick(object sender, EventArgs e)
{
int i = int.Parse(Session["nima"].ToString());
if (i==3)
{
lblContent.Text = i.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), "AlarmMessage", "$('#pnlAlarm').slideToggle();", true);
Session["nima"] = 0;
}
else
{
i = i + 1;
Session["nima"] = i;
}
}
I want to know every time that I use RegisterStartupScript , $('#pnlAlarm').slideToggle(); add to my page and increase my page size?
thanlks
By definition, that method will:
register a startup script block that is included every time that an asynchronous postback occurs.
So yes, it will be included, and therefore increase your page size.
msdn ScriptManager.RegisterStartupScript Method