六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

DataGrid學習5

[摘要]更新數(shù)據(jù)庫在 Web 應用程序中可能經(jīng)常很棘手。針對這種情況,DataGrid 控件提供了一些使更新更容易的內(nèi)置支持。為了允許對行進行編輯,DataGrid 支持整型 EditItemIndex 屬性,該屬性指示網(wǎng)格的哪一行應該是可編輯的。設置了該屬性后,DataGrid 按該索引將行呈現(xiàn)為文本輸...

更新數(shù)據(jù)庫在 Web 應用程序中可能經(jīng)常很棘手。針對這種情況,DataGrid 控件提供了一些使更新更容易的內(nèi)置支持。為了允許對行進行編輯,DataGrid 支持整型 EditItemIndex 屬性,該屬性指示網(wǎng)格的哪一行應該是可編輯的。設置了該屬性后,DataGrid 按該索引將行呈現(xiàn)為文本輸入框,而不是簡單的標簽。值 -1(默認值)指示沒有行是可編輯的。頁可以在服務器端窗體中包含 DataGrid,并通過 DataGrid 的對象模型獲取對編輯數(shù)據(jù)的訪問。 



為了確定哪一行應該是可編輯的,需要一種方法接受用戶關(guān)于他們希望編輯哪一行的輸入。DataGrid 可以包含一個 EditCommandColumn 來呈現(xiàn)激發(fā)三個特殊事件的鏈接:EditCommand、UpdateCommand 和 CancelCommand。EditCommandColumn 以聲明方式添加到 DataGrid 的 Columns 集合,如下面的示例所示。 
<ASP:DataGrid id="MyDataGrid" runat="server"
  ...
  OnEditCommand="MyDataGrid_Edit"
  OnCancelCommand="MyDataGrid_Cancel"
  OnUpdateCommand="MyDataGrid_Update"
  DataKeyField="au_id"
>
  <Columns>
    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
  </Columns>
</ASP:DataGrid>
在 DataGrid 標記本身上,將事件處理程序連到從 EditCommandColumn 激發(fā)的每個命令。這些處理程序的 DataGridCommandEventArgs 參數(shù)使您得以直接訪問由用來設置 DataGrid 的 EditItemIndex 的客戶端選擇的索引。注意,需要重新綁定 DataGrid 以使更改生效,如下面的示例所示。
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E) {
    MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
    BindGrid();
}
Public Sub MyDataGrid_Edit(sender As Object, E As DataGridCommandEventArgs)
    MyDataGrid.EditItemIndex = E.Item.ItemIndex
    BindGrid()
End Sub
public function MyDataGrid_Edit(sender:Object, E:DataGridCommandEventArgs) : void {
    MyDataGrid.EditItemIndex = int(E.Item.ItemIndex);
    BindGrid();
}
當編輯某行 DataGrid 時,EditCommandColumn 呈現(xiàn) Update 和 Cancel 鏈接。如果客戶端選擇 Cancel,只需將 EditItemIndex 設置回 -1。但如果客戶端選擇 Update,則需要對數(shù)據(jù)庫執(zhí)行更新命令。執(zhí)行更新查詢要求知道希望更新的行的數(shù)據(jù)庫中的主鍵。為支持此要求,DataGrid 公開一個可以設置為主鍵字段名的 DataKeyField 屬性。在連到 UpdateCommand 的事件處理程序中,可以從 DataGrid 的 DataKeys 集合檢索鍵名。使用事件的 ItemIndex 在此集合中索引,如下面的示例所示。 
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
myCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CType(E.Item.ItemIndex, Integer))
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[int(E.Item.ItemIndex)];
在 Update 事件處理程序的最后,將 EditItemIndex 設置回 -1。下面的示例說明此代碼的運行。 



<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
 <script language="C#" runat="server">



    SqlConnection myConnection;



    protected void Page_Load(Object Src, EventArgs E)
    {
        myConnection=new SqlConnection("server=jeff;uid=sa;password=;database=pubs");
        if (!IsPostBack)
            BindGrid();
    }



    public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
    {
        MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
        BindGrid();
    }



    public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
    {
        MyDataGrid.EditItemIndex = -1;
        BindGrid();
    }



    public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
    {
        String updateCmd = "UPDATE Authors SET au_lname = @LName, au_fname = @FName, phone = @Phone, "
             + "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";



        SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);



        myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
        myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
        myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
        myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
        myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
        myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
        myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
        myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
        myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));



        myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];



        String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@State","@Zip","@Contract"};



        int numCols = e.Item.Cells.Count;
        for (int i=2; i<numCols-1; i++) //跳過第一、第二和最后一列
        {
            String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text;



            // 檢查在所需字段中是否有空值
            if (i<6 && colvalue == "")
            {
                Message.InnerHtml = "錯誤:“作者 ID”、“姓名”或“電話”不允許使用空值";
                Message.Style["color"] = "red";
                return;
            }



            myCommand.Parameters[cols[i-1]].Value = colvalue;
        }



        //追加最后一行,將 true/false 值轉(zhuǎn)換為 0/1
        if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "True", true)==0)
                    myCommand.Parameters["@Contract"].Value =  "1";
        else
                    myCommand.Parameters["@Contract"].Value =  "0";



        myCommand.Connection.Open();



        try
        {
            myCommand.ExecuteNonQuery();
            Message.InnerHtml = "<b>已更新記錄</b><br>" + updateCmd;
            MyDataGrid.EditItemIndex = -1;
        }
        catch (SqlException exc)
        {
            if (exc.Number == 2627)
                Message.InnerHtml = "錯誤:已存在具有相同主鍵的記錄";
            else
                Message.InnerHtml = "錯誤:未能更新記錄,請確保正確填寫了字段";
            Message.Style["color"] = "red";
        }



        myCommand.Connection.Close();



        BindGrid();
    }



    public void BindGrid()
    {
        SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);



        DataSet ds = new DataSet();
        myCommand.Fill(ds, "Authors");



        MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
        MyDataGrid.DataBind();
    }



 </script>
 <body style="font: 10.5pt 宋體">
  <form runat="server" ID="Form1">
   <h3><font face="宋體">更新數(shù)據(jù)行</font></h3>
   <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server" /><p>
   <ASP:DataGrid id="MyDataGrid" runat="server" Width="800" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel" OnUpdateCommand="MyDataGrid_Update" DataKeyField="au_id">
   <Columns>
    <asp:EditCommandColumn EditText="編輯" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false" />
   </Columns>
   </ASP:DataGrid>
  </form>
 </body>
</html>



標簽:DataGrid學習5 
主站蜘蛛池模板: 天天天综合 | 思思久久96热在精品国产免费 | 四虎永久免费网站免费观看 | 午夜视 | 亚洲第一成人在线 | 婷婷色天使在线视频观看 | 色噜| 欧洲美女高清一级毛片 | 视频一区在线 | 伊人久久大香 | 亚洲免费专区 | 性欧美高清精品video | 全部毛片免费看 | 日本在线视频免费观看 | 日韩伦理一区二区 | 天天射天天干天天色 | 亚洲一区在线免费观看 | 青娱乐福利视频 | 亚洲区第一页 | 青青青青爽极品在线视频 | 亚洲手机在线观看 | 天天躁日日2018躁狠狠躁 | 日韩一区国产一级 | 欧美一级特黄aaaaaaa在线观看 | 亚洲免费视频播放 | 四虎国产精品永久地址51 | 四虎永久在线精品网址 | 人妖在线精品一区二区三区 | 全免费午夜一级毛片一级毛 | 天天噜天天干 | 热re久久精品国产99热 | 影音先锋亚洲综合小说在线 | 爽爽影院在线18观看 | 四虎影院在线免费观看视频 | 亚洲国产高清精品线久久 | 日韩永久免费视频 | 天天干天天操天天玩 | 香蕉毛片a | 天天色天天干天天 | 四虎影视在线永久免费观看 | 天天射日 |