GridViewCommandEventArgs 事件参数没有包含用于指示单击按钮所在行的属性?

datagrid、datalist、repeat的ItemCommand 事件参数都有一个Item属性可以得到激发当前事件控件所在行,但是GridView的GridViewCommandEventArgs 却没有这个属性

不过,MSDN也说明了解决方案:

注意

GridViewCommandEventArgs 类未包含一个用于指示单击按钮所在行的属性。如果需要知道哪个行引发了事件,请使用 CommandArgument 属性将行的索引传给事件处理方法。

同时也,提供一个实例:
None.gif<%@ Page language="C#" %>
None.gif
None.gif
<script runat="server">
None.gif
None.gif  
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
// If multiple buttons are used in a GridView control, use the
InBlock.gif    
// CommandName property to determine which button was clicked.
InBlock.gif
    if(e.CommandName=="Add")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
// Convert the row index stored in the CommandArgument
InBlock.gif      
// property to an Integer.
InBlock.gif
      int index = Convert.ToInt32(e.CommandArgument);
InBlock.gif            
InBlock.gif      
// Retrieve the row that contains the button clicked 
InBlock.gif      
// by the user from the Rows collection.
InBlock.gif
      GridViewRow row = CustomersGridView.Rows[index];
InBlock.gif            
InBlock.gif      
// Create a new ListItem object for the customer in the row.     
InBlock.gif
      ListItem item = new ListItem();
InBlock.gif      item.Text 
= Server.HtmlDecode(row.Cells[2].Text);
InBlock.gif            
InBlock.gif      
// If the customer is not already in the ListBox, add the ListItem 
InBlock.gif      
// object to the Items collection of the ListBox control. 
InBlock.gif
      if (!CustomersListBox.Items.Contains(item))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        CustomersListBox.Items.Add(item);
ExpandedSubBlockEnd.gif      }
           
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
InBlock.gif    
// The GridViewCommandEventArgs class does not contain a 
InBlock.gif    
// property that indicates which row's command button was
InBlock.gif    
// clicked. To identify which row's button was clicked, use 
InBlock.gif    
// the button's CommandArgument property by setting it to the 
InBlock.gif    
// row's index.
InBlock.gif
    if(e.Row.RowType == DataControlRowType.DataRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
// Retrieve the LinkButton control from the first column.
InBlock.gif
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
InBlock.gif          
InBlock.gif      
// Set the LinkButton's CommandArgument property with the
InBlock.gif      
// row's index.
InBlock.gif
      addButton.CommandArgument = e.Row.RowIndex.ToString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif  }

None.gif    
None.gif
</script>
None.gif
None.gif
<html>
None.gif  
<body>
None.gif    
<form runat="server">
None.gif        
None.gif      
<h3>GridView RowCommand Example</h3>
None.gif            
None.gif      
<table width="100%">         
None.gif        
<tr>                
None.gif          
<td width="50%">
None.gif                    
None.gif            
<asp:gridview id="CustomersGridView" 
None.gif              datasourceid
="CustomersSource"
None.gif              allowpaging
="true" 
None.gif              autogeneratecolumns
="false"
None.gif              onrowcommand
="CustomersGridView_RowCommand"
None.gif              onrowcreated
="CustomersGridView_RowCreated"  
None.gif              runat
="server">
None.gif                
None.gif              
<columns>
None.gif                
<asp:buttonfield buttontype="Link" 
None.gif                  commandname
="Add" 
None.gif                  text
="Add"/>
None.gif                
<asp:boundfield datafield="CustomerID" 
None.gif                  headertext
="Customer ID"/>
None.gif                
<asp:boundfield datafield="CompanyName" 
None.gif                  headertext
="Company Name"/> 
None.gif                
<asp:boundfield datafield="City" 
None.gif                  headertext
="City"/>         
None.gif              
</columns>
None.gif                
None.gif            
</asp:gridview>
None.gif                    
None.gif          
</td>
None.gif                    
None.gif          
<td valign="top" width="50%">
None.gif                    
None.gif            Customers: 
<br/>
None.gif            
<asp:listbox id="CustomersListBox"
None.gif              runat
="server"/> 
None.gif                    
None.gif          
</td>  
None.gif        
</tr>      
None.gif      
</table>
None.gif            
None.gif      
<!-- This example uses Microsoft SQL Server and connects  -->
None.gif      
<!-- to the Northwind sample database. Use an ASP.NET     -->
None.gif      
<!-- expression to retrieve the connection string value   -->
None.gif      
<!-- from the Web.config file.                            -->
None.gif      
<asp:sqldatasource id="CustomersSource"
None.gif        selectcommand
="Select [CustomerID], [CompanyName], [City] From [Customers]"
None.gif        connectionstring
="<%$ ConnectionStrings:NorthWindConnectionString%>" 
None.gif        runat
="server"/>
None.gif            
None.gif    
</form>
None.gif  
</body>
None.gif
</html>

让人非常遗憾的是,为什么ASPNET不提供这个属性,这个属性应该是很常用的01.gif,难道是又一个疏忽?
按新版控件GridView的命名习惯,如果提供一个GridViewRow类型的Row属性,对于我们开发人员就方便多了,
省去了手动添加RowIndex,然后还要解析出来,当ComandArg参数多的时候更麻烦

当然对于简单的情况可以使用声名的方式绑定RowIndex,也就不需要在Created事件处理:
None.gif<asp:imagebutton skinid="ChangeOrder" commandargument="<%# ((GridViewRow)Container).RowIndex %>" runat="server" id="btnChangeOrder" imageurl="~/Designer/images/updown.gif" causesvalidation="false" commandname="ChangeOrder"/>                    

发布了134 篇原创文章 · 获赞 44 · 访问量 18万+

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览