而
Response.Write("<script language=javascript>alert('d');</script>");
是弹出提示框,而且必须是在.cs页面,
在html页面中的
<script runat="server">
Response.Write("<script language=javascript>alert('d');</script>");
</script>
报错:Newline in constant
在html页面中
即便是后台有cs文件也不会触发Page_Load事件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="temp" %>
<script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
..........................
}
</script>
如果把上述方法放进后台cs文件
page_load()
{
}
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
..........................
}
page_load是空的,所以页面就呈现出来的是空的。
可以在Page_Load事件里面写:
this.CustomersGridView.DataSource = this.CustomersSqlDataSource;
this.CustomersGridView.DataBind();
这样Page_Load 时就有内容了。
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="temp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>GridView</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<!-- Populate the Columns collection declaratively. -->
<asp:GridView id="CustomersGridView"
AutoGenerateColumns="false"
OnRowCommand="CustomersGridView_RowCommand"
runat="server">
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Select" HeaderText="Select Customer"
Text="Select"/>
<asp:BoundField DataField="CompanyName"
HeaderText="Company Name"/>
<asp:BoundField DataField="ContactName"
HeaderText="Contact Name"/>
</Columns>
</asp:GridView>
<asp:sqldatasource
id="CustomersSqlDataSource"
selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>GridView</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<!-- Populate the Columns collection declaratively. -->
<asp:GridView id="CustomersGridView"
AutoGenerateColumns="false"
OnRowCommand="CustomersGridView_RowCommand"
runat="server">
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Select" HeaderText="Select Customer"
Text="Select"/>
<asp:BoundField DataField="CompanyName"
HeaderText="Company Name"/>
<asp:BoundField DataField="ContactName"
HeaderText="Contact Name"/>
</Columns>
</asp:GridView>
<asp:sqldatasource
id="CustomersSqlDataSource"
selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
cs页面:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class temp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CustomersGridView.DataSource = this.CustomersSqlDataSource;
this.CustomersGridView.DataBind();
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);//Get the Argument for the Command
Response.Write("<script language=javascript>alert('" + index + "');</script>");
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected author.
Message.Text = "You selected " + contact + ".";
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class temp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CustomersGridView.DataSource = this.CustomersSqlDataSource;
this.CustomersGridView.DataBind();
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);//Get the Argument for the Command
Response.Write("<script language=javascript>alert('" + index + "');</script>");
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected author.
Message.Text = "You selected " + contact + ".";
}
}
}