|
|
|
มีปัญหา Edit กับ Delete Data from Gridview ด้วย checkbox ครับ |
|
|
|
|
|
|
|
เวลา Compile แล้วในส่วนของ Edit ไม่สามารถแก้ไขข้อมูลได้ ส่วนของ Delete Error ว่า "Input string was not in a correct format." ขอบคุณครับ
โค้ต ASP ครับ
Code (ASP)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EditUpdateDelete.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID ="chkdel" runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate>
<asp:Label ID ="lb_ID" runat ="server" Text ='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Firstname">
<ItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#Eval("fname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%#Eval("fname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("lname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt2" runat="server" Text='<%#Eval("lname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID ="btn1" runat ="server" Text ="Edit" CommandName ="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID ="btn2" runat ="server" Text ="Update" CommandName ="Edit" />
<asp:Button ID ="btn3" runat ="server" Text ="Cancel" CommandName ="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
โค้ต c# ครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace EditUpdateDelete
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowData();
}
}
string strCon = WebConfigurationManager.ConnectionStrings["AdminRoleConnectionString"].ConnectionString;
protected void ShowData()
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
string sql = "select ID,fname,lname from AllUser";
SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
cmd.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
ShowData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label ID = GridView1.Rows[e.RowIndex].FindControl("lb_ID") as Label;
TextBox Firstname = GridView1.Rows[e.RowIndex].FindControl("txt1") as TextBox;
TextBox Lastname = GridView1.Rows[e.RowIndex].FindControl("txt2") as TextBox;
SqlConnection con = new SqlConnection(strCon);
con.Open();
string sql2 = "Update AllUser set fname='" + Firstname.Text + "', lname = '" + Lastname.Text + "' where ID =+ Convert.ToInt32(ID.Text)+";
SqlCommand cmd = new SqlCommand(sql2, con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
ShowData();
}
protected void DeleteRecord(int IDUser)
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("delete from AllUser where ID=@ID", con);
cmd.Parameters.AddWithValue("@ID", IDUser);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow grow in GridView1.Rows)
{
//Searching CheckBox("chkDel") in an individual row of Grid
CheckBox chkdel = (CheckBox)grow.FindControl("chkdel");
//If CheckBox is checked than delete the record with particular empid
if (chkdel.Checked)
{
int IDUser = Convert.ToInt32(grow.Cells[1].Text);
DeleteRecord(IDUser);
}
}
}
}
}
Tag : .NET, MySQL, Web (ASP.NET), C#
|
ประวัติการแก้ไข 2017-06-29 16:15:07
|
|
|
|
|
Date :
2017-06-29 16:12:22 |
By :
kanin9182 |
View :
759 |
Reply :
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|