【】让gridview和detailview 联动效果。程序实现(前提条件主键只有一个字段)
注意,此图中的gridview有三个字段,但实际上应用的只有ddid这个字段。
<asp:DetailsView runat="server" ID="DetailsView1" Height="221px" Width="458px" AutoGenerateRows="False" DataKeyNames="ddid,spid,userid" ForeColor="#333333" GridLines="None" CellPadding="1" AllowPaging="True" AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True"
AutoGenerateInsertButton="True" OnPageIndexChanging="DetailsView1_PageIndexChanging" >
<FooterStyle ForeColor="White" Font-Bold="True" BackColor="#990000" /> <CommandRowStyle Font-Bold="True" BackColor="#FFFFC0" /> <RowStyle ForeColor="#333333" BackColor="#FFFBD6" />
<PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#FFCC66" /> <Fields> <asp:BoundField DataField="ddid" HeaderText="ddid" ReadOnly="True" SortExpression="ddid" /> <asp:BoundField DataField="spid" HeaderText="spid" ReadOnly="True" SortExpression="spid" /> <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="True" SortExpression="userid" /> <asp:BoundField DataField="spnum" HeaderText="spnum" SortExpression="spnum" /> <asp:BoundField DataField="spmoney" HeaderText="spmoney" SortExpression="spmoney" /> </Fields>
<FieldHeaderStyle Font-Bold="True" /> <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#990000" /> <AlternatingRowStyle BackColor="White" /> </asp:DetailsView>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="ddid,spid,userid" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="ddid" HeaderText="ddid" ReadOnly="True" SortExpression="ddid" /> <asp:BoundField DataField="spid" HeaderText="spid" ReadOnly="True" SortExpression="spid" /> <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="True" SortExpression="userid" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="select1" CommandArgument='<%# Eval("ddid") %>' Text="查看详细"></asp:LinkButton> </ItemTemplate> (响应rowcommand事件,并传参数) </asp:TemplateField>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:lddbConnectionString %>" SelectCommand="SELECT [ddid], [spid], [userid] FROM [dingdan]"></asp:SqlDataSource>
rowcommand事件代码
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "select1") {
string strdetail = "select * from dingdan where dd>e.CommandArgument.ToString();//从上面同颜色的地区得到参数 DataTable detail = sn.GiveSPNum(strdetail);// 从数据库中得到详细数据,并显示。 DetailsView1.DataSource = detail; DetailsView1.DataBind(); } }
【】控件可视化,使得detailview控件和gridview控件具有联动效果。
1,为gridview创建数据源。
2,添加gridview控件,制定其数据源,和datakeynames属性。
3,为detailview控件准备数据源。
如图:
|
请发表评论