Gridview中的编辑模板与项模板的用法
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" BorderColor="Black"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowdatabound="GridView1_RowDataBound" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate>
<%#Eval("CustomerID") %>
</ItemTemplate>
<EditItemTemplate>
<%#Eval("CustomerID") %>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Name">
<ItemTemplate>
<%#Eval("CompanyName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text ='<%#Eval("CompanyName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Contact">
<ItemTemplate>
<%#Eval("ContactName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text ='<%#Eval("ContactName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Address">
<ItemTemplate>
<%#Eval("Address") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text ='<%#Eval("Address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="City">
<ItemTemplate>
<%#Eval("City") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="编辑">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="编辑" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button2" runat="server" Text="修改" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="取消" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
public void databind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM CUSTOMERS", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[];
this.GridView1.DataKeyNames = new string[] { "CustomerID", "City" };
this.GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
databind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -;
databind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell item in e.Row.Cells)
{
item.Attributes.Add("style", "border-color:black"); }
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dr = (DropDownList)e.Row.FindControl("DropDownList1");
if (dr != null)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
SqlCommand cmd = new SqlCommand("Select distinct City from Customers", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dr.DataSource = ds.Tables[];
dr.DataTextField = "City";
dr.DataValueField = "City";
dr.DataBind();
dr.SelectedItem.Text = this.GridView1.DataKeys[e.Row.RowIndex].Values[].ToString();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
SqlCommand cmd = new SqlCommand("Update Customers set CompanyName=@CompanyName,ContactName=@ContactName,City=@City where CustomerID=@CustomerID", con);
SqlParameter[] ps = new SqlParameter[];
ps[] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,);
ps[] = new SqlParameter("@ContactName",SqlDbType.NVarChar,);
ps[] = new SqlParameter("@City",SqlDbType.NVarChar,);
ps[] = new SqlParameter("@CustomerID",SqlDbType.NChar,);
ps[].Value = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim();
ps[].Value = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text.Trim();
ps[].Value = ((DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedItem.Text;
ps[].Value = this.GridView1.DataKeys[e.RowIndex].Values[];
cmd.Parameters.AddRange(ps);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
this.GridView1.EditIndex = -;
databind();
}
Gridview中的编辑模板与项模板的用法的更多相关文章
- Win10系列:JavaScript 项目模板中的文件和项模板文件
通过上面内容的学习,相信读者已经对各种项目模板和项模板有了大致的了解,本节将进一步介绍项目模板中默认包含的项目文件以及项模板文件,首先讲解这些文件中的初始内容以及作用,然后介绍在一个页面中如何添加控件 ...
- Win10系列:JavaScript 项目模板和项模板
使用Visual Studio 开发Windows应用商店应用时,通过其提供的模板可以帮助我们快速地创建一个应用.其中,在新建一个Windows应用商店应用程序项目时可以在项目模板中选择所需要的模板类 ...
- ListBox项模板中绑定ListBoxItem属性的方法
原文:ListBox项模板中绑定ListBoxItem属性的方法 <ListBox> <ListBox.ItemTemplate> <DataTemplate> & ...
- WijmoJS 中自定义 React 菜单和列表项模板
WijmoJS 中自定义 React 菜单和列表项模板 在V2019.0 Update2 的全新版本中,React 框架下 WijmoJS 的前端UI组件功能再度增强. WijmoJS的菜单和类似列表 ...
- GridView 中Item项居中显示
直接在GridView中设置 android:gravity="center"这个属性是不起作用的.要在你adapter中的布局文件中设 置android:layout_gravi ...
- 扩展GridView控件——为内容项添加拖放及分组功能
引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组
[源码下载] 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 作者:webabcd 介绍背水一战 Windows 10 之 控件 ...
- GridView中显示时间日期格式问题
以下都是GridView基本常用的日期,时间格式 形式 语法 结果 注释 数字 {0:N2} 12.36 数字 {0:N0} 13 货币 {0:c2} $12.36 货币 {0:c4} $ ...
随机推荐
- 轻量级.Net ORM SqlSuger项目实战
SqlSuger,清垃圾ORM实战例子. //添加引用 using SqlSugar; //在构造函数中实例化SqlSuger clinet = new SqlSugarClient(new Conn ...
- 生成Uuid工具类
package com.freeter.util; import java.util.UUID; /** * @author liuqi * **/public class Uuid{ public ...
- Django Template语法中 OneToOne、ForeignKey 外键查询
主表的Models的结构 class A(models.Model): username = models.CharField(max_length=32, verbose_name='用户名称') ...
- django rest framework 解析器组件 接口设计,视图组件 (1)
一.解析器组件 -解析器组件是用来解析用户请求数据的(application/json), content-type 将客户端发来的json数据进行解析 -必须适应APIView -request.d ...
- python抓取网站提示错误ssl.SSLCertVerificationError处理
python在抓取制定网站的错误提示:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify ...
- robotframework连接mysql
1.安装mysql数据库,并启动,创建数据库test及user表(可以自定义) 2.配置robotframework环境加载DatabaseLibrary 3.安装pymysql,下载地址:https ...
- 21-C#笔记-名称空间
和C++不同的地方: 1. 访问内部成员的方式使用 点 namespace_name.item_name; 2. using 的语法 using System; 参考: http://www.runo ...
- Navicat Premium永久激活方式
今天安装完Navicat Premium,运行时出现只能用14天的信息,所以就比较生气,只能用14天,那我还下你干啥,所以就查教程,弄了个破解工具,最后大功告成,应该是可以永久使用了,具体的教程看下 ...
- ORA-25153错误及解决办法
出现下图错误 原因就是没有临时表空间,所以要建立临时表空间,下面的语句,记得把地址换成你自己想放的地方. alter tablespace temp add tempfile 'C:/temp.dbf ...
- 绘制matplotlib 饼状图
参考:https://blog.csdn.net/ScarlettYellow/article/details/80458797 (2)2016年就业人员在三次产业中分布的饼状图. def swap( ...