I am trying to export gridview to excel / pdf / word format
I have added all necessary implementation but still it gives me an error like "control of type 'gridview' must be placed inside a form tag with runat=server"
Please look at my code below ,
.aspx page
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/OrganizationMaster.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="SearchInvoice.aspx.cs" Inherits="Site.pages.ContactStaff.ViewInvoice" %>
.aspx.cs page
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
.ascx.cs control
protected void btnExport_Click(object sender, EventArgs e)
{
if (ddlExport.SelectedIndex != 0)
{
if (ddlExport.SelectedValue == "Word")
{
Response.AddHeader("content-disposition", "attachment;filename=OtherCourses.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
List<InvoiceHelper> invoicelist = svc.RetriveInvoiceList(Request.Url);
grdinvoicelist.AllowPaging = false;
grdinvoicelist.DataSource = invoicelist;
grdinvoicelist.DataBind();
grdinvoicelist.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
}
}
You might have same problem as I had.
Inside your MasterPage you already have a form tag,
When you do 2nd form tag in your webform.
try using cleanest masterpage as possible such as:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="TotalEmpty.master.cs"
Inherits="ProjectName.MasterPages.TotalEmpty" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
Related
New to C#, ASP.net from PHP and trying to convert my code.
I currently am trying the following code to retrieve the posted data from a form.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs" Inherits="Ass2.CarPage"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<%
If Request.Form["Car"] == "Volvo" then
header('Location:VolvoHomepage.html');End If
If Request.Form["Car"] == "Ford" then
header('Location:FordHomepage.html');End If
If Request.Form["Car"] == "Mercedes" then
header('Location:MercedesHomepage.html');End If
If Request.Form["Car"] == "Audi" then
header('Location:AudiHomepage.html');End If
If Request.Form["Car"] == "Vauxhall" then
header('Location:VauxhallHomepage.html');End If
%>
</body>
</html>
but I keep receiving "Server Error in '/' Application."
Could anyone help please?
This line is saying that you are coding in C# language:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs" Inherits="Ass2.CarPage"%>
But below you are using a mix of Visual Basic and PHP languages:
<% If Request.Form["Car"] == "Volvo" then
header('Location:VolvoHomepage.html');End If
%>
In C# the code above would be:
<% if (Request.Form["Car"] == "Volvo") {
// do your thing
}
%>
However in Web Forms framework you should declare "user controls" on the aspx file and code the "logic" into the aspx.cs file. Your aspx code may look like:
<myUserControls:VolvoHomepage runat="server" id="_ucVolvo" visible="false" />
<myUserControls:FordHomepage runat="server" id="_ucFord" visible="false" />
...
You will copy/paste each code of your html files in the corresponding user control.
And now the Page_Load method (for example) of CarPage.aspx.cs may define if the user controls are visible or not:
protected void Page_Load(object sender, eventargs e) {
if (Request.Form["Car"] == "Volvo") _ucVolvo.Visible = true;
else if (Request.Form["Car"] == "Ford") _ucFord.Visible = true;
}
Is it more clear?
I am trying to add a Javascript to all my pages in the Site. So, I created a base Page and Most of the Pages on the site are inherited from this Page using the <#Reference tag. Here's a Sample of my Base Page
<%# Page Language="C#" CodeBehind="BasePage.aspx.cs" Inherits="MyProj.BasePage" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body ID="BasePage_Body" runat="server">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
In basepage.aspx.cs I am using this as follows..
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//Adding this Client Side script for my Page
if (<someCondition>)
{
BasePage_Body.Attributes.Add("onunload", "HandleClose()");
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "MWGBrowserhandlingClose",
"<script language=\"JavaScript\">" +
"function HandleClose() {" +
"alert(\"Killing the session on the server!!\");" +
"PageMethods.AbandonSession();" +
"} </script>");
}
}
But I get Nullreference Exception for BasePage_Body Any inputs on why is this and how to resolve.
Thanks in advance
i have a default.aspx page:
<%# 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:Button ID="btn" runat="server" Text="clicke here" OnClick="btn_Click"/>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Label runat="server" ID="lbl"></asp:Label>
</div>
</div>
</form>
</body>
</html>
and default.aspx.cs :
public partial class _Default : System.Web.UI.Page
{
string test;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
test = txt.Text;
lbl.Text = test;
}
}
now i want to make the dll of my default.aspx.cs file and remove it from the website and give the reference of it.
so how can i do this??
Right click on Properties. Then go to the Application tab and change output type to class library.
Once the dll is created in bin folder, right click on Reference in your project where you want to add the dll. And then add the reference of the dll.
In this case it would be best to create a server control. You can then use it in any ASP.NET Project.
Here is a walkthrough...
http://msdn.microsoft.com/en-us/library/vstudio/yhzc935f(v=vs.100).aspx
I am using an combobox with some values and AutoPostBack = true, the page does not refresh.
I have a selectedIndexChanged event as well.
I managed to get the selectedValue and I would like to show this in a TextBox.
In the selectedIndexChanged event I did:
textBox1.Text = selectedValue.ToString();
When I inspect this textbox element with Google Chrome I can see the value is set in the TextBox.
But in the browser the value isn't shown, still an empty TextBox.
Do you guys have any clue why this could happen?
Thanks!
How do you populate items for ComboBox? If dynamically via On-Load event then make sure that method that adds items does not run on PostBack.
Here is your working code buddy.
Don't forget ToString method.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs" Inherits="Test2" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<asp:ComboBox ID="ComboBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ComboBox1_SelectedIndexChanged">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
</asp:ComboBox>
<asp:TextBox runat="server" ID="textBox1"/>
</div>
</form>
</body>
</html>
and code
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = ComboBox1.SelectedValue.ToString();
}
}
How to trigger onclick event after received return value from popup aspx which using add attribute a javascript?
After adding
.Attributes.Add("onClick", "return popWin('" + NewBatchNo_TextBox.Text + "');");
Original onclick event do not fire?
if this method can not work, any other method to get value from pop up message box and return value and run click event
Thanks.
try this
Main.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<!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>
<script type='text/javascript'>
function DoStuff() {
document.getElementById('Button1').click();
}
function popWin() {
var popy = window.open('popup.aspx', 'popup_form', 'menubar=no,status=no,top=100%,left=100;')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Show List" />
</div>
</form>
</body>
</html>
Main.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 Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", "return popWin()");
}
}
}
popup.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="popup.aspx.cs" Inherits="popup" %>
<!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>
<script type='text/javascript'>
function validepopupform() {
window.opener.document.getElementById('txtPopupValue').value = document.getElementById('txtPop').value
self.close();
}
window.onbeforeunload = CloseEvent;
function CloseEvent() {
if (window.opener && !window.opener.closed) {
window.opener.DoStuff();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPop" runat="server"></asp:TextBox>
<input type='button' value='go' onclick='validepopupform()' />
</div>
</form>
</body>
</html>
The same problem I faced several times the cause is that java script cannot call code behind(c#/vb function/events).
An alternate way I used is to use hidden fields that is accessible by both java script and code behind. But then you want to call events not read values modified by java script.
For this we refreshed the page through java script when we want to trigger that code behind event and let the page load to monitor the scenario(value that java script set) and triggers the required event.
The code at the end would be real mess and unmanageable, would take lots of efforts to debug.