在后台改变SqlDataSource的参数
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
<title>GridView DetailsView Master-Details (with Editing)</title>
</head>
<script runat="server">
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "sel")
{
//可以用e.CommandArgument来取得当前点击的行的索引,
//这里点的按钮必须是 <asp:ButtonField CommandName="sel" DataTextField="au_id" Text="按钮" />
//我试过了,用模板列就取不得行数,不知道为什么
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
GridView1.SelectedIndex = index;
SqlDataSource1.SelectParameters["au_id"].DefaultValue= row.Cells[0].Text;
SqlDataSource1.SelectParameters["au_lname"].DefaultValue = row.Cells[1].Text;
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedIndex = -1;
SqlDataSource1.SelectParameters["au_id"].DefaultValue = "";
SqlDataSource1.SelectParameters["au_lname"].DefaultValue ="";
}
</script>
<form id="form1" runat="server">
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1"
Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="au_id">
<Fields>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
</Fields>
</asp:DetailsView>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="au_id" DataSourceID="SqlDataSource2" GridLines="Vertical" OnRowCommand="GridView1_RowCommand" OnPageIndexChanged="GridView1_PageIndexChanged">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
<asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />
<asp:CheckBoxField DataField="contract" HeaderText="contract" SortExpression="contract" />
<asp:ButtonField CommandName="sel" DataTextField="au_id" Text="按钮" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT * FROM [authors]"></asp:SqlDataSource>
<br />
</form>
</body>
</html>