Field's Space

.NET技术学习

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
原文地址:http://dotnet.chinaitlab.com/ADONET/398553.html

1.为DataGrid控件设计样式
   
   在<asp:datagridid="DataGrid1"runat="server">之后添加如下代码
   
   <FooterStyleForeColor="Black"BackColor="#CCCCCC"></FooterStyle>
   <SelectedItemStyleFont-Bold="True"ForeColor="White"BackColor="#008A8C"></SelectedItemStyle>
   <AlternatingItemStyleBackColor="Gainsboro"></AlternatingItemStyle>
   <ItemStyleForeColor="Black"BackColor="#EEEEEE"></ItemStyle>
   <HeaderStyleFont-Bold="True"ForeColor="White"BackColor="#000084"></HeaderStyle>
   
   说明:
   (1)在每个标签内主要是 ForeColor  BackColor Font-Bold这几个属性值
   
   2.为DataGrid控件添加绑定列
   
   <asp:BoundColumnDataField=""ReadOnly="True"HeaderText=""></asp:BoundColumn>
   说明:
   (1)在标签内的基本属性是DataField/HeaderText
   (2)DataFormatString用于获取或设置指定列中各项的显示格式的字符串。
    形式为{A:Bxx}。例如,格式化字符串{0:F2}将显示带两位小数的定点数。
    其中A值只能设置为0,因为每个单元格中只有一个值。
    冒号后的字符(常规示例中为B)指定值的显示格式
    C 以货币格式显示数值。
       D 以十进制格式显示数值。
       E 以科学记数法(指数)格式显示数值。
       F 以固定格式显示数值。
       G 以常规格式显示数值。
       N 以数字格式显示数值。
       X 以十六进制格式显示数值。
   (3)Visible获取或设置一个值,该值指示此列在DataGrid控件中是否可见。
   (4)ReadOnly设置些列是否只读,若是只读的话,则不能修改.
   (5)SortExpression获取或设置选择进行排序的列时传递到OnSortCommand方法的字段或表达式的名称。
  

  3.为DataGrid控件添加模板列

   <asp:TemplateColumnHeaderText="类别">
    <ItemTemplate>
     <asp:LabelText='<%#DataBinder.Eval(Container.DataItem,"actorclassname")%>'runat="server"ID="Label1"/>
    </ItemTemplate>
    <EditItemTemplate>
     <selectname="sltclassname">
      <%=ActorClass.GetParentClass(0)%>
     </select>
    </EditItemTemplate>
   </asp:TemplateColumn>

   说明:

   (1)基本框架是

    <asp:TemplateColumnHeaderText="类别">
    <ItemTemplate></ItemTemplate>
    </asp:TemplateColumn>

   (2)全面的模板列

    <asp:TemplateColumn>

              <HeaderTemplate>
                 <b>Tax</b>
              </HeaderTemplate>

              <ItemTemplate>
                 <asp:Label
                      Text='<%#DataBinder.Eval(Container.DataItem,"Tax")%>'
                      runat="server"/>
              </ItemTemplate>

              <EditItemTemplate>

                 <asp:CheckBox
                      Text="Taxable"
                      runat="server"/>

              </EditItemTemplate>

              <FooterTemplate>
                 <asp:HyperLinkid="HyperLink1"
                      Text="Microsoft"
                      NavigateUrl="http://www.microsoft.com"
                      runat="server"/>
              </FooterTemplate>

           </asp:TemplateColumn>

 (3)为布尔型列应用模板列

  <asp:TemplateColumn>
        <ItemTemplate>
            <asp:Label
                Text='<%#DataBinder.Eval(Container.DataItem,"Tax")%>'
                    runat="server"/>
           </ItemTemplate>
           <EditItemTemplate>
            <asp:CheckBox
                Text="Taxable"
                    runat="server"/>

           </EditItemTemplate>
       </asp:TemplateColumn>
       在正常状态,用Label控件显示
       在编辑状态,用CheckBox控件显示
   
 (4) 为枚举类型列应用模板列,如业务地区(全网/广东/云南等等)

  <asp:TemplateColumnHeaderText="处理方式">
   <ItemTemplate>
    <asp:LabelID="lbStatus">
     <%#DataBinder.Eval(Container,"DataItem.DealWith")%>
    </asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
    <asp:DropDownListid="dpStatus2"runat="server"DataTextField="status">
     <asp:ListItemValue="Log">Log(日志)</asp:ListItem>
     <asp:ListItemValue="SendSms">SendSms(短信)</asp:ListItem>
    </asp:DropDownList>
   </EditItemTemplate>
  </asp:TemplateColumn>
  在正常状态,用Label控件显示
  在编辑状态,用DropDownList控件显示
 
 (5)为长字符串应用模板列,如一篇文章的内容
   还未做过
  
 4.为DataGrid控件添加按钮列
 
 <asp:ButtonColumn
       HeaderText="Removefromcart"
       ButtonType="PushButton"
       Text="Remove"
       CommandName="RemoveFromCart"/>
   (1)要使用按钮列,必须在DataGrid控件中添加OnItemCommand属性,并为该事件添加处理方法.
   (2)模板列可以实现按钮列能实现的任何功能.
   
   5.为DataGrid控件添加编辑列

   <asp:EditCommandColumnButtonType="LinkButton"UpdateText="更新"HeaderText="编辑"CancelText="取消"EditText="编辑"></asp:EditCommandColumn>
   (1)ButtonType有两个值:LinkButton超级链接样式按钮的列|PushButton普通按钮的列。

 6.为DataGrid控件添加超链接列

 <asp:HyperLinkColumnText="添加子类"DataNavigateUrlField="ActorclassID"DataNavigateUrlFormatString="addActorClass.aspx?classID={0}"></asp:HyperLinkColumn>

 (1)为每一行设置相同的文字及跳转的URL地址

  设置Text和NavigateUrl属性,则列中的所有超级链接将共享同一标题和URL

 (2)为每一行设置不同的文字及不同跳转的URL地址

  A. 用DataTextField设置数据源字段,若还想在原数据的基础上加工一下(如字段值为300,想显示为300元)
   则再设置DataTextFormatString字段
  B. 用DataNavigateUrlField及DataNavigateUrlFormatString来设置URL地址
   用DataTextField="money"DataTextFormatString="{0}元"
  C. 举例
   DataNavigateUrlField="ActorclassID"DataNavigateUrlFormatString="addActorClass.aspx?classID={0}"
   
 7.为DataGrid控件添加"编辑"代码

  在DataGrid标签中加入
  OnUpdateCommand="DataGrid1_Update"OnCancelCommand="DataGrid1_Cancel"OnEditCommand="DataGrid1_Edit"代码
  在codeBehind页面加入如下代码
  ///响应编辑按钮
  publicvoidDataGrid1_Edit(Objectsender,DataGridCommandEventArgse)
  {
   DataGrid1.EditItemIndex=e.Item.ItemIndex;
   if(Request.QueryString.Get("classID")!=null)
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
  }
  
  ///响应取消按钮
  publicvoidDataGrid1_Cancel(Objectsender,DataGridCommandEventArgse)
  {
   DataGrid1.EditItemIndex=-1;
   if(Request.QueryString.Get("classID")!=null)
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));

  }
  
  ///响应更新按钮  
  publicvoidDataGrid1_Update(Objectsender,DataGridCommandEventArgse)
  {
   TextBoxClassNameText=(TextBox)e.Item.Cells[1].Controls[0];
   stringclassName=ClassNameText.Text;
   intclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
   TextBoxorderID2=(TextBox)e.Item.Cells[5].Controls[0];
   intorderID=Int32.Parse(orderID2.Text);
   ActorClass.ModifyActorClass(className,classID,orderID);

   DataGrid1.EditItemIndex=-1;
   if(Request.QueryString.Get("classID")!=null)
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
  }

  说明

  (1)DataGrid事件处理程序的格式
   MethodName(Objectsender,DataGridCommandEventArgse)
  (2)更新按钮的说明
   A.获取编辑状态中的文本框
    TextBoxClassNameText=(TextBox)e.Item.Cells[1].Controls[0];
    stringclassName=ClassNameText.Text;
   B.获取编辑状态中的下拉列表框
    方法一
    intclassID;
    classID=Int32.Parse(Request.Form.Get("sltclassname"));
    方法二
    DropDownListbbb=(DropDownList)e.Item.Cells[10].FindControl("dpStatus2");
    stringddpValue=bbb.SelectedValue
   C.获取编辑状态中的复选框
    boolboolEnabled=((CheckBox)e.Item.FindControl("chk_enabled")).Checked;
    Stringstr2;
    if(boolEnabled)
    {
     str2="1";
    }
    else 
    {
     str2="0";
    }
    赋值给str2,原因是插入到数据库的布尔型值只能是1或者0
   D.获取编辑状态中的文本值,即该列是只读的.
    stringstoryID=(e.Item.Cells[0].Text).ToString();

8.为DataGrid控件添加分页事件

  在DataGrid控件标签中加入如下代码
  OnPageIndexChanged="DataGrid1_PageIndexChanged"
  在后台中加入如下代码
  ///<summary>
  ///响应分页事件
  ///</summary>
  ///<paramname="sender"></param>
  ///<paramname="e"></param>
  publicvoidDataGrid1_Page(Objectsender,DataGridPageChangedEventArgse)
  {
   DataGrid1.CurrentPageIndex=e.NewPageIndex;
   DataBind();
  }
 
 9.为DataGrid控件添加绑定事件,即在DataGrid绑定时发生的事件处理

  一般用些事件来,做一些页面上的效果.如更改背景色,文本框大小等.
  OnItemDataBound="DataGrid1_ItemDataBound"
  ///<summary>
  ///响应DataGrid绑定事件
  ///</summary>
  ///<paramname="sender"></param>
  ///<paramname="e"></param>
  publicvoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
  {
   if(e.Item.ItemType==ListItemType.Item)
   {
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");

   }
   elseif(e.Item.ItemType==ListItemType.AlternatingItem)
   {
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#f6f6f6'");
   }
  }
  
 10.为DataGrid控件添加接钮处理事件程序

  在DataGrid控件标签中加入如下代码
  OnItemCommand="ItemsGrid_Command"
  在后台中加入如下代码
  publicvoidItemsGrid_Command(Objectsender,DataGridCommandEventArgse)
  {
   switch(((LinkButton)e.CommandSource).CommandName)
   {

    case"Delete":
     intclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     ActorClass.DeleteActorClass(classID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;

     //Addothercaseshere,iftherearemultipleButtonColumnsin
     //theDataGridcontrol.
    case"hidden":
     intactorID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     ActorClass.HiddenActorClass(actorID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;
    case"MoveUp":
     intactorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     stringorderID=(e.Item.Cells[5].Text).ToString();
     ActorClass.MoveUp(orderID,actorclassID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;
    case"MoveDown":
     actorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     orderID=(e.Item.Cells[5].Text).ToString();
     ActorClass.MoveDown(orderID,actorclassID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbyorderID"));
     break;
    

    default:
     //Donothing.
     break;

   }
  }

 11.为DataGrid添加模板列,但是内容根据字段值来显示"链接",还是文本

  以下三个都是,根据字段列不同,而显示内容及显示控件不同的处理代码.
  <asp:TemplateColumnHeaderText="子菜单">
   <ItemTemplate>
    <%#ActorClassManage.hasLeaf(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),DataBinder.Eval(Container.DataItem,"child").ToString())%>
   </ItemTemplate>
  </asp:TemplateColumn>
  publicstaticstringhasLeaf(stringid,stringchild)
  {
   stringlRtn="";
   if(Int32.Parse(child)>0)
    lRtn="<ahref='Actorclassmanage.aspx?classID="+id+"'><fontcolor=blue>子菜单("+child+")</font></a>";
   else
    lRtn="无子菜单";

   returnlRtn;
  }
  <asp:TemplateColumnHeaderText="属性">
   <ItemTemplate>
    <asp:LinkButtonText='<%#IsHidden(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),(bool)DataBinder.Eval(Container.DataItem,"Enabled"))%>'runat="server"CommandName="hidden"ID="Linkbutton1"></asp:LinkButton>
   </ItemTemplate>
  </asp:TemplateColumn>
  publicstaticstringIsHidden(stringid,boolenabled)
  {
   stringlRtn="";
   if(enabled==true)
   {
    lRtn="[显示]";
   }
   else
   {
    lRtn="隐藏";
   }
   returnlRtn;
  }
  publicstaticvoidSort(stringactorclassID,stringorderID)
  {
   stringtemp="";
   if(Int32.Parse(BgPicManage.GetMaxCode("actorclass","orderID"))==Int32.Parse(orderID))
   {
    temp+="<ipnuttype='submit'value='向下移'>";
   }
   if(Int32.Parse(orderID)==0)
   {
    temp+="<ipnuttype='submit'value='向上移'>";
   }
  }
  
 12.DataGrid控件自定义分页代码
 
  将下列代码放于包含<DataGrid>的form中去,
  <pstyle="FONT-SIZE:9pt"align="center">
         <asp:labelid="lblPageCount"runat="server"></asp:label>
         <asp:labelid="lblCurrentIndex"runat="server"></asp:label>
         <asp:linkbuttonid="btnFirst"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="0"></asp:linkbutton>
         <asp:linkbuttonid="btnPrev"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="prev"></asp:linkbutton>
         <asp:linkbuttonid="btnNext"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="next"></asp:linkbutton>
         <asp:linkbuttonid="btnLast"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="last"></asp:linkbutton>
       </p>
       
       后台代码
       
       privatevoidPage_Load(objectsender,System.EventArgse)
       {
         //在此处放置用户代码以初始化页面
         btnFirst.Text="最首页";
         btnPrev.Text="前一页";
         btnNext.Text="下一页";
         btnLast.Text="最后页";
         //绑定数据源
         if(!Page.IsPostBack)
         {
          OpenDatabase();
          BindGrid();
         }
       }
       
       //用于显示"第几页,总*页"
       privatevoidShowStats()
  {
    lblCurrentIndex.Text="第"+(MyDataGrid.CurrentPageIndex+1).ToString()+"页";
    lblPageCount.Text="总共"+MyDataGrid.PageCount.ToString()+"页";
  }
  
  //响应分页按钮
  publicvoidPagerButtonClick(objectsender,EventArgse)
       {
        stringarg=((LinkButton)sender).CommandArgument.ToString();
         switch(arg)
         {
           case"next":
             if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
             {
              MyDataGrid.CurrentPageIndex+=1;
            }
            break;
           case"prev":
             if(MyDataGrid.CurrentPageIndex>0)
             {
              MyDataGrid.CurrentPageIndex-=1;
             }
             break;
           case"last":
             MyDataGrid.CurrentPageIndex=(MyDataGrid.PageCount-1);
             break;
           default:
             MyDataGrid.CurrentPageIndex=System.Convert.ToInt32(arg);
             break;
         }
         BindGrid();
         ShowStats();
       }

posted on 2006-07-15 16:37  Field  阅读(948)  评论(1编辑  收藏  举报