Gridview missing After button clicked - c#

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.

Related

ASP.NET AsyncPostBackTrigger Not Firing in UpdatePanel

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!

gridview button doesn't redirect in asp.net

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");
}
}
}

How to download pdf file using asp.net?

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") %>'

It is possible to use DataTables Column Filter Add-on with a Gridview

I am trying to use the DataTables Column Filter Add-on on a gridview table using an sql datasource but i am not coming up with any luck. I have the gridview render a header and footer but i can not add anything to the footer.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Glossary.aspx.cs" Inherits="Home.Glossary" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title spellcheck="true">Glossary</title>
<style type="text/css" media="all">
#import "DataTables-1.9.4/DataTables-1.9.4/media/css/jquery.dataTables_themeroller.css";
#form1 {
width: 100%;
}
</style>
<script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.js"></script>
<script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
<script src="JQuery-DataTables-ColumnFilter/media/js/jquery.dataTables.columnFilter.js"></script>
<script>
$(document).ready(function () {
$('#<%=GridView1.ClientID%>').dataTable().columnFilter();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="background: #A0A0A0">
</div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="TermText,DefNbr,DefVerNbr" DataSourceID="TedGlossary"
EnableSortingAndPagingCallbacks="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowFooter="true"
AutoGenerateColumns="False" AutoGenerateEditButton="False" style="margin-right: 0px" Width="100%" Height="382px">
<Columns>
<asp:BoundField DataField="TermText" HeaderText="Term" ReadOnly="True" SortExpression="Term" />
<asp:BoundField DataField="DefNbr" HeaderText="Number" ReadOnly="True" SortExpression="DefinitionNumber" />
<asp:BoundField DataField="DefVerNbr" HeaderText="Version" ReadOnly="True" SortExpression="DefinitinonVersionNumber" />
<asp:BoundField DataField="DefText" HeaderText="Definition" SortExpression="Definition" />
<asp:BoundField DataField="AmplifyingExplanationText" HeaderText="Amplifying Explanation" SortExpression="AmplifyingExplanationText" />
<asp:BoundField DataField="SeeAlsoText" HeaderText="See Also" SortExpression="See Also" />
<asp:BoundField DataField="AuthoritativeSrcText" HeaderText="Authoritative Source" SortExpression="AuthoritativeSrcText" />
<asp:BoundField DataField="ScopeName" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="DomnName" HeaderText="Domain" SortExpression="Domain" />
<asp:BoundField DataField="GovernanceStateName" HeaderText="Governance State" SortExpression="GovernanceStateName" />
<asp:BoundField DataField="LastUpdtTimestamp" HeaderText="Last Updated" SortExpression="LastUpdtTimestamp" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="TedGlossary" runat="server" ConnectionString="<%$ ConnectionStrings:Glsry_Taylor %>" SelectCommand="SELECT * FROM [Glossary] ORDER BY [TermText]"></asp:SqlDataSource>
</form>
</body>
</html>
Here is the CS for the rendering
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Home
{
public partial class Glossary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.PreRender += new EventHandler(GridView1_PreRender);
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
//forces grid to render thead/tbody/tfoot elements
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Here is the HTML output, there is too much for the site to handle i put in pastebin the link is included
http://pastebin.com/2qh2eXWY
In order to add anything to the footer, you need to use TemplateField controls rather than BoundField controls. See http://www.dreamincode.net/forums/topic/222381-insert-data-using-gridview-footerrow/ for an example of how to do this. One thing to note though, the HTML generated by the GridView control will use <td> elements in the footer rather than <th> elements as expected by the DataTables documentation.

Textbox Inside GridView Inside UpdatePanel textchange Event not firing

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;
}
}

Categories

Resources