GridView模板列 \ HyperLink \ LinkButton 演练

 

 

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ProductID,ProductName" 
          AutoGenerateColumns
=False onrowcommand="GridView1_RowCommand" 
            onrowdatabound
="GridView1_RowDataBound"  >
            
<Columns>
                
<asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                    SortExpression
="ProductID" />
                
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
                    
<ItemTemplate>
                        
<asp:LinkButton runat=server CommandName=ProcName  Text='<%# Eval("ProductName") %>' CommandArgument='<%#Eval("ProductID")%>'></asp:LinkButton>
                        
<href="Default.aspx?id=<%#Eval("ProductID")%>&name=<%Eval("ProductName"%>" target=_blank><%Eval("ProductName"%></a>
                        
<asp:HyperLink ID="link" runat="server" Text='<%# Eval("ProductName") %>' NavigateUrl="~/Default.aspx" ></asp:HyperLink>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                    SortExpression
="ProductName" />
                
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    SortExpression
="CategoryID" />
            
</Columns>
        
</asp:GridView>

 

 

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
string sName = e.CommandName;
            
int sArg = int.Parse(e.CommandArgument.ToString());
            Response.Write(sName 
+ " - " + sArg);
        }

        
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
int index = e.Row.RowIndex;
                Control ctrl 
= e.Row.Cells[2].FindControl("link");
                HyperLink link 
= ctrl as HyperLink;
                
if (link != null)
                {
                    
string sID = this.GridView1.DataKeys[index].Values[0].ToString();
                    
string sName = this.GridView1.DataKeys[index].Values[1].ToString();
                    link.NavigateUrl 
+= "?id=" + sID + "&name=" + sName;
                }
            }
        }

 

 

posted on 2008-08-11 13:09  王丹小筑  阅读(533)  评论(0)    收藏  举报

导航