TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Aseem Bhardwaj
1.6k
114
20.3k
gridview2 under form tag
Oct 26 2016 4:37 AM
i am using master page with form tag runat server
but on my dedicated page to master page, i am using two gridviews to get data by corresponding click from grid1 and get value in grid2.
i am using itextsharp for save file in pdf from grid2.
the problem is when i run the command on browser
it shows error that is gridview2 should be under form tag. that i already given in the master page. how would i get the optimal solution for this.
what i have treied as follows:-
HERE IS MY CS CODE:-
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.IO;
using System.Text;
public partial class list : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ledgerConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
//userLabel.Text = "<b><font color=Yellow>" + "</font>" + "<b><font color=yellow>" + Session["user"] + "</font>";
userLabel.Text = Session["user"].ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
base.VerifyRenderingInServerForm(control);
}
protected void button1_Click(object sender, EventArgs e)
{
string search = searchtext.Text;
ViewState["searchvals"] = search;
getdata(search);
GridView2.DataSource = null;
GridView2.DataBind();
}
public void getdata(string search)
{
con.Open();
SqlCommand cmd = new SqlCommand("select pk_id, ma_accname from master where ma_accname LIKE '%' + @search + '%' and userId=" + Convert.ToInt32(Session["user"]) + "", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@search", search);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
con.Open();
cmd = new SqlCommand();
cmd.CommandText = "sp_getDataML";
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@gl_acid", GridView1.SelectedValue);
cmd.Parameters.AddWithValue("@userid", Convert.ToInt32(Session["user"]));
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridPageIndex1(object sender, GridViewPageEventArgs e)
{
/*Fill Gridview*/
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
getdata(ViewState["searchvals"].ToString());
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "view")
{
int id = Convert.ToInt32(e.CommandArgument);
ViewState["id"] = id;
viewdetaildata();
}
}
public void viewdetaildata()
{
if (con.State == ConnectionState.Closed)
con.Open();
cmd = new SqlCommand();
cmd.CommandText = "sp_getDataML";
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@gl_acid", Convert.ToInt32(ViewState["id"]));
cmd.Parameters.AddWithValue("@userid", Convert.ToInt32(Session["user"]));
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
protected void GridView2_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
GridView2.DataBind();
}
protected void ExportToPDF(object sender, EventArgs e)
{
exportGridToPdf();
}
private void exportGridToPdf()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-dispostion", "attachment; filename=grid.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView2.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
GridView2.AllowPaging = true;
GridView2.DataBind();
}
}
HERE IS MY MAIN ASPX:-
enter code here
<%@ Page Title="" Language="C#" MasterPageFile="~/logIn/login.master" AutoEventWireup="true" CodeFile="list.aspx.cs" Inherits="list" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript">
function PrintGridData() {
var prtGrid = document.getElementById('<%=GridView2.ClientID %>');
prtGrid.border = 0;
var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>
<center>
<div style="width: 50%; height: 5%; border-bottom-color: black">
<h4 style="font-size: xx-large; text-align: center; color:yellow">hello, welcome
<asp:Label ID="userLabel" runat="server" Text="Label"></asp:Label></h4>
</div>
</center>
<div style="text-align: center; vertical-align: bottom; width: 100%">
<asp:TextBox runat="server" ID="searchtext" placeholder="Search"></asp:TextBox>
<asp:Button Text="Button" runat="server" ID="button1" OnClick="button1_Click" />
</div>
<div style="align-items: center; margin-top: 2%;">
<center>
<asp:GridView ID="GridView1" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True" PageSize="3" OnPageIndexChanging="GridPageIndex1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="View Details">
<ItemTemplate>
<asp:Button ID="btn2" runat="server" CommandName="view" CommandArgument='<%# Eval ("pk_id") %>' Text="View Details"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
<asp:Label runat="server" ID="nameLabel"></asp:Label>
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:TextBox ID="dateFromText" runat="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="dateFrom" runat="server" PopupButtonID="dfPopUp" TargetControlID="dateFromText" />
<asp:TextBox ID="dateToText" runat="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="dateTo" runat="server" PopupButtonID="dtPopup" TargetControlID="dateToText" />
<center>
<asp:GridView ID="GridView2" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" RowStyle-HorizontalAlign="Center" AllowPaging="true" OnPageIndexChanging="GridView2_PageIndexChanging1" PageSize="5" HorizontalAlign="Center">
</asp:GridView>
</center>
<input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" />
<asp:Button ID="btnExport" runat="server" Text="Get PDF" OnClick="ExportToPDF" />
</div>
</asp:Content>
HERE IS MY MASTER PAGE:-
enter code here
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="login.master.cs" Inherits="logIn_login" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Decent Ledger</title>
<!--Custom Theme files-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Register Login Widget template Responsive, Login form web template,Flat Pricing tables,Flat Drop downs Sign up Web Templates, Flat Web Templates, Login Signup Responsive web template, SmartPhone Compatible web template, free WebDesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<!-- Custom Theme files -->
<link href="../Contents/assets/css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--web-fonts-->
<link href='//fonts.googleapis.com/css?family=Jura:400,300,500,600' rel='stylesheet' type='text/css'>
<!--//web-fonts-->
<link href="../Contents/assets/css/gridCss.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Reply
Answers (
2
)
Telerik: Discard newly added unsaved row on sort grid column
Node.js Add to existing mvc Dont net Project