Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
I designed a DataList name DataListProduct --
In DataListProduct I create a <asp:ImageButton Id="ImgBtnCrt" runat="server" ImageUrl="~/imgcart/add-item.png" Width="35" Height="32" OnClick="ImgBtnCrt_OnClick" />

I want to if, I click on this image button the value of product is save in database.
Posted
Updated 19-Nov-13 22:34pm
v2
Comments
Er Daljeet Singh 20-Nov-13 4:41am    
means you have a image control in datalist.
Eng. Ashish Srivastava 20-Nov-13 4:55am    
yes I have a ImageButton control if I click on click event of ImageButton_Click i want to save data of product in sql
Er Daljeet Singh 20-Nov-13 5:08am    
i had post a solution just go through it.

1 solution

here i am posting code for custom datalist
in which updation ,deletion and editng is implemented
hope this help,

code for .aspx

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Datalist.aspx.cs" Inherits="Datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" align="center" Width="80%"
            oncancelcommand="DataList1_CancelCommand"
            ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
            onupdatecommand="DataList1_UpdateCommand">
            <HeaderTemplate>
                <table class="headertable" cellpadding="5" cellspacing="5">
                    <tr>
                        <th class="headertd">
                            Id
                        </th>
                        <th class="headertd">
                            Name
                        </th>
                        <th class="headertd">
                            Class
                        </th>
                        <th class="headertd">
                            Edit
                        </th>
                        <th class="headertd">
                            Delete
                        </th>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table class="headertable" cellpadding="5" cellspacing="7">
                    <tr>
                        <td class="itemtd">
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("class") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <table class="headertable" cellpadding="5" cellspacing="5">
                    <tr>
                        <td class="itemtd">
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("class") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
                        </td>
                    </tr>
                </table>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <table class="headertable" cellpadding="5" cellspacing="5">
                    <tr>
                        <td class="itemtd">
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                        </td>
                        <td class="itemtd">
                            <asp:TextBox ID="txt1" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
                        </td>
                        <td class="itemtd">
                            <asp:TextBox ID="txt2" runat="server" Text='<%#Eval("class") %>'></asp:TextBox>
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button1" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
                        </td>
                        <td class="itemtd">
                            <asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>




and here is the code for .aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class Datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ToString());
  
    protected void Page_Load(object sender, EventArgs e)
    {

    }
 
    /// <summary>
    /// In this method we use store procedure using sql.
    /// </summary>
    public void getdatasp()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("showdata", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = cmd.ExecuteReader();
        DataList1.DataSource = dr;
        DataList1.DataBind();
        dr.Close();
        con.Close();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {       
            getdatasp();  
    }

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {        
            DataList1.EditItemIndex = e.Item.ItemIndex;
            getdatasp();       
    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {      
            DataList1.EditItemIndex = -1;
            getdatasp();      
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int _id = Convert.ToInt32(((Label)e.Item.FindControl("Label1")).Text);
        string _name = ((TextBox)e.Item.FindControl("txt1")).Text;
        string _class = ((TextBox)e.Item.FindControl("txt2")).Text;
       
            con.Open();
            SqlCommand cmd = new SqlCommand("updatedata", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = _id;
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = _name;
            cmd.Parameters.Add("@class", SqlDbType.VarChar).Value = _class;
            cmd.ExecuteNonQuery();
            con.Close();
            DataList1.EditItemIndex = -1;
            getdatasp();
      
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int _id = Convert.ToInt32(((Label)DataList1.FindControl("Label1")).Text);       
            con.Open();
            SqlCommand cmd = new SqlCommand("deletedata", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = _id;
            cmd.ExecuteNonQuery();
            con.Close();
            DataList1.EditItemIndex = -1;
            getdatasp();       
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900