编辑多行 GridView

<% @ Page Language = " C# "  Debug = " true "   %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script  runat ="server" >
string[] ClassNameArray 
=  {  " 大学 " " 中学 " " 高中 "  };

// 初始化原来的值
protected  void  GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{    
  
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
  {
    System.Data.DataRowView drv 
=  (System.Data.DataRowView)e.Row.DataItem;
    RadioButtonList rbl 
=  (RadioButtonList)e.Row.FindControl( " txtGender " );
    
if  (rbl  !=   null )
    {
      
if  ((bool)drv[ " Gender " ])
      {
        rbl.Items.FindByText(
" " ).Selected  =   true ;
      }
      
else
      {
        rbl.Items.FindByText(
" " ).Selected  =   true ;
      }
    }

    DropDownList ddl 
=  (DropDownList)e.Row.FindControl( " txtClassName " );
    
if  (ddl  !=   null )
    {
      ddl.Items.FindByText(drv[
" ClassName " ].ToString()).Selected  =   true ;
    }
  }
}

//  初始化项目列表
protected  void  GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{    
  
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
  {
    DropDownList ddl 
=  (DropDownList)e.Row.FindControl( " txtClassName " );
    ddl.DataSource 
=  ClassNameArray;
    ddl.DataBind();
  }
}

// 处理提交的数据
protected  void  XianhuiMeng_Click(object sender, EventArgs e)
{
  foreach (GridViewRow gvr 
in  GridView1.Rows)
  {
    string tGender 
=  ((RadioButtonList)gvr.FindControl( " txtGender " )).SelectedValue;
    SqlDataSource1.UpdateParameters.Add(
new  Parameter( " @Title " , TypeCode.String, ((TextBox)gvr.FindControl( " txtTitle " )).Text));
    SqlDataSource1.UpdateParameters.Add(
new  Parameter( " @Gender " , TypeCode.Boolean, (tGender  ==   " "   ?   " True "  :  " False " )));
    SqlDataSource1.UpdateParameters.Add(
new  Parameter( " @ClassName "
                   TypeCode.String, ((DropDownList)gvr.FindControl(
" txtClassName " )).SelectedValue));
    SqlDataSource1.UpdateParameters.Add(
new  Parameter( " @id " , TypeCode.Int32, GridView1.DataKeys[gvr.DataItemIndex].Value.ToString()));
    SqlDataSource1.Update();
    SqlDataSource1.UpdateParameters.Clear();
  }
  Response.Redirect(Request.UrlReferrer.ToString());
}
</ script >

< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  id ="Head1"  runat ="server" >
< title > 同时编辑 GridView的 多行 </ title >
</ head >
< body >
< form  id ="form1"  runat ="server" >
  
< asp:GridView  ID ="GridView1"  runat ="server"  Font-Size ="12px"  CellPadding ="3"  AutoGenerateColumns ="False"
    DataKeyNames
="id"  DataSourceID ="SqlDataSource1"  OnRowDataBound ="GridView1_RowDataBound"
    OnRowCreated
="GridView1_RowCreated" >
    
< HeaderStyle  BackColor ="#EDEDED"   />
    
< Columns >
      
< asp:TemplateField  HeaderText ="姓名" >
        
< ItemTemplate >
          
< asp:TextBox  ID ="txtTitle"  runat ="server"  Text ='<%#Eval("Title")  % > '> </ asp:TextBox >
        
</ ItemTemplate >
      
</ asp:TemplateField >
      
< asp:TemplateField  HeaderText ="性别" >
        
< ItemTemplate >
          
< asp:RadioButtonList  ID ="txtGender"  runat ="server"  RepeatColumns ="2" >
            
< asp:ListItem  Text ='男' ></ asp:ListItem >
            
< asp:ListItem  Text ='女' ></ asp:ListItem >
          
</ asp:RadioButtonList >
        
</ ItemTemplate >
      
</ asp:TemplateField >
      
< asp:TemplateField  HeaderText ="学历" >
        
< ItemTemplate >
          
< asp:DropDownList  ID ="txtClassName"  runat ="server" >
          
</ asp:DropDownList >
        
</ ItemTemplate >
      
</ asp:TemplateField >
    
</ Columns >
  
</ asp:GridView >
  
< asp:SqlDataSource  ID ="SqlDataSource1"  runat ="server"  
    ConnectionString
="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|ASPNET20Book.mdb;Persist Security Info=True"
    ProviderName
="System.Data.OleDb"  SelectCommand ="SELECT * FROM [Student]"  
    UpdateCommand
="Update [Student] Set Title = @Title,Gender = @Gender,ClassName=@ClassName Where id=@id" >
  
</ asp:SqlDataSource >
  
< asp:Button  ID ="Button1"  runat ="server"  Text ="保存所有修改"  OnClick ="XianhuiMeng_Click"   />
</ form >
</ body >
</ html >
<% @ Page Language = " C# "  Debug = " true "   %>
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >
string [] ClassNameArray  =   "大学""中学""高中" } ;

// 初始化原来的值
protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
{    
  
if (e.Row.RowType == DataControlRowType.DataRow)
  
{
    System.Data.DataRowView drv 
= (System.Data.DataRowView)e.Row.DataItem;
    RadioButtonList rbl 
= (RadioButtonList)e.Row.FindControl("txtGender");
    
if (rbl != null)
    
{
      
if ((bool)drv["Gender"])
      
{
        rbl.Items.FindByText(
"").Selected = true;
      }

      
else
      
{
        rbl.Items.FindByText(
"").Selected = true;
      }

    }


    DropDownList ddl 
= (DropDownList)e.Row.FindControl("txtClassName");
    
if (ddl != null)
    
{
      ddl.Items.FindByText(drv[
"ClassName"].ToString()).Selected = true;
    }

  }

}


//  初始化项目列表
protected   void  GridView1_RowCreated( object  sender, GridViewRowEventArgs e)
{    
  
if (e.Row.RowType == DataControlRowType.DataRow)
  
{
    DropDownList ddl 
= (DropDownList)e.Row.FindControl("txtClassName");
    ddl.DataSource 
= ClassNameArray;
    ddl.DataBind();
  }

}


// 处理提交的数据
protected   void  XianhuiMeng_Click( object  sender, EventArgs e)
{
  
foreach (GridViewRow gvr in GridView1.Rows)
  
{
    
string tGender = ((RadioButtonList)gvr.FindControl("txtGender")).SelectedValue;
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@Title", TypeCode.String, ((TextBox)gvr.FindControl("txtTitle")).Text));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@Gender", TypeCode.Boolean, (tGender == "" ? "True" : "False")));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@ClassName"
                   TypeCode.String, ((DropDownList)gvr.FindControl(
"txtClassName")).SelectedValue));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@id", TypeCode.Int32, GridView1.DataKeys[gvr.DataItemIndex].Value.ToString()));
    SqlDataSource1.Update();
    SqlDataSource1.UpdateParameters.Clear();
  }

  Response.Redirect(Request.UrlReferrer.ToString());
}

</ script >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head id = " Head1 "  runat = " server " >
< title > 同时编辑 GridView的 多行 </ title >
</ head >
< body >
< form id = " form1 "  runat = " server " >
  
< asp:GridView ID = " GridView1 "  runat = " server "  Font - Size = " 12px "  CellPadding = " 3 "  AutoGenerateColumns = " False "
    DataKeyNames
= " id "  DataSourceID = " SqlDataSource1 "  OnRowDataBound = " GridView1_RowDataBound "
    OnRowCreated
= " GridView1_RowCreated " >
    
< HeaderStyle BackColor = " #EDEDED "   />
    
< Columns >
      
< asp:TemplateField HeaderText = " 姓名 " >
        
< ItemTemplate >
          
< asp:TextBox ID = " txtTitle "  runat = " server "  Text = ' <%#Eval("Title") %> ' ></ asp:TextBox >
        
</ ItemTemplate >
      
</ asp:TemplateField >
      
< asp:TemplateField HeaderText = " 性别 " >
        
< ItemTemplate >
          
< asp:RadioButtonList ID = " txtGender "  runat = " server "  RepeatColumns = " 2 " >
            
< asp:ListItem Text = ' ' ></ asp:ListItem >
            
< asp:ListItem Text = ' ' ></ asp:ListItem >
          
</ asp:RadioButtonList >
        
</ ItemTemplate >
      
</ asp:TemplateField >
      
< asp:TemplateField HeaderText = " 学历 " >
        
< ItemTemplate >
          
< asp:DropDownList ID = " txtClassName "  runat = " server " >
          
</ asp:DropDownList >
        
</ ItemTemplate >
      
</ asp:TemplateField >
    
</ Columns >
  
</ asp:GridView >
  
< asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  
    ConnectionString
= " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|ASPNET20Book.mdb;Persist Security Info=True "
    ProviderName
= " System.Data.OleDb "  SelectCommand = " SELECT * FROM [Student] "  
    UpdateCommand
= " Update [Student] Set Title = @Title,Gender = @Gender,ClassName=@ClassName Where id=@id " >
  
</ asp:SqlDataSource >
  
< asp:Button ID = " Button1 "  runat = " server "  Text = " 保存所有修改 "  OnClick = " XianhuiMeng_Click "   />
</ form >
</ body >
</ html >
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、表头表尾 1、自定义多行表头,列合并,行合并 2、删除表身指定列、行 3、固定表头,向下拉动滚动条时,表头固定不动 4、表脚行统计,总计、平均值 二、导入导出 1、导出Excel,Word 2、导入Excel 三、多层嵌套 1、在父GridView中的编辑模板中嵌套一个子GridView 2、三层GridView嵌套 四、分页排序 1、分页 2、列排序,点击列升序、降序排序 五、结合控件 1、CheckBox控件,多选、全选 2、DropDownList控件 3、隐藏控件,当选择处理状态选中备注时,隐藏textbox显示 4、radio控件,选中获取GridView表主键 5、GridView中DropDownList绑定数据,直接绑定显示,无需点击编辑按纽。 6、JavaScript操作checbox实现全选,多选 六、设置属性 1、JavaScript设置GridView行的背景颜色,单偶行的背景颜色,鼠标停留行背景,鼠标选中时的行背景 2、GridView的JavaScript中的行单击,双击、删除提示框、快捷键事件 3、GridView设置属性,单元格文本颜色,单元格背景颜色,表中增加空行 七、无代码 GridView排序、发送邮件、点击行查看详情,内容过长截取 八、选增删改 1、GridView自带的选中、编辑、删除,即CommandField 2、GridView添加记录,在GridView表脚添加控件,用添加记录 3、更新所有记录,GridView直接绑定控件,然后更新 九、主键索引 根据主键多条记录删除,单条记录删除。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值