前台代码:
<asp:GridView ID="ClientGridview" runat="server" AutoGenerateColumns="false" DataKeyNames="ClientID" Font-Size="Small" OnRowCommand="ClientGridview_RowCommand"> <Columns> <asp:BoundField DataField="ClientID" HeaderText="客户ID" ReadOnly="true" /> <asp:BoundField DataField="ClientName" HeaderText="客户名" /> <asp:ButtonField CommandName="ShowTeam" HeaderText="团队信息" Text="团队信息" /> <asp:ButtonField CommandName="ClientInfo" HeaderText="详细信息" Text="详细信息" /> </Columns> </asp:GridView>
后台代码:
有两种操作:1.查看DetailsView中的信息(CommandName=ClientInfo)
2.转向另一个页面(CommandName=ShowTeam)
protected void ClientGridview_RowCommand(object sender, GridViewCommandEventArgs e) { conn = ConfigurationManager.AppSettings["str"]; con = new SqlConnection(conn); con.Open(); int index = Convert.ToInt32((string)e.CommandArgument); int ClientID = Convert.ToInt32(ClientGridview.DataKeys[index].Value.ToString()); Session["ClientID"] = ClientID; if ((string)e.CommandName == "ShowTeam") Response.Redirect("SearchTeamInfo.aspx"); else { if ((string)e.CommandName == "ClientInfo") { da = new SqlDataAdapter("select * from client where ClientID='" + ClientID + "'", con); ds = new DataSet(); da.Fill(ds, "Client"); ClientDetailsView.DataSource = ds; ClientDetailsView.DataBind(); } } con.Close(); }