asp.net 에서 DatagridView 의 첨삭 방법 을 사용 하여 구체 적 으로 실현 합 니 다.
9470 단어 DatagridView늘다삭제 하 다.고치다.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GPS_Web.Default" %>
<!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">
<div>
<!--GridView :onrowediting、onrowupdating、onrowcancelingedit、onrowdeleting--->
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:TemplateField HeaderText=" " Visible="False">
<ItemTemplate>
<asp:Label ID="lblNum" runat="server" Text='<%# Eval("Num") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" ">
<EditItemTemplate>
<asp:TextBox ID="txtTableName" runat="server" Text='<%# Eval("TableName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("TableName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" ">
<EditItemTemplate>
<asp:TextBox ID="txtTextName" runat="server" Text='<%# Eval("TextName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TextName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText=" " ShowSelectButton="True" />
<asp:CommandField HeaderText=" " ShowEditButton="True" />
<asp:CommandField HeaderText=" " ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text=" " />
<br />
<br />
</div>
</form>
</body>
</html>
default.aspx.cs 페이지 코드:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPS_Web.ywpages.DAL;
using System.Data;
namespace GPS_Web
{
/// <summary>
/// :http://blog.csdn.net/wanglei_samrtfish/article/details/8070480
/// </summary>
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridViewBind();
}
}
private void GridViewBind()
{
string sql = "select Num,TableName,TextName from dbo.GroupType_Demo";
try
{ //
GridView1.DataSource = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0];
GridView1.DataBind();
}
catch (Exception ex){}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{ //gridview
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
/// <summary>
///
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ //
int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
//
string TableName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTableName")).Text;
string TextName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTextName")).Text;
//
string sql = string.Format("update dbo.GroupType_Demo set TableName='{0}',TextName='{1}' where Num={2}",TableName,TextName,Num);
try
{
int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
if (i > 0)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
}
GridView1.EditIndex = -1;
GridViewBind();
}
/// <summary>
///
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//
int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
//
string sql = string.Format("delete dbo.GroupType_Demo where num={0}", Num);
try
{
int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
if (i > 0)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert(' !')</script>");
} www.jb51.net
GridView1.EditIndex = -1;
GridViewBind();
}
/// <summary>
///
/// </summary>
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default_Add.aspx");
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//
int i = e.NewSelectedIndex;
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
btnAdd.Text = " " + (i+1) + " 。";
}
}
}
실 행 된 페이지 효과:
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
python에서 사전 추가 및 삭제 사용 방법추가 작업: 변수 이름 [key] = value # 키를 통해value 값을 추가하고 키가 존재하면 덮어씁니다. setdefault (key,default) # 키와value를 지정하고 키가 존재하면 덮어쓰지 않습니...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.