Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Asp.net datalist control

Asp.net datalist control

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelp
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Otekpo Emmanuel
    wrote on last edited by
    #1

    Hello friends, please I need your help in this stuff. I have a datalist control on my asp.net webpage with the following buttons(Add, Remove, Cancel). I'm able to code the the button add using OnItemCommand. But, I'm unable to code the one of Remove and Cancel to call the sub procedures I wrote in vb code behind. Also, I would like a specific record to be removed from the datalist control when the remove button is click. Please help. Thanks in advance!

    J 1 Reply Last reply
    0
    • O Otekpo Emmanuel

      Hello friends, please I need your help in this stuff. I have a datalist control on my asp.net webpage with the following buttons(Add, Remove, Cancel). I'm able to code the the button add using OnItemCommand. But, I'm unable to code the one of Remove and Cancel to call the sub procedures I wrote in vb code behind. Also, I would like a specific record to be removed from the datalist control when the remove button is click. Please help. Thanks in advance!

      J Offline
      J Offline
      joginder banger
      wrote on last edited by
      #2

      I here mention a complete code below in Data List Add,Remove and Update.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Data.SqlClient;
      using System.Data;

      public partial class mytesting : System.Web.UI.Page
      {

          protected void Page\_Load(object sender, EventArgs e)
          {
      
              if (!IsPostBack)
              {
                  GetData();
              }
          }
          private void GetData()
          {
      
              SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jogi;Integrated Security=True");
      
              try
              {
      
                  con.Open();
                  SqlCommand cmd = new SqlCommand("select \* from jogi2", con);
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataSet ds = new DataSet();
                  da.Fill(ds);
                  DataList1.DataSource = ds.Tables\[0\];
                  DataList1.DataBind();
              }
              finally
              {
                  con.Close();
              }
          }
          protected void DataList1\_EditCommand(object source, DataListCommandEventArgs e)
          {
              DataList1.EditItemIndex = e.Item.ItemIndex;
              GetData();
          }
          protected void DataList1\_CancelCommand(object source, DataListCommandEventArgs e)
          {
              DataList1.EditItemIndex = -1;
              GetData();
          }
          protected void DataList1\_UpdateCommand(object source, DataListCommandEventArgs e)
          {
              String id = ((Label)e.Item.FindControl("Label3")).Text;
              TextBox role = (TextBox)e.Item.FindControl("TextBox1");
              TextBox password = (TextBox)e.Item.FindControl("TextBox3");
              SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jogi;Integrated Security=True");
              con.Open();
              SqlCommand cmd = new SqlCommand("update jogi2 set role='" + role.Text + "',password='" + password.Text + "'where id ='" + id + "'", con);
              int i = cmd.ExecuteNonQuery();
              if (i > 0)
              {
                  Label4.Text = "Recard Updated successfully....";
                  GetData();
              }
          }
          protected void DataList1\_DeleteCommand(object source, DataListCommandEventArgs e)
          {
              String id = ((Label)e.Item.FindControl("Label1")).Text;
              SqlConnection con = new SqlConne
      
      O 1 Reply Last reply
      0
      • J joginder banger

        I here mention a complete code below in Data List Add,Remove and Update.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Data.SqlClient;
        using System.Data;

        public partial class mytesting : System.Web.UI.Page
        {

            protected void Page\_Load(object sender, EventArgs e)
            {
        
                if (!IsPostBack)
                {
                    GetData();
                }
            }
            private void GetData()
            {
        
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jogi;Integrated Security=True");
        
                try
                {
        
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select \* from jogi2", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataList1.DataSource = ds.Tables\[0\];
                    DataList1.DataBind();
                }
                finally
                {
                    con.Close();
                }
            }
            protected void DataList1\_EditCommand(object source, DataListCommandEventArgs e)
            {
                DataList1.EditItemIndex = e.Item.ItemIndex;
                GetData();
            }
            protected void DataList1\_CancelCommand(object source, DataListCommandEventArgs e)
            {
                DataList1.EditItemIndex = -1;
                GetData();
            }
            protected void DataList1\_UpdateCommand(object source, DataListCommandEventArgs e)
            {
                String id = ((Label)e.Item.FindControl("Label3")).Text;
                TextBox role = (TextBox)e.Item.FindControl("TextBox1");
                TextBox password = (TextBox)e.Item.FindControl("TextBox3");
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jogi;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("update jogi2 set role='" + role.Text + "',password='" + password.Text + "'where id ='" + id + "'", con);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    Label4.Text = "Recard Updated successfully....";
                    GetData();
                }
            }
            protected void DataList1\_DeleteCommand(object source, DataListCommandEventArgs e)
            {
                String id = ((Label)e.Item.FindControl("Label1")).Text;
                SqlConnection con = new SqlConne
        
        O Offline
        O Offline
        Otekpo Emmanuel
        wrote on last edited by
        #3

        Waooo, what a marvellous post! Banger, thank you very much. You post was easy to understand. Thanks alot...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups