数据连接之--Datalist 的使用(查看、编辑、删除)
<一>? 數據庫綁定:
???????
??????? con.Open();
??????? SqlDataAdapter sda = new SqlDataAdapter();
??????? sda.SelectCommand = new SqlCommand("select * from person", con);
??????? DataSet ds = new DataSet();
??????? sda.Fill(ds, "person");
??????? this.DataList1.DataKeyField = "pid";
??????? this.DataList1.DataSource = ds.Tables["person"];
??????? this.DataList1.DataBind();
<二> Datalist屬性生成器
編輯 更新,刪除,取消 的CommandName分別是edit? update delete cancel ,這樣在datalist的事件中就可以直接響應這些事件,以便進行操作。
樣式有列表如下:
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand">
??????? <ItemTemplate>
??????????? <asp:LinkButton ID="LinkButton1" runat="server" CommandName="select">查看詳細信息</asp:LinkButton>
??????????? <asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit" OnClick="LinkButton2_Click">編輯</asp:LinkButton>
??????????? <asp:LinkButton ID="LinkButton5" runat="server" CommandName="delete">刪除</asp:LinkButton><%# DataBinder.Eval(Container.DataItem ,"pname") %><%# DataBinder.Eval(Container.DataItem ,"psex") %>
??????? </ItemTemplate>
??????? <SelectedItemTemplate>
??????? <%# DataBinder.Eval(Container.DataItem,"pid","序列號:{0}") %><br>
?????????? <%# DataBinder.Eval(Container.DataItem,"pname") %>
????????????? <%# DataBinder.Eval(Container.DataItem,"psex") %>
??????? </SelectedItemTemplate>
??????????? <EditItemTemplate>
??????????????? <asp:LinkButton ID="LinkButton3" runat="server" CommandName="cancel">取消</asp:LinkButton>
??????????????? <asp:LinkButton ID="LinkButton4" runat="server" CommandName="update">保存</asp:LinkButton>
??????????????? <asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem ,"pname") %>'></asp:TextBox>
??????????? </EditItemTemplate>
??????????? <AlternatingItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
??????????????? Font-Strikeout="False" Font-Underline="False" ForeColor="Maroon" />
??????????? <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
??????????????? Font-Underline="False" ForeColor="Green" />
??????? </asp:DataList>
程序代碼:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
??? {
??????? if (e.CommandName == "select")
??????? {
??????????? this.DataList1.SelectedIndex = e.Item.ItemIndex;//選擇
??????????? this.DataList1.DataBind();
??????? }
??? }
??? protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
??? {
??????? this.DataList1.EditItemIndex = e.Item.ItemIndex;//編輯
??????? this.DataList1.DataBind();
??? }
??? protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
??? {
??????? this.DataList1.EditItemIndex = -1;//取消
??????? this.DataList1.DataBind();
??? }
??? protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)//更新
??? {
??????? string pid=this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
??????? string name = ((TextBox)e.Item.FindControl("TextBox1")).Text;
??????? SqlConnection con = connecttion.ado.sqldb();
??????? con.Open();
??????? SqlCommand cmd = new SqlCommand("update person set pname='"+name+ "'where pid='" + pid + "'",con);
??????? cmd.ExecuteNonQuery();
??????? this.DataList1.EditItemIndex = -1;
??????? this.DataList1.DataBind();
??????? Response.Write(pid+name);
??? }
}
?
?
轉載于:https://www.cnblogs.com/gergro/archive/2006/03/28/361129.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的数据连接之--Datalist 的使用(查看、编辑、删除)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vb Shell 打开程序 等待运行完
- 下一篇: 5个常用的设计模式