• datalist的数据绑定事件收藏


    <ASP:DataList id="MyDataList" runat="server" RepeatLayout="Table"
         OnItemDataBound="CheckTitle">
    //OnItemDataBound事件发生在各行被绑定到源数据时被调用
      <HeaderTemplate>
        <div class="rHead">
          Some of the Latest Wrox Press Books<br />
          <img src="images/redrule.gif">
        </div>
      </HeaderTemplate>

      <ItemTemplate>
        <div class="rItem">
          <b><ASP:Label id="TitleLabel" runat="server"
                  Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' /></b><br />
          * ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
          Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>
        </div>
      </ItemTemplate>
      //上面绑定到数据源的各个字段
      <FooterTemplate>
        <img src="images/redrule.gif">
        <div class="rFoot">
          For more information visit <a href="http://www.wrox.com">http://www.wrox.com</a>
        </div>
      </FooterTemplate>

    </ASP:DataList><p />
    下面是脚本:
    <script language="c#" runat="server">

     void Page_Load(Object sender, EventArgs e)
     {
      if (!Page.IsPostBack)  // fill the DataList from a DataView object
      {
       // get connection string from ..\global\connect-strings.ascx user control
       string strConnect = ctlConnectStrings.OLEDBConnectionString;

       // create a SQL statement to select some rows from the database
       string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610026%'";

       // create a variable to hold an instance of a DataView object
       DataView objDataView;

       // get dataset from get-dataset-control.ascx user control
                            //数据源是dataview
       objDataView = ctlDataView.GetDataView(strConnect, strSelect);

       if (objDataView == null)
        return;

       // set the DataSource property of the DataList
       MyDataList.DataSource = objDataView;

       // and bind the control to the data
       MyDataList.DataBind();
      }
     }

    //OnItemDataBound事件发生在各行被绑定到源数据时被调用该函数(即在发送到客户端之前发生):
     void CheckTitle(Object objSender, DataListItemEventArgs objArgs)
     {
      // see what type of row (header, footer, item, etc.) caused the event
      ListItemType objItemType = (ListItemType)objArgs.Item.ItemType;

      // only format the results if it's an Item or AlternatingItem event
      if (objItemType == ListItemType.Item || objItemType == ListItemType.AlternatingItem)
      {
       // objArgs.Item.DataItem returns the data for this row of items
       DataRowView objRowVals = (DataRowView)objArgs.Item.DataItem;

       // get the value of the Title column
       string strTitle = objRowVals["Title"].ToString();

       if (strTitle.IndexOf("Active Server Pages") != -1 || strTitle.IndexOf("ADO") != -1)
       {
        // get a reference to the "Title" ASP:Label control in this row
                                   //找到此行中id="TitleLabel"的单元并添加特殊字段
        Label objLabel = (Label)objArgs.Item.FindControl("TitleLabel");

        // add a message to this Label control
        objLabel.Text += " &nbsp; <span class='bigRed'>Great for ASP Programmers!</span>";
       }
      }
     }

    </script>
    小结:DataListItemEventArgs objArgs对象包含了关于该事件和被绑定的行信息。
       注意用法:
    ListItemType objItemType = (ListItemType)objArgs.Item.ItemType;
       DataRowView objRowVals = (DataRowView)objArgs.Item.DataItem;
    //objArgs.Item.DataItem返回了此行的数据项(本例中为"Title"和"ISBN""PublicationDate")
       string strTitle = objRowVals["Title"].ToString();

  • 相关阅读:
    zabbix_agent 主动模式配置
    zabbix 监控ipmi
    超级详细全截图化VMware 安装ubantu
    docker 部署
    C# DataTable和List转换操作类
    C#类型转换工具类
    C# 注册windows 服务
    C# wsdl.exe 生成类文件
    visual studio code download url
    c# xml序列化和反序列化
  • 原文地址:https://www.cnblogs.com/wubin264/p/1391996.html
Copyright © 2020-2023  润新知