รบกวนช่วยดูทีครับ แต่ผมใช้ DataTable ครับ กด Edit ก็ไม่มีไรเกิดขึ้นครับ กด Delete ก็ Error ก็ Add ก็ Error
ผมทำตาม ตัวอย่างนี้ครับ
https://www.thaicreate.com/asp.net/c-sharp-asp.net-datagrid-control-visual-studio-2005-framework2.0.html
แต่ผมใช้ DataTable ครับ
กด Edit ก็ไม่มีไรเกิดขึ้นครับ กด Delete ก็ Error ก็ Add ก็ Error ครับ ไม่ทราบว่าผมผิดตรงไหนหรอครับ ช่วยดูทีครับ
.aspx/head]
<%
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication3.WebForm2"%>
<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="801px" ShowFooter="True"
DataKeyNames="id"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing"
OnRowDeleting="GridView1_RowDeleting"
OnRowUpdating="GridView1_RowUpdating"
>
<Columns>
<asp:TemplateField HeaderText="รหัส">
<EditItemTemplate>
<asp:TextBox ID="EditID" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="AddId" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อ">
<EditItemTemplate>
<asp:TextBox ID="EditName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="AddName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="นามสกุล">
<EditItemTemplate>
<asp:TextBox ID="EditSurname" runat="server" Text='<%# Bind("surname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="AddSurname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="อายุ">
<EditItemTemplate>
<asp:TextBox ID="EditAge" runat="server" Text='<%# Bind("age") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="AddAge" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Bind("age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="เบอร์โทร">
<EditItemTemplate>
<asp:TextBox ID="EditTel" runat="server" Text='<%# Bind("tel") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="AddTel" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblTel" runat="server" Text='<%# Bind("tel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField FooterText="เพิ่มรายการ" HeaderText="แก้ไข"
ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="เพิ่มรายการ" CommandName="Add"/>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
%>
[head].cs
DataTable dt;
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
// BindData();
//}
dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("surname");
dt.Columns.Add("age");
dt.Columns.Add("tel");
dr = dt.NewRow();
dr["id"] = "1";
dr["name"] = "1";
dr["surname"] = "1";
dr["age"] = "1";
dr["tel"] = "1";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void BindData()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
TextBox EditID = (TextBox)GridView1.FooterRow.FindControl("EditID");
TextBox EditName = (TextBox)GridView1.FooterRow.FindControl("EditName");
TextBox EditSurname = (TextBox)GridView1.FooterRow.FindControl("EditSurname");
TextBox EditAge = (TextBox)GridView1.FooterRow.FindControl("EditAge");
TextBox EditTel = (TextBox)GridView1.FooterRow.FindControl("EditTel");
dr = dt.NewRow();
dr["id"] = EditID.Text;
dr["name"] = EditName.Text;
dr["surname"] = EditSurname.Text;
dr["age"] = EditAge.Text;
dr["tel"] = EditTel.Text;
dt.Rows.Add(dr);
BindData();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.ShowFooter = false;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox EditID = (TextBox)GridView1.FooterRow.FindControl("EditID");
TextBox EditName = (TextBox)GridView1.FooterRow.FindControl("EditName");
TextBox EditSurname = (TextBox)GridView1.FooterRow.FindControl("EditSurname");
TextBox EditAge = (TextBox)GridView1.FooterRow.FindControl("EditAge");
TextBox EditTel = (TextBox)GridView1.FooterRow.FindControl("EditTel");
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)]["id"] = EditID.Text;
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)]["nameame"] = EditName.Text;
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)]["surname"] = EditSurname.Text;
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)]["age"] = EditAge.Text;
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)]["tel"] = EditTel.Text;
GridView1.EditIndex = -1;
GridView1.ShowFooter = true;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.ShowFooter = true;
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dt.Rows[Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)].Delete();
BindData();
}
นี่ Error ตอน Add ครับ
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. Tag : - - - -
Date :
2009-06-08 17:00:46
By :
hassadin
View :
2013
Reply :
2
อ่อ ผมลืมจับ DataTable ใส่ Session ครับ
ตอนนี้ แสดงผล Add เพิ่มได้แล้วครับ แต่ Update กับ Delete
มัน Error บรรทัดนี้
dt.Rows[Convert.ToInt32(myDataGrid.DataKeys[e.Item.ItemIndex])]["CustomerID"] = txtCustomerID.Text;
แบบนี้อ่ะครับ
There is no row at position 1.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: There is no row at position 1.
Date :
2009-06-09 00:41:19
By :
hassadin
กำ ได้แล้วครับ
พอโพสปุ๊บนึงออกปั๊บเลย
DataTable มันนับ Row เริ่มที่ 0 แต่ Id ที่ผมส่งไปมันเป็น 1 เลยต้องนำมาลบ -1 ด้วย
dt.Rows[Convert.ToInt32(myDataGrid.DataKeys[e.Item.ItemIndex])-1].Delete();
https://www.thaicreate.com/dotnet/forum/039394.html
Date :
2009-06-09 00:45:47
By :
hassadin
Load balance : Server 01