GridView_Updating

 

以前做Asp.Net 1.X是经常会碰到处理列表控件的提示情况,现在开始学习Asp.Net2.0了, 用GridView完成相似的事情,更新和删除显示提示信息,确认后使用SqlDataSource 完成数据库操作的 

 

 

<% @ Page Language = " C# "   %>

< script runat = " server " >

    
void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    {
        
switch  (e.CommandName.ToLower())
        {
            
case   " edit " :
                GridView1.Columns[
0 ].Visible  =   false ;
                GridView1.Columns[
1 ].Visible  =   true ;
                
break ;
            
case   " update " :
            
case   " delete " :
            
case   " cancel " :
                GridView1.Columns[
0 ].Visible  =   true ;
                GridView1.Columns[
1 ].Visible  =   false ;
                
break ;
            
default :
                
//  Do nothing.
                 break ;
        }
    }

    
void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
    {
        
int  index  =  GridView1.EditIndex;
        GridViewRow row 
=  GridView1.Rows[index];

        TextBox firstNameTextBox 
=  (TextBox)row.Cells[ 1 ].FindControl( " FirstNameTextBox " );

        String firstName 
=   "" ;
        
if  (firstNameTextBox  !=   null )
        {
            firstName 
=  firstNameTextBox.Text;
        }

        TextBox lastNameTextBox 
=  (TextBox)row.Cells[ 2 ].FindControl( " LastNameTextBox " );

        String lastName 
=   "" ;
        
if  (lastNameTextBox  !=   null )
        {
            lastName 
=  lastNameTextBox.Text;
        }

        Parameter lastNameParameter 
=   new  Parameter( " au_lname " , TypeCode.String, lastName);
        Parameter firstNameParameter 
=   new  Parameter( " au_fname " , TypeCode.String, firstName);

        SqlDataSource1.UpdateParameters.Clear();
        SqlDataSource1.UpdateParameters.Add(lastNameParameter);
        SqlDataSource1.UpdateParameters.Add(firstNameParameter);
    }

    
void  GridView1_RowDeleting( object  sender, GridViewDeleteEventArgs e)
    {
        SqlDataSource1.DeleteParameters.Clear();
    }
    


< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > GridView更新和删除显示提示信息 -  王智 - 2006 </ title >


    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:GridView ID = " GridView1 "  runat = " server "
            DataSourceID
= " SqlDataSource1 "  AutoGenerateColumns = " False "  DataKeyNames = " au_id "  OnRowCommand = " GridView1_RowCommand "  OnRowUpdating = " GridView1_RowUpdating "  OnRowDeleting = " GridView1_RowDeleting " >
            
< Columns >
                
< asp:TemplateField >
                    
< ItemTemplate >
                        
< asp:LinkButton ID = " Edit "  runat = " server "  CommandName = " Edit " > 编辑 </ asp:LinkButton >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField Visible = " False " >
                    
< ItemTemplate >
                        
< asp:LinkButton ID = " Update "  runat = " server "  CommandName = " Update "  OnClientClick = " return confirm('确定要更新?'); " > 更新 </ asp:LinkButton >& nbsp; & nbsp;
                        
< asp:LinkButton ID = " Delete "  runat = " server "  CommandName = " Delete "  OnClientClick = " return confirm('确定要删除'); " > 删除 </ asp:LinkButton >& nbsp; & nbsp;
                        
< asp:LinkButton ID = " Cancel "  runat = " server "  CommandName = " Cancel " > 取消 </ asp:LinkButton >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:templatefield headertext = " Last Name " >
                    
< itemtemplate >
                        
<% #Eval( " au_lname " %>
                    
</ itemtemplate >
                    
< edititemtemplate >
                        
< asp:textbox id = " LastNameTextBox "  text = ' <%#Eval("au_lname") %> '
                            width
= " 175 "  runat = " server " />
                        
< br />
                        
< asp:requiredfieldvalidator id = " LastNameRequiredValidator "  controltovalidate = " LastNameTextBox "  
                            errormessage
= " Please enter a last name. "  validationgroup = " NameGroup "  runat = " server " />  
                    
</ edititemtemplate >
                
</ asp:templatefield >
                
                
< asp:templatefield headertext = " First Name " >
                    
< itemtemplate >
                        
<% #Eval( " au_fname " %>
                    
</ itemtemplate >
                    
< edititemtemplate >
                          
< asp:textbox id = " FirstNameTextBox "  text = ' <%#Eval("au_fname") %> '
                            width
= " 175 "  runat = " server " />
                          
< br />
                          
< asp:requiredfieldvalidator id = " FirstNameRequiredValidator "  controltovalidate = " FirstNameTextBox "
                                errormessage
= " Please enter a first name. "  validationgroup = " NameGroup "  runat = " server " />
                     
</ edititemtemplate >
                
</ asp:templatefield >
            
</ Columns >
        
</ asp:GridView >
        
< asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  ConnectionString = " <%$ ConnectionStrings:Pubs2000 %> "
            ProviderName
= " <%$ ConnectionStrings:Pubs2000.ProviderName %> "  SelectCommand = " SELECT au_id, au_lname, au_fname FROM authors "  
            UpdateCommand
= " UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE (au_id = @au_id) "  
            DeleteCommand
= " DELETE FROM authors WHERE (au_id = @au_id) " >
        
</ asp:SqlDataSource >
    
</ div >
    
</ form >


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值