如何在DataList控件Item的事件中运行sql查询。 [英] How to run sql query in DataList control Item's event.
本文介绍了如何在DataList控件Item的事件中运行sql查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设计了一个DataList名称DataListProduct -
在DataListProduct中我创建了一个< asp:ImageButton Id =ImgBtnCrtrunat =serverImageUrl =〜/ imgcart / add-item。 pngWidth =35Height =32OnClick =ImgBtnCrt_OnClick/>
我想如果,我点击这个图片按钮的价值产品保存在数据库中。
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.
推荐答案
这里我发布了自定义数据列表的代码
,其中实现了更新,删除和编辑
希望这个帮助,
代码为.aspx
here i am posting code for custom datalist
in which updation ,deletion and editng is implemented
hope this help,
code for .aspx
<%@ 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
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();
}
}
这篇关于如何在DataList控件Item的事件中运行sql查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文