Datalist选择和编辑数据

<form runat="server">

  <ASP:DataList id="MyDataList" runat="server"
       CellSpacing = "2"
       SelectedItemStyle-BackColor="red"
       SelectedItemStyle-ForeColor="white"
       EditItemStyle-BackColor="yellow"
       EditItemStyle-ForeColor="black"
       DataKeyField="ISBN"//调用datalist的DataKeys("索引")将返回该索引行的ISBN栏的值
       OnItemCommand="DoItemSelect"//以下四个按钮都会触发该事件,故需要commandname来区别不同按钮
       OnEditCommand="DoItemEdit"
       OnUpdateCommand="DoItemUpdate"
       OnDeleteCommand="DoItemDelete"
       OnCancelCommand="DoItemCancel">

    <HeaderTemplate>
      <b>Some Wrox Press Books:</b><br />
    </HeaderTemplate>

    <ItemTemplate>
      //定义CommandName="Select",点击后将会使点击行呈现为SelectedItemTemplate定义内容
      <ASP:Button CommandName="Select" Text="Info" runat="server" />
      <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </ItemTemplate>

    <SelectedItemTemplate>
      Title: <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
     //定义CommandName="Edit" ,点击后将会使点击行呈现为EditItemTemplate定义内容
     //以及触发EditCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
      ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
      Published:
      <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
    </SelectedItemTemplate>

    <EditItemTemplate>
      <b>ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %></b> &nbsp;
      //定义了三个button的CommandName不同属性
       //CommandName="Update" 点击触发UpdateCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Update" Text="Update" runat="server" />
       //CommandName="Delete"点击触发DeleteCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Delete" Text="Delete" runat="server" />
     //CommandName="Cancel"点击触发CancelCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Cancel" Text="Cancel" runat="server" /><br />
      Title:
      <ASP:TextBox id="txtTitle" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                   size="46" runat="server" /><br />
      PublicationDate:
      <ASP:TextBox id="txtPubDate" size="20" runat="server"
                   Text='<%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>'/>
    </EditItemTemplate>

  </ASP:DataList>

</form>
脚本:
<script language="C#" runat="server">

 void Page_Load(Object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
   BindDataGrid();  // create data set and bind to list control
 }

       //此事件函数四个按钮都会触发
 void DoItemSelect(Object objSource, DataListCommandEventArgs objArgs )
 {
  lblSQL.Text = "";  // clear any content from SQL statement Label

  // see if it was the Select button that was clicked
                 //判断是否为Info按钮(通过比较commandname属性)
  if (objArgs.CommandName == "Select")
  {
   // set the SelectedIndex property of the list to this item// s index
   MyDataList.SelectedIndex = objArgs.Item.ItemIndex;
   BindDataGrid();  // bind the data and display it
  }
 }


 void DoItemEdit(Object objSource, DataListCommandEventArgs objArgs)
 {
  // set the SelectedIndex property of the list to -1 to "unselect" it
  MyDataList.SelectedIndex = -1;

  // set the EditItemIndex property of the list to this item's index
  MyDataList.EditItemIndex = objArgs.Item.ItemIndex;
  BindDataGrid();  // bind the data and display it
 }


 void DoItemUpdate(Object objSource, DataListCommandEventArgs objArgs)
 {
  // get a reference to the title and publication date text boxes
  TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
  TextBox objPubDateCtrl = (TextBox)objArgs.Item.FindControl("txtPubDate");
  
  // create a suitable SQL statement and execute it
  string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
       + "PublicationDate='" + objPubDateCtrl.Text + "' "
       + "WHERE ISBN='" + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
  ExecuteSQLStatement(strSQL);

  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }
 

 void DoItemDelete(Object objSource, DataListCommandEventArgs objArgs)
 {
  // create a suitable SQL statement and execute it
  string strSQL = "DELETE FROM Booklist WHERE ISBN='"
      + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
  ExecuteSQLStatement(strSQL);

  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }


 void DoItemCancel(Object objSource, DataListCommandEventArgs objArgs)
 {
  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }


 void ExecuteSQLStatement(string strSQL)
 {
  // this is where the SQL statement would be executed against the
  // original data source. In this example, we're simply displaying
  // the statement in a Label on the page
  lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
 }


 void BindDataGrid()
 {
  // 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 '%18610025%'";

  // create a variable to hold an instance of a DataReader object
  OleDbDataReader objDataReader;

  try
  {
   // create a new Connection object using the connection string
   OleDbConnection objConnect = new OleDbConnection(strConnect);

   // open the connection to the database
   objConnect.Open();

   // create a new Command using the connection object and select statement
   OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

   // execute the SQL statement against the command to get the DataReader
   objDataReader = objCommand.ExecuteReader();
  }
  catch (Exception objError)
  {
   // display error details
   outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
        + objError.Message + "<br />" + objError.Source + "<p />";
   return;  //  and stop execution
  }

  // set the DataSource property and bind the list
  MyDataList.DataSource = objDataReader;
  MyDataList.DataBind();
 }

</script>

小结:默认下通过设置asp:button的commandname属性为Edit,Update,Delete,Cancel来激发EditCommand,UpdateCommand,
DeleteCommand和CancelCommand事件。
当然这些button也会触发ItemCommand事件。
注:若要在初始时直接进入编辑模式去掉选中状态,可以这样:
 <ItemTemplate>
      //定义CommandName="Edit",点击后将会使点击行呈现为EditItemTemplate定义内容
      <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
      <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </ItemTemplate>
除去 <SelectedItemTemplate>的定义和ItemCommand事件和处理函数的定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值