在asp.net ajax 1.0中,如果有一个detailview控件,想做到每点detailview中的每行,在旁边显示出这条记录的detail情况(master-detail的效果
的话),可以使用updatepanel控件实现
首先,创建一个自定义web控件webusercontrol,ascx,把detailviw控件放到updatepanel控件内,detailview控件如下
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="作者的详细信息"></asp:Label><br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="au_id"
DataSourceID="SqlDataSource1" Height="50px" Width="438px">
<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" />
<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" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [authors] where
au_id=@authid">
<SelectParameters>
<asp:Parameter Name="authid" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
其中用的是pubs数据库,这里用detailview控件根据主(master)gridview的author ID来显示其详细信息,还要为此控件加两个属性和一个方法,
public partial class WebUserControl : System.Web.UI.UserControl
{
private string _authorID;
//定义主键属性
public string AuthorID
{
get { return _authorID; }
set
{
_authorID = value;
this.SqlDataSource1.SelectParameters["authid"].DefaultValue = _authorID;
SqlDataSource1.DataBind();
}
}
//定义UpdatePanel的更新模式
public UpdatePanelUpdateMode UpdateMode
{
get { return this.UpdatePanel1.UpdateMode; }
set { this.UpdatePanel1.UpdateMode = value; }
}
//定义更新方法
public void Update()
{
//调用内容方法
this.UpdatePanel1.Update();
}
}
然后在default.aspx中添加一个updatepanel和gridview,显示所有的作者
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<table style="width: 481px">
<tr>
<td style="width: 240px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
Width="243px" DataKeyNames="au_id">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [au_lname], [au_fname], [au_id] FROM [authors]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
<uc1:WebUserControl id="WebUserControl1" runat="server">
</uc1:WebUserControl></td>
并且要写gridview的selectedindexchanged事件,当用户选择了某位作者后,可以马上在webusercontrol控件中显示出其详细信息
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//判断是否选中了GridView
if (GridView1.SelectedIndex == -1)
{
WebUserControl1.AuthorID = GridView1.DataKeys[0].Value.ToString();
}
else
{
WebUserControl1.AuthorID = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
}
//必须将更新控件的模式设置为"conditional"
WebUserControl1.UpdateMode = UpdatePanelUpdateMode.Conditional;
//更新数据
WebUserControl1.Update();
}