GridView .net訪問

來源:互聯網
上載者:User

標籤:style   class   code   tar   ext   com   

HTML code

 
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting">  
  2.     <Columns>   
  3.         <asp:BoundField DataField="id" HeaderText="id" />  
  4.         <asp:BoundField DataField="name" HeaderText="name" />  
  5.         <asp:TemplateField ShowHeader="False">  
  6.             <ItemTemplate>  
  7.                 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"  
  8.                     Text="刪除" OnClientClick=‘<%#  "if (!confirm(\"你確定要刪除" + Eval("name").ToString() + "嗎?\")) return false;"%>‘></asp:LinkButton>  
  9.             </ItemTemplate>  
  10.         </asp:TemplateField>  
  11.     </Columns>  
  12.  </asp:GridView>  

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:BoundField DataField="id" HeaderText="id" /> <asp:BoundField DataField="name" HeaderText="name" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="刪除" OnClientClick=‘<%# "if (!confirm(\"你確定要刪除" + Eval("name").ToString() + "嗎?\")) return false;"%>‘></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns></asp:GridView>


C# code

  1. private void BindGridView()   
  2. {   
  3.     SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=Demo");   
  4.     SqlDataAdapter da = new SqlDataAdapter("select id, name from yourtable", cn);   
  5.     DataSet ds = new DataSet();   
  6.     cn.Open();   
  7.     da.Fill(ds);   
  8.     cn.Close();   
  9.     GridView1.DataSource = ds.Tables[0].DefaultView;   
  10.     GridView1.DataKeyNames = new string[] { "id" };   
  11.     GridView1.DataBind();   
  12. }   
  13.   
  14. protected void Page_Load(object sender, EventArgs e)   
  15. {   
  16.     if (!IsPostBack)   
  17.     {   
  18.         BindGridView();   
  19.     }   
  20. }   
  21.   
  22. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)   
  23. {   
  24.     string strSql = "delete from yourtable where id = @id";   
  25.     SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=Demo");   
  26.     SqlCommand cmd = new SqlCommand(strSql, cn);   
  27.     cmd.Parameters.Add("@id", SqlDbType.VarChar, 11).Value = GridView1.DataKeys[e.RowIndex].Value.ToString();   
  28.     cn.Open();   
  29.     cmd.ExecuteNonQuery();   
  30.     cn.Close();   
  31.     BindGridView();   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.