GridView编辑、删除 gridview编辑模板

前台:
<asp:GridView ID="GridView1"runat="server" DataKeyNames="ID"
OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_CancelingEdit"
OnRowDeleting="GridView1_RowDeleting" PageSize="15"AllowPaging="true"OnPageIndexChanging="GridView1_PageIndexChanging"
ShowHeader="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="ID"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("ID") %>‘ID="Label0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="姓名"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("UserName") %>‘ID="Label2"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBoxText=‘<%#Bind("UserName") %>‘runat="server"ID="UserName"></asp:TextBox><asp:RequiredFieldValidator
runat="server" ErrorMessage="不能为空" ControlToValidate="UserName"Display="Dynamic">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="金额"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("Money") %>‘ID="Label33"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBoxText=‘<%#Bind("Money")%>‘runat="server"ID="Money"></asp:TextBox>
<asp:RequiredFieldValidatorID="RequiredFieldValidator2" runat="server"ErrorMessage="不能为空"
ControlToValidate="Money"Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidatorID="RegularExpressionValidator1" runat="server"ErrorMessage="整数或浮点数"
ValidationExpression="^d*$|[1-9]d*.d*|0.d*[1-9]d*$"ControlToValidate="Money"
Display="Dynamic">*</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"ShowEditButton="True" HeaderText="操作"/>
</Columns>
<HeaderStyle BackColor="blue" ForeColor="white"/>
</asp:GridView>

后台代码:
string myConnectionString =ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
SqlConnection myConnection = new SqlConnection();
protectedvoid Page_Load(object sender, EventArgs e)
{
myConnection.ConnectionString =myConnectionString;
if (!IsPostBack)
{
T();
}
}

public void T()
{
myConnection.Open();
string sql = "Select * From Test";
SqlDataAdapter da = new SqlDataAdapter(sq l,myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
myConnection.Close();
}

public void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{

myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID =int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
string userName =((TextBox)row.FindControl("UserName")).Text.ToString();
double Money =Convert.ToDouble(((TextBox)row.FindControl("Money")).Text.ToString());
SqlDataReader dr;
string sql = "Update Test Set UserName=‘" + userName + "‘,Money=" +Money + " Where ID=" + ID + "";
SqlCommand myCommand = new SqlCommand(sql,myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}

public void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID =int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
SqlDataReader dr;
string sql = "Delete From Test Where ID=" + ID +"";
SqlCommand myCommand = new SqlCommand(sql,myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}

public void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
T();
}

GridView编辑、删除 gridview编辑模板

public void GridView1_CancelingEdit(object sender,GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
T();
}

protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
T();
}

  

爱华网本文地址 » http://www.aihuau.com/a/25101015/262112.html

更多阅读

Windows 强制删除文件及文件夹命令 强制删除文件夹

Windows 强制删除文件及文件夹命令一、删除文件或目录CMD命令:rd/s/q D:app ?(强制删除文件文件夹和文件夹内所有文件)del/f/s/q D:app.txt ? ?(强制删除文件,文件名必须加文件后缀名)二、删除文件或目录BAT命令:1、新建.BAT批处理文件

艺匠、知羽、金夫人精品模板下载 韩国艺匠和金夫人

为了方便大家使用Flash电子相册制作系统艺匠、知羽、金夫人,特别在此集中艺匠、知羽、金夫人的精品模板供大家下载;由于模板文件较大,上传需要很长时间,所以不定期上传,敬请关注!艺匠flash电子相册系统完美破解版主程序下载知羽flash电子

声明:《GridView编辑、删除 gridview编辑模板》为网友社会演员多分享!如侵犯到您的合法权益请联系我们删除