protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
string str = "select * from Users" ;
DataTable dt = AspnetDbConnector .GenerateDataTable(str);
RadGrid1.DataSource = dt;
}
protected void RadGrid1_detaildata(object sender, GridDetailTableDataBindEventArgs e)
{
// 从 GridDetailTable 获取 GridDataItem 的 item 标示
GridDataItem dataItem = (GridDataItem )e.DetailTableView.ParentItem;
// 得到主表的 userId
string userId = dataItem.OwnerTableView.Items[dataItem.ItemIndex].GetDataKeyValue("userId" ).ToString();
string str2 = "select * from TeamUser where userId='" + userId + "'" ;
DataTable dt2 = AspnetDbConnector .GenerateDataTable(str2);
e.DetailTableView.DataSource = dt2;
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
// 判断事件是来自主表还是从表
if (e.Item.OwnerTableView.Name=="Master" )
{
string userId = e.Item.OwnerTableView.Items[e.Item.ItemIndex].GetDataKeyValue("userId" ).ToString();
TextBox tb = (TextBox ) e.Item.FindControl("tb1" );
string loginname = tb.Text;
string updateSql1 = "update Users set loginname='" + loginname + "' where userId='" + userId + "'" ;
AspnetDbConnector .runSql(updateSql1);
}
else if (e.Item.OwnerTableView.Name=="Detail" )
{
string teamuserId = e.Item.OwnerTableView.Items[e.Item.ItemIndex].GetDataKeyValue("teamuserId" ).ToString();
TextBox tb = (TextBox )e.Item.FindControl("tb1" );
string description = tb.Text;
string updateSql2 = "update TeamUser set description= '" + description + "' where teamuserId='" + teamuserId +
"'" ;
AspnetDbConnector .runSql(updateSql2);
}
}
Html 代码:
<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="Default4.aspx.cs" Inherits ="Default4" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml">
< head runat ="server">
< title > title >
head >
< body >
< form id ="form1" runat ="server">
< telerik : RadScriptManager ID ="RadScriptManager1" runat ="server">
< Scripts >
<% --Needed for JavaScript IntelliSense in VS2010-- %>
<% --For VS2008 replace RadScriptManager with ScriptManager-- %>
< asp : ScriptReference Assembly ="Telerik.Web.UI" Name ="Telerik.Web.UI.Common.Core.js" />
< asp : ScriptReference Assembly ="Telerik.Web.UI" Name ="Telerik.Web.UI.Common.jQuery.js" />
< asp : ScriptReference Assembly ="Telerik.Web.UI" Name ="Telerik.Web.UI.Common.jQueryInclude.js" />
Scripts >
telerik : RadScriptManager >
< div >
< telerik : RadGrid ID ="RadGrid1" runat ="server" GridLines ="None"
onneeddatasource ="RadGrid1_NeedDataSource"
OnDetailTableDataBind =" RadGrid1_detaildata "
onitemcommand ="RadGrid1_ItemCommand"
onupdatecommand ="RadGrid1_UpdateCommand" >
< MasterTableView DataKeyNames ="userId" Name ="Master" CommandItemDisplay ="Top">
< DetailTables >
< telerik : GridTableView runat ="server" Name ="Detail" DataKeyNames ="teamuserId" CommandItemDisplay =Top>
< ParentTableRelation >
< telerik : GridRelationFields DetailKeyField ="userId" MasterKeyField ="userId" />
ParentTableRelation >
< CommandItemSettings ExportToPdfText ="Export to Pdf" />
< RowIndicatorColumn >
< HeaderStyle Width ="20px" />
RowIndicatorColumn >
< ExpandCollapseColumn >
< HeaderStyle Width ="20px" />
ExpandCollapseColumn >
< Columns >
< telerik : GridEditCommandColumn >
telerik : GridEditCommandColumn >
< telerik : GridButtonColumn Text ="Temp" CommandName ="Test"> telerik : GridButtonColumn >
< telerik : GridTemplateColumn HeaderText ="description">
< ItemTemplate >
< asp : label ID ="TextBox1" runat ="server"
Text =' <% # Bind("description") %> ' > asp : label >
ItemTemplate >
< EditItemTemplate >
< asp : TextBox ID ="tb1" runat ="server"
Text =' <% # Bind("description") %> ' > asp : TextBox >
EditItemTemplate >
telerik : GridTemplateColumn >
Columns >
< EditFormSettings >
< EditColumn UniqueName ="EditCommandColumn1"> EditColumn >
EditFormSettings >
telerik : GridTableView >
DetailTables >
< CommandItemSettings ExportToPdfText ="Export to Pdf"> CommandItemSettings >
< RowIndicatorColumn >
< HeaderStyle Width ="20px"> HeaderStyle >
RowIndicatorColumn >
< ExpandCollapseColumn Visible ="True">
< HeaderStyle Width ="20px"> HeaderStyle >
ExpandCollapseColumn >
< Columns >
< telerik : GridTemplateColumn HeaderText ="loginname">
< ItemTemplate >
< asp : label ID ="TextBox1" runat ="server"
Text =' <% # Bind("loginname") %> ' > asp : label >
ItemTemplate >
< EditItemTemplate >
< asp : TextBox ID ="tb1" runat ="server"
Text =' <% # Bind("loginname") %> ' > asp : TextBox >
EditItemTemplate >
telerik : GridTemplateColumn >
< telerik : GridEditCommandColumn >
telerik : GridEditCommandColumn >
Columns >
< EditFormSettings >
< EditColumn UniqueName ="EditCommandColumn1"> EditColumn >
EditFormSettings >
MasterTableView >
telerik : RadGrid >
div >
form >
body >
html >