GridView里面的内嵌的服务器控件如LinkButton如何传值?

      GridView里面的内嵌的服务器控件如LinkButton如何传值到LinkButton 事件中

< asp:GridView ID = " GridView1 "  runat = " server "  AllowPaging = " True "  AutoGenerateColumns = " False "
    CellPadding
= " 4 "  DataKeyNames = " Code "  DataSourceID = " ObjectDataSource1 "  ForeColor = " #333333 "
    GridLines
= " Vertical "  Width = " 100% "  PageSize = " 6 " >
    
< FooterStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
    
< Columns >
        
< asp:BoundField DataField = " publishType "  HeaderText = " 杂志类型 "   >
            
< ItemStyle HorizontalAlign = " Center "   />
        
</ asp:BoundField >
        
< asp:BoundField DataField = " magCode "  HeaderText = " 杂志编号 "   />
        
< asp:BoundField DataField = " Name "  HeaderText = " 杂志名称 "   />
        
< asp:BoundField DataField = " Volumn "  HeaderText = " 刊数 "  SortExpression = " Volumn "   />
        
< asp:BoundField DataField = " Code "  HeaderText = " 期刊编号 "  SortExpression = " Code "   />
        
< asp:CheckBoxField DataField = " CanPublish "  HeaderText = " 发布 "   />
        
< asp:BoundField DataField = " Category "  HeaderText = " 所属分类 "   />
        
< asp:BoundField DataField = " Author "  HeaderText = " 作者 "   />
         
< asp:BoundField DataField = " Brief "  HeaderText = " 摘要内容 "   />
          
< asp:TemplateField ShowHeader = " False " >
            
< ItemStyle HorizontalAlign = " Center "  Width = " 40px "   />
            
< ItemTemplate >  
                
< asp:HyperLink ID = " HyperLink1 "  runat = " server "  NavigateUrl = ' <%# Doker.WebSite.AppCode.UrlLinks.GetMagazineDetailPreview(Eval("PublishType"),Eval("Code")) %> '  Target = " _blank " > 预览 </ asp:HyperLink >
            
</ ItemTemplate >
        
</ asp:TemplateField >
        
< asp:TemplateField ShowHeader = " False " >
            
< ItemStyle HorizontalAlign = " Center "  Width = " 40px "   />
            
< ItemTemplate >
                
< asp:LinkButton ID = " LinkButton1 "  runat = " server "  CausesValidation = " False "  CommandName = " Generate "
                    OnClick
= " LinkButton1_Click "  Text = " 生成 "  CommandArgument = ' <%# Eval("Code") %> ' ></ asp:LinkButton >
            
</ ItemTemplate >
        
</ asp:TemplateField >
        
< asp:TemplateField ShowHeader = " False " >
            
< ItemStyle HorizontalAlign = " Center "  Width = " 40px "   />
            
< ItemTemplate >
                
< asp:LinkButton ID = " LinkButton2 "  runat = " server "  CausesValidation = " False "  CommandName = " Publish "
                    OnClick
= " LinkButton2_Click "  Text = " 发布 "  CommandArgument = ' <%# Eval("Code") %> ' ></ asp:LinkButton >
            
</ ItemTemplate >
        
</ asp:TemplateField >
    
</ Columns >
    
< RowStyle BackColor = " #F7F6F3 "  ForeColor = " #333333 "   />
    
< EditRowStyle BackColor = " #999999 "   />
    
< SelectedRowStyle BackColor = " #E2DED6 "  Font - Bold = " True "  ForeColor = " #333333 "   />
    
< PagerStyle BackColor = " #284775 "  ForeColor = " White "  HorizontalAlign = " Left "   />
    
< HeaderStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
    
< AlternatingRowStyle BackColor = " White "  ForeColor = " #284775 "   />
</ asp:GridView >
      从代码可以看出LinkButton是通过CommandArgument='<%# Eval("Code") %>属性把字段"Code"传给事件。
      在事件中又始何获取CommandArgument的值呢?如下代码:
string  _code  =  ((LinkButton)sender).CommandArgument;
      这边有一个小小的技巧是:点击LinkButton那一行的数据存储在GridViewRow中,然后再能过GridViewRow获取字段值,(注意sender就是LinkButton)代码如下:
      
         protected   void  LinkButton1_Click( object  sender, EventArgs e)
        
{
            LinkButton btn 
= sender as LinkButton;
            GridViewRow row 
= btn.Parent.Parent as GridViewRow;

            SeedMaker sm 
= new SeedMaker();

            
string publishType = string.Empty;
            
string magName = string.Empty;
            
string magBtype = string.Empty;
            
string magStype = string.Empty;
            
string magCode = string.Empty;
            
string magVol = string.Empty;
            
string volCode = string.Empty;
            
string magCate = string.Empty;
            
string content = string.Empty;
            
string author = string.Empty;

            publishType 
= row.Cells[0].Text;
            magCode 
= row.Cells[1].Text;
            magName 
= row.Cells[2].Text;
            magVol 
= row.Cells[3].Text;
            volCode 
= row.Cells[4].Text;
            magCate 
= row.Cells[6].Text;
            author 
= row.Cells[7].Text;
            content 
= row.Cells[8].Text;
}

你可能感兴趣的:(GridView)