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;
}
}
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!
On page Load I will load empty gridview with header. On button clicked I dont want to be gone.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Portfolio.aspx.cs" Inherits="PortFolio.Portfolio" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true">
<Columns>
<asp:BoundField DataField="Parent" HeaderText="Parent" HeaderStyle-Width="175px" />
<asp:BoundField DataField="Child" HeaderText="Child" HeaderStyle-Width="175px" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckboxSelectAll" onclick="HeaderCheckBoxClick(this);" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="ChildCheckBoxClick(this);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PortFolio
{
public partial class Portfolio : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = new List<string>();
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
I'm using (!IsPostBack) because I dont want to always load my data on Page_Load because my gridview has checkbox that will be missing state if I always load on page load. I'm thinking of using viewstate, but I dont know how.
Weird thing is, if I run this locally the gridview not missing, after publish to https the gridview missing. Please help
If you have set EnableViewState to "false", then you need to set it to "true" at the page level.
I have a button inside gridview and i am trying to redirect it to a page.
The following is my code for the template view.
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnView" runat="server" CausesValidation="false" OnClick="btnView_Click" Text="View" Font-Size="Small"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
I tried the following solution for the OnClick.
protected void btnView_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Response.Redirect("Customer.aspx" );
}
My code isn't working fro some reason and the button doesn't redirect. I looked at other solutions as well but I they are not working for me. Can someone please let me know what I am missing here. Thank you.
Code working at my end. Try checking page load event in server side. If there are, then perhaps by adding IsPostBack will help. Refer to this post.
Aspx Page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="tempform.aspx.cs" Inherits="PivotTest.tempform" %>
<!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>temp test Page</title>
</head>
<body>
<form id="random" runat="server">
<asp:GridView ID="demo" runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="temp" DataField="Col1" />
<asp:TemplateField HeaderText="Run">
<ItemTemplate>
<asp:Button ID="btnView" runat="server" CausesValidation="false" OnClick="btnView_Click" Text="View" Font-Size="Small"></asp:Button>
</ItemTemplate>
<HeaderStyle Width="88px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Aspx.cs Page:
using Samples.SampleData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PivotTest
{
public partial class tempform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
demo.DataSource = new List<Temp>() {
new Temp() { Col1="1"},
new Temp() { Col1="1"},
new Temp() { Col1="1"},
new Temp() { Col1="1"},
};
demo.DataBind();
}
}
protected void btnView_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Response.Redirect("Customer.aspx");
}
}
}
I upload file and stored in Data folder in server
File path:
Project Name
|_bin
|_css
|_Data
|_Mohamedfaisal.pdf
this is my asp.net code
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.Data;
namespace Expatriates {
public partial class FileUploadForm: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e) {
if (FileUpload1.HasFile) {
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
}
DataTable dt = new DataTable();
dt.Columns.Add("File", typeof(string));
dt.Columns.Add("size", typeof(string));
dt.Columns.Add("type", typeof(string));
foreach(string strFile in Directory.GetFiles(Server.MapPath("~/Data/"))) {
FileInfo fi = new FileInfo(strFile);
dt.Rows.Add(fi.Name, fi.Length, fi.Extension);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Download") {
Response.Clear();
Response.Write(e.CommandArgument);
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument); // error occured
Response.End();
}
}
}
}
Front End asp.net
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadForm.aspx.cs" Inherits="Expatriates.FileUploadForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family:Arial;">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
<asp:BoundField DataField="Type" HeaderText="File Type" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
my front end design
UI design
when I click that link it shows that following error message
An exception of type 'System.IO.DirectoryNotFoundException' occurred
in mscorlib.dll but was not handled in user code
Additional information: Could not find a part of the path 'F:\Visual
Studio Project\Expatriates\Expatriates\Data\'.
Uploading file is working perfectly, But I am not getting a download.
You don't supply a commandArgument for your RowCommand event. Change
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>
to
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'></asp:LinkButton>
The only reason your code could fail would be if e.CommandArgument doesn't have a valid file name. I think the Command Argument isn't being passed for some reason, please look into your markup.
You have to explicitly specify CommandArgument for a LinkButton like this:
CommandArgument='<%# Eval("File") %>'
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