错误-GridView的gvdetails必须在带有runnat = Server的Form标记内 [英] Error - gvdetails of GridView must be inside of Form tag with runnat = Server
本文介绍了错误-GridView的gvdetails必须在带有runnat = Server的Form标记内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么错误将int行gvdetails.RenderControl(htw)赋予int? AND告诉GridView的gvdetails必须在带有runnat = Server
的Form标记内
下面给出的HTML代码
Why error is giving int the line gvdetails.RenderControl(htw)? AND tell gvdetails of GridView must be inside of Form tag with runnat = Server
HTML CODE GIVEN BELOW
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Export Data from Grid to Word and Excel.aspx.cs" Inherits="Export_Data_from_Grid_to_Word_and_Excel" %>
<!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 align="center">
<br />
<br />
<asp:ImageButton ID="btWord" runat="server" Width="98px"
ImageUrl="~/word_icon[1].png" Height="93px" onclick="ImageButton1_Click" />
<asp:ImageButton ID="btExcel" runat="server" Width="119px"
ImageUrl="~/excel[1].gif" Height="108px" onclick="ImageButton2_Click" />
<asp:GridView ID="gvdetails" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="qid"
DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
Width="260px">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="qid" HeaderText="qid" SortExpression="qid"
ReadOnly="True" />
<asp:BoundField DataField="qname" HeaderText="qname"
SortExpression="qname" />
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
<asp:BoundField DataField="aid" HeaderText="aid" SortExpression="aid" />
<asp:BoundField DataField="cid" HeaderText="cid" SortExpression="cid" />
<asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tuitionConnectionString %>"
SelectCommand="SELECT * FROM [question]"></asp:SqlDataSource>
<br />
</div>
</form>
</body>
</html>
请参阅下面的我的CS代码
SEE MY CS CODE IS GIVEN BELOW FOR THIS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Export_Data_from_Grid_to_Word_and_Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
gvdetails.AllowPaging = false;
gvdetails.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
foreach (GridViewRow gvrow in gvdetails.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvdetails.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
推荐答案
ConnectionStrings:tuitionConnectionString %> " =" 选择*来自[问题]" < /asp:SqlDataSource > < br > < /div > < /form > < /body > < /html >
ConnectionStrings:tuitionConnectionString %>" SelectCommand="SELECT * FROM [question]"></asp:SqlDataSource> <br /> </div> </form> </body> </html>
请参阅下面的我的CS代码
SEE MY CS CODE IS GIVEN BELOW FOR THIS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Export_Data_from_Grid_to_Word_and_Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
gvdetails.AllowPaging = false;
gvdetails.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
foreach (GridViewRow gvrow in gvdetails.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvdetails.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
使用下面的代码,
Use the code below,
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ConvertXL(Response);
}
public void ConvertXL(HttpResponse Response)
{
try
{
gvdetails.SelectedIndex = -1;
Response.Clear();
HtmlForm form = new HtmlForm();
string attachment = "attachment;filename=UserLogDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.xls";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gvdetails);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
catch (Exception ex)
{
return;
}
}
并导出到Word中使用
And for export to Word use
Response.ContentType = "application/ms-word";
这篇关于错误-GridView的gvdetails必须在带有runnat = Server的Form标记内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文