主要三种方法:Gridview自带的“选择”按钮,模板列生成的“hyperlink”以及“LinkButton”
(1)如何使用ASP.Net Gridview 的hyperlink打开新网页?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="product.aspx?id={0}" DataTextField="ID" HeaderText="详细" Text="详细" />
</Columns>
</asp:GridView>
(2)GridView弹出新页面/弹出制定大小位置新窗口:
方案一:简单的方法,新窗口不固定大小
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:HyperLinkField HeaderText="姓名" Text="姓名" DataNavigateUrlFields="姓名" DataNavigateUrlFormatString="Default6.aspx?GoodsID={0}" Target="mainframe" NavigateUrl="~/Default6.aspx" DataTextField="姓名" >
</asp:HyperLinkField>
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
DataNavigateUrlFields是链接的字段名,DataNavigateUrlFormatString是路径。
软件开发网
方案二:精确控制弹出窗口大小位置
<asp:HyperLinkColumn DataNavigateUrlField="EmployeeID" DataNavigateUrlFormatString="javascript:varwin=window.open('detail.aspx?ID={0}',null,'width=300,height=200');window.Close();"
DataTextField="LastName" HeaderText="LastName"></asp:HyperLinkColumn>
使用的是结合javascript的window.open方法
(3)LinkButton
大多数情况是给按钮CommandName,然后在RowCommand事件中处理
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnUp" runat="server" CommandName="up">Up</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "+")
{
int index = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
//如果是TemplateField
string str = ((Label)(GridView1.Rows[index].FindControl("控件名"))).Text;
//如果是BoundField
string str = (GridView1.Rows[index].Cells[单元格索引].Text;
}
}
HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productid" DataSourceID="SqlDataSource1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="productid" HeaderText="productid" InsertVisible="False"
ReadOnly="True" SortExpression="productid" />
<asp:BoundField DataField="productname" HeaderText="productname" SortExpression="productname" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("productname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" runat="server" CausesValidation="False" CommandName="Select"
Text="选择"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select productid, productname from products"></asp:SqlDataSource>
C# code
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//这里通过DataKeys的集合得到主键,不过前提是给GridView设置过DataKeyNames
string productid = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Response.Write(productid);
}
(4)GridView中选择按钮的使用方法
<script runat="server">
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Retrieve the company name from the appropriate cell.
String companyName = CustomersGridView.SelectedRow.Cells[2].Text;
// Display company name selected by the user.
Message.Text = "You selected " + companyName + ".";
}
</script>
<html>
<body>
<form runat="server">
<h3>CommandField Example</h3>
<asp:Label id="Message"
forecolor="Red"
runat="server"/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
<columns>
<asp:commandfield showselectbutton="true"
buttontype="Image"
selectimageurl="~\Images\SelectButton.jpg"
headertext="Select Customer"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>