|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
简单的说:.net只有微软一家在做的,微软也不允许别人跟他做相同的工具,所以他就把需要的工具全部封装在.net的平台上了;而java是公开了。asp.net|datagrid上面的代码完成格局化DataGrid的列,也便是将数据原中的0,1值转换成实践的笔墨的功效,次要是在数据绑定的帮定事务。
检察例子
起首筹办数据源,数据源接纳数据库、XML、数组等都能够。上面以XML做例子。Contacts.xml文件以下:
<?xmlversion="1.0"encoding="gb2312"standalone="yes"?>
<Contacts>
<Contact>
<Email>myaddress@mycompany.com</Email>
<FirstName>E章</FirstName>
<LastName>孟子</LastName>
<Manager>0</Manager>
</Contact>
<Contact>
<Email>youraddress@yourcompany.com</Email>
<FirstName>宪会</FirstName>
<LastName>孟</LastName>
<Manager>1</Manager>
</Contact>
<Contact>
<Email>mm@mmm.mm</Email>
<FirstName>Lover</FirstName>
<LastName>Net</LastName>
<Manager>0</Manager>
</Contact>
<Contact>
<Email>xxx@xxxx.xx</Email>
<FirstName>NET开辟者场地</FirstName>
<LastName/>
<Manager>0</Manager>
</Contact>
<Contact>
<Email>hhh@hhh.hh</Email>
<FirstName>XML开辟者场地</FirstName>
<LastName/>
<Manager>1</Manager>
</Contact>
</Contacts>
FormatDataGridVB.ASPx
<%@PageLanguage="vb"AutoEventWireup="false"Codebehind="FormatDataGridVB.aspx.vb"
Inherits="aspx<ahref="http://dev.21tx.com/web/"target="_blank">Web</a>.FormatDataGridVB"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<metaname="GENERATOR"Content="MicrosoftVisualStudio7.0">
<metaname="CODE_LANGUAGE"Content="<ahref="http://dev.21tx.com/dotnet/csharp/"target="_blank">C#</a>">
<metaname="vs_defaultClientScript"content="<ahref="http://dev.21tx.com/web/javascript/"target="_blank">JavaScript</a>">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<formid="idbSample"method="post"runat="server"class="SubHeading">
<asp:LabelID="MyTitle"runat="server"Font-Bold="True"></asp:Label>
<br>
<br>
<asp:DataGridid="FormatDataGrid"runat="server"AutoGenerateColumns="False"Width="100%">
<HeaderStyleFont-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label
Text=<%#FormatFullName(DataBinder.Eval(Container,"DataItem.FirstName"),_
DataBinder.Eval(Container,"DataItem.LastName"))%>
runat="server"ID="Label1">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumnDataField="Email"ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumnDataField="Manager"ReadOnly="True">
<HeaderStyleHorizontalAlign="Center"></HeaderStyle>
<ItemStyleHorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
FormatDataGridVB.aspx.vb
ImportsSystem
ImportsSystem.Data
ImportsSystem.Web.UI
ImportsSystem.Web.UI.WebControls
ImportsSystem.Xml
PublicClassFormatDataGridVB
InheritsSystem.Web.UI.Page
ProtectedWithEventsFormatDataGridAsSystem.Web.UI.WebControls.DataGrid
ProtectedWithEventsMyTitleAsSystem.Web.UI.WebControls.Label
#Region"Web窗体计划器天生的代码"
该挪用是Web窗体计划器所必须的。
<System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
EndSub
PrivateSubPage_Init(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Init
CODEGEN:此办法挪用是Web窗体计划器所必须的
不要利用代码编纂器修正它。
InitializeComponent()
EndSub
#EndRegion
Private_dsContactsAsDataSet
PrivateSubPage_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
MyTitle.Text="格局化DataGrid的例子【将数据原中的0,1值转换成实践的笔墨】"
FormatDataGrid.Columns(0).HeaderText="姓名"
FormatDataGrid.Columns(1).HeaderText="电子邮件"
FormatDataGrid.Columns(2).HeaderText="职位"
装载XML数据原,注重:这里与数据原范例没有干系,换成数据库也是合用的
_dsContacts=NewDataSet()
_dsContacts.ReadXml(Server.MapPath("Contacts.xml"))
DimdcPkAsDataColumn()={_dsContacts.Tables("Contact").Columns("Email")}
_dsContacts.Tables("Contact").PrimaryKey=dcPk
IfNotPage.IsPostBackThen
只在页面初次哀求时才举行数据绑定
BindContacts()
EndIf
EndSub
PrivateSubBindContacts()
DimdvAsDataView=NewDataView(_dsContacts.Tables("Contact"))
dv.Sort="LastName,FirstName"
FormatDataGrid.DataSource=dv
FormatDataGrid.DataBind()
EndSub
ProtectedFunctionFormatFullName(ByValFirstNameAsObject,ByValLastNameAsObject)AsString
格局划称号列
ReturnCType(LastName,String)&"."&CType(FirstName,String)
EndFunction
PrivateSubFormatDataGrid_ItemDataBound(ByValsenderAsObject,_
ByValeAsSystem.Web.UI.WebControls.DataGridItemEventArgs)HandlesFormatDataGrid.ItemDataBound
确保处置的是数据行,而不是Header大概Footer
Ife.Item.ItemType=ListItemType.ItemOrElsee.Item.ItemType=ListItemType.AlternatingItemThen
失掉Manager字段的值
DimisManagerAsString=CType(DataBinder.Eval(e.Item.DataItem,"Manager"),String)
IfisManager="1"Then
设定笔墨和背景色彩
e.Item.Cells(2).Text="司理"
e.Item.Cells(2).Style.Add("font-weight","bold")
e.Item.Cells(2).ForeColor=System.Drawing.Color.Red
e.Item.BackColor=System.Drawing.Color.AliceBlue
Else
e.Item.Cells(2).Text="一般员工"
EndIf
EndIf
EndSub
EndClass
C#版本
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
///<summary>
///SummarydescriptionforidbSample.
///</summary>
publicclassidbSample:System.Web.UI.Page
{
#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.dgContacts.ItemDataBound+=
newSystem.Web.UI.WebControls.DataGridItemEventHandler(this.dgContacts_ItemDataBound);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
protectedSystem.Web.UI.WebControls.DataGridFormatDataGrid;
privateDataSet_dsContacts;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//装载XML数据原,注重:这里与数据原范例没有干系,换成数据库也是合用的
_dsContacts=newDataSet();
_dsContacts.ReadXml(Server.MapPath("Contacts.xml"));
DataColumn[]dcPk={_dsContacts.Tables["Contact"].Columns["Email"]};
_dsContacts.Tables["Contact"].PrimaryKey=dcPk;
if(!Page.IsPostBack)
{
BindContacts();
}
}
privatevoidBindContacts()
{
DataViewdv=newDataView(_dsContacts.Tables["Contact"]);
dv.Sort="LastName,FirstName";
dgContacts.DataSource=dv;
dgContacts.DataBind();
}
protectedstringFormatFullName(objectFirstName,objectLastName)
{
//格局划称号列
return(string)LastName+","+(string)FirstName;
}
protectedvoidFormatDataGrid_ItemDataBound(objectsource,
System.Web.UI.WebControls.DataGridItemEventArgse)
{
//确保处置的是数据行,而不是Header大概Footer
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
//失掉Manager字段的值
stringisManager=(string)DataBinder.Eval(e.Item.DataItem,"Manager");
if(isManager=="1")
{
//设定笔墨和背景色彩
e.Item.Cells[2].Text="司理"
e.Item.Cells[2].Style.Add("font-weight","bold")
e.Item.Cells[2].ForeColor=System.Drawing.Color.Red
e.Item.BackColor=System.Drawing.Color.AliceBlue
}
else
{
e.Item.Cells[2].Text="一般员工";
}
}
}
}
也许C#刚上市的时候有些抄袭Java吧,但自从C#2.0上市之后,整个局面就扭转乾坤了,不但Java在模仿C#,而且他从来都没能跟得上C#的脚步。 |
|