asp.net中有关gridview控件添加编辑按钮的示例(一)

本文介绍了如何在ASP.NET的GridView控件中实现编辑功能,包括添加编辑按钮、设置编辑样式、绑定数据源及更新数据库等内容。

微软为.net中的gridview添加了编辑,删除等处理事件,可是我们想要自己添加这些功能,下面我来介绍一下编辑按钮的示例,我用的是VS2008:

在gridview控件中的列编辑框中添加一列textbox的模板,命名为txt1,:编辑,更新,取消,命令列,首先它是系统生成的东西,当我们选中它时,在右则属性栏中其类型为Link,将其ButtonType调整为Button,并将其转化为TemplateField,然后为gridview控件添加Rowediting事件,并添加如下处理代码:

ExpandedBlockStart.gif代码
GridView2.EditIndex = e.NewEditIndex;
        
//当前编辑行背景色高亮
        this.GridView2.EditRowStyle.BackColor =  Color.FromName("#F7CE90");
        
string strSQL = "";
        strSQL 
= "select * from userliuyan ";
        
try
        {
            GridView2.DataSource 
= ws.GetWritersBig(strSQL, CommandType.Text, null);
            GridView2.DataBind();
        }
        
catch (Exception)
        {

            
throw;
        }

 

添加更新事件:

ExpandedBlockStart.gif代码
string ID = GridView2.DataKeys[e.RowIndex].Values[0].ToString();
        
string answer = ((TextBox)GridView2.Rows[e.RowIndex].FindControl("txt1")).Text;
        
    
//判断表单项是否有空并给出提示信息
        if (answer == "" &&answer.Length>500)
        {
            Response.Write(
"<script>alert('请输入完整信息!')</script>");
            
return;
        }
        
string sqltxt = "update userliuyan set lAnswer='" + answer + "' where lId=" + ID;
        
int id = 0;
      
//把文本框中的内容更新到数据库不写了

       
if (id != 0)
       {
           Response.Write(
"<script language='javascript'>alert('数据已被保存!');</script>");
           
//返回浏览状态
           string strSQL = "";
           strSQL 
= "select * from userliuyan ";
           
try
           {
               GridView2.DataSource 
= ws.GetWritersBig(strSQL, CommandType.Text, null);
               GridView2.DataBind();
           }
           
catch (Exception)
           {

               
throw;
           }
       }

 

这样一个简单的编辑功能就完成了

转载于:https://www.cnblogs.com/gjs85/archive/2009/12/21/1629378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值