第三天ASP.NET学习总结

    技术2022-05-11  81

    1.DataGrid控件不用模板时显示头部文字和超链接:<asp:DataGrid  ID="dgrdLinks"  AutoGenerateColumns="False"  EnableViewState="False"  CellPadding="10"  Runat="Server"><Columns>  <asp:HyperLinkColumn    DataNavigateUrlField="link_url"    DataTextField="link_title"     HeaderText="链接标题"    HeaderStyle-Font-Name="Arial"/></Columns></asp:DataGrid>         //DataGrid默认时显示header,不显示footer的

    2.DataGrid的GridLines=none时是没有单元格线的....BackImgeUrl是背景图片的地址.

    3.DataGrid的style....同样可以对DataGrid的个别列进行样式设置<asp:DataGrid  ID="dgrdTitles"  CellPadding="10"  BorderStyle="Dashed"   //虚线框  BorderColor="blue"  BorderWidth="8px"  HeaderStyle-Font-Name="Arial"  HeaderStyle-Font-Bold="True"  HeaderStyle-BackColor="lightyellow"  ItemStyle-Font-Name="Arial"  ItemStyle-Font-Size="10pt"  AlternatingItemStyle-BackColor="AliceBlue"  Runat="Server" />

    4.DataGrid对列进行排序(必须将AllowSorting设为True,并要响应一个SortCommand事件)前台:     <asp:DataGrid  ID="dgrdTitles"  AllowSorting="True"  OnSortCommand="dgrdTitles_SortCommand"  AutoGenerateColumns="False"  CellPadding="10"  Runat="Server"><Columns>  <asp:BoundColumn    DataField="Title"    HeaderText="Sort Titles"    SortExpression="title"/>  <asp:BoundColumn    HeaderText="Sort Price"    DataField="price"    DataFormatString="{0:c}"    SortExpression="price"/>  <asp:BoundColumn    DataField="Notes" /></Columns></asp:DataGrid>

    后台:     void Page_Load(Object sender , EventArgs e) { if (! IsPostBack ) {  BindDataGrid( "Title" ); }}

    void BindDataGrid( string  strSortField ) { SqlConnection conPubs; SqlCommand cmdSelect;

     conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Pubs" ); cmdSelect = new SqlCommand( "Select * From Titles Order By " + strSortField, conPubs ); conPubs.Open(); dgrdTitles.DataSource = cmdSelect.ExecuteReader(); dgrdTitles.DataBind(); conPubs.Close();}

    void dgrdTitles_SortCommand( object s, DataGridSortCommandEventArgs e ) { BindDataGrid( e.SortExpression );}

    5.DataGrid对数据进行分页显示(必须将AllowPaging设为True,并要响应一个PageIndexChanged事件)前台:<asp:DataGrid  ID="dgrdProducts"  AllowPaging="True"  AllowCustomPaging="True"  PageSize="3"  OnPageIndexChanged="dgrdProducts_PageIndexChanged"  PagerStyle-Mode="NumericPages"  CellPadding="3"  Runat="Server" />后台:SqlConnection conNorthwind;string  strSelect;int intStartIndex;int intEndIndex;

    void Page_Load(Object sender , EventArgs e) { SqlCommand cmdSelect;

     conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" ); if (! IsPostBack )  {  // Get  Total Pages  strSelect = "Select Count(*) From Products";  cmdSelect = new SqlCommand( strSelect, conNorthwind );  conNorthwind.Open();  dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );  conNorthwind.Close();  BindDataGrid(); }}

    void BindDataGrid () { SqlDataAdapter dadProducts; DataSet dstProducts;

     intEndIndex = intStartIndex + dgrdProducts.PageSize; strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID"; dadProducts = new SqlDataAdapter( strSelect, conNorthwind ); dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex ); dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex ); dstProducts = new DataSet(); dadProducts.Fill( dstProducts );

     dgrdProducts.DataSource = dstProducts; dgrdProducts.DataBind();}

    void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) { intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize ); dgrdProducts.CurrentPageIndex = e.NewPageIndex; BindDataGrid();}

    6.DataGrid大类控制小类....前台:<asp:DataGrid  ID="dgrdCategories"  OnItemCommand="dgrdCategories_ItemCommand"  DataKeyField="CategoryID"  AutoGenerateColumns="False"  SelectedItemStyle-BackColor="LightGreen"  ShowHeader="False"  Runat="Server"><Columns>  <asp:TemplateColumn>  <ItemTemplate>  <asp:LinkButton    Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'    Runat="Server"/>  </ItemTemplate>  </asp:TemplateColumn>  <asp:BoundColumn DataField="Description" /></Columns></asp:DataGrid>

    <p>

    <asp:DataGrid  ID="dgrdProducts"  HeaderStyle-BackColor="yellow"  Runat="Server" />后台:void Page_Load(Object sender, EventArgs e){  BindMasterGrid();}

    void BindMasterGrid (){ SqlConnection conNorthwind; SqlCommand cmdSelect;

     conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" ); cmdSelect = new SqlCommand( "Select * From Categories", conNorthwind ); conNorthwind.Open(); dgrdCategories.DataSource = cmdSelect.ExecuteReader(); dgrdCategories.DataBind(); conNorthwind.Close();}

    void BindDetailGrid( int intCatID ) { SqlConnection conNorthwind; string  strSelect; SqlCommand cmdSelect;

     conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" ); strSelect = "Select * From Products Where CategoryID=@catID"; cmdSelect = new SqlCommand( strSelect, conNorthwind ); cmdSelect.Parameters.Add( "@catID", intCatID ); conNorthwind.Open(); dgrdProducts.DataSource = cmdSelect.ExecuteReader(); dgrdProducts.DataBind(); conNorthwind.Close();}

    void dgrdCategories_ItemCommand( object s, DataGridCommandEventArgs e ) { int intCatID;

     intCatID = Convert.ToInt32(dgrdCategories.DataKeys[ e.Item.ItemIndex ]); dgrdCategories.SelectedIndex = e.Item.ItemIndex; BindDetailGrid( intCatID );}

     


    最新回复(0)