DNN中gridView中数据链接地址设定的两个实例

实例一,用HyperLink控件,数据绑定时增加链接地址

UI代码: 

代码
< asp:GridView ID = " grdResult "  runat = " server "  CssClass = " DataGrid_Container "  AllowPaging = " False "     
   AutoGenerateColumns
= " False "  BorderWidth = " 0 "   OnRowDataBound = " grdResult_RowDataBound "    >    
   
< RowStyle CssClass = " DataGrid_Item "   />    
   
< SelectedRowStyle CssClass = " DataGrid_SelectedItem "   />            
   
< HeaderStyle CssClass = " DataGrid_Header "    />            
   
< AlternatingRowStyle CssClass = " DataGrid_AlvternatingItem "   />       
   
< Columns >                
       
< asp:TemplateField HeaderText = " 选择 "    >    
           
< ItemTemplate  >    
               
< input id = " SelectId "  type = " checkbox "  value = ' <%# Eval("DocId") %> '   />      
               
< asp:HiddenField id = " DocId "  value = ' <%# Eval("DocId") %> '  runat = " server "   />      
               
< asp:HiddenField id = " DocType "  value = ' <%# Eval("DocType") %> '  runat = " server "   />      
            
</ ItemTemplate >                     
       
</ asp:TemplateField >    
       
< asp:TemplateField  HeaderText = " 名称 "  HeaderStyle - HorizontalAlign = " Left "  ItemStyle - HorizontalAlign = " Left "   >    
           
< ItemTemplate   >    
               
< asp:Image ID = " imgFolder "  runat = " server "   />    
               
< asp:HyperLink ID = " DocName "  Text = ' <%# Eval("DocName") %> '  runat = " server " ></ asp:HyperLink >    
           
</ ItemTemplate >    
       
</ asp:TemplateField >                            
       
< asp:BoundField DataField = " FileSize "  HeaderText = " 文件大小(KB) "    />    
       
< asp:BoundField DataField = " CreateDate "  HeaderText = " 修改时间 "  DataFormatString = " {0:yyyy年MM月dd日 HH:mm} "   />      
   
</ Columns >                  
<
/ asp:GridView >   

 C#代码绑定方法一:

代码
protected   void  Page_Load( object  sender, System.EventArgs e)   
{   
    
if ( ! Page.IsPostBack)   
    {   
        BindGridData();             
    }   
}   
private   void  BindGridData()   
{   
    List
< DocumentInfo >  di = DocumentController.GetListDocument( this .UserId);   
    
if  (di.Count  ==   0 )   
    {   
        di.Add(
new  DocumentInfo());         
    }   
        grdResult.DataSource 
=  di;   
        grdResult.DataBind();   
      
}     
  
// 数据绑定时增加链接地址   
protected   void  grdResult_RowDataBound( object  sender, GridViewRowEventArgs e)   
{              
    
if  (e.Row.RowType  ==  DataControlRowType.DataRow )   
    {   
        HyperLink DocName 
=  (HyperLink)e.Row.FindControl( " DocName " );                   
        Image imgFolder
= (Image) e.Row.FindControl( " imgFolder " );   
        
// List<DocumentInfo> di =e.Row.DataItem as List<DocumentInfo>;   
        HiddenField txtDocId  =  (HiddenField) e.Row.FindControl( " DocId " );   
        HiddenField txtDocType 
=  (HiddenField)e.Row.FindControl( " DocType " );   
           
        
// 根据表中的列判断是否有数据   
         if  (txtDocId.Value  ==   " 0 " )   
        {   
            
int  cols  =  e.Row.Cells.Count;   
            TableCellCollection tc 
=  e.Row.Cells;   
            tc.Clear();   
            tc.Add(
new  TableCell());   
            tc[
0 ].ColumnSpan  =  cols;   
            tc[
0 ].Text  =   " 没有找到你要的资料。 " ;      
        }   
           
        
if  (txtDocType.Value.Trim().ToLower()  ==   " fold " )   
        {   
            imgFolder.ImageUrl 
=   this .ControlPath  +   " images/folder1.gif " ;   
            
string  navUrl  =   " javascript:navurl(' "   +   " http:// "   +  Request.Url.Host  +  ResolveUrl( " ../Document/callIndex.aspx?FolderId= " );   
            DocName.NavigateUrl 
=  navUrl  +  txtDocId.Value.Trim()  +   " ') " ;    
        }   
  
    }   
}  

 C#代码绑定方法二:

代码
// 绑定时设定链接地址   
protected   void  grdResult_RowDataBound( object  sender, GridViewRowEventArgs e)   
{              
    
if  (e.Row.RowType  ==  DataControlRowType.DataRow  &&  e.Row.RowIndex  >=   0 )   
    {                   
        DataRowView dr
= (DataRowView)e.Row.DataItem;   
        
int  intTabID =  Common.GetTabId(PortalId,  " ReceiveAccount " );                    
        HyperLink likSendMsg 
=  (HyperLink)e.Row.FindControl( " OptSendMsg " );   
          
        
if  (intTabID  >=   0 )   
        {   
            likSendMsg.NavigateUrl 
=  Globals.NavigateURL(intTabID,  "" " fId= "   +  dr[ " UserId " ].ToString());                                    
        }    
    }   
}  

 

实例二,用ImageButton控件,当用户点击button时,通过事件处理程序按照DataKey的值,

进行转向链接或进行数据处理

UI代码: 

代码
     < asp:GridView ID = " grdData "  runat = " server "  Visible = " False "  CssClass = " DataGrid_Container "  
        PagerSettings
- Visible = " false "   BorderWidth = " 0px "   DataKeyNames = " RowId "  
        AutoGenerateColumns
= " False "  AllowSorting = " True "  OnRowEditing = " grdData_RowEdit "  OnSorting = " grdData_Sorting "
        OnRowDeleting
= " grdData_RowDeleting "   >
        
< AlternatingRowStyle CssClass = " DataGrid_AlvternatingItem "   />
        
< RowStyle CssClass = " DataGrid_Item "   />
        
< HeaderStyle CssClass = " DataGrid_Header "   />        
        
< Columns >
            
< asp:TemplateField HeaderText = " 执行操作 " >
                
< ItemTemplate  >                    
                    
< asp:ImageButton ID = " BtnUpdate "  runat = " server "   ToolTip = " 编辑 "  CommandName = " Edit "  ImageUrl = " ~/images/edit.gif "   />                         
                    
< asp:ImageButton ID = " BtnDelete "  runat = " server "  CausesValidation = " False "  CommandName = " Delete "
                            ToolTip
= " 删除 "  OnClientClick = " return confirm('确认要删除此记录吗?') "  ImageUrl = " ~/images/delete.gif " />  
                
</ ItemTemplate >
            
</ asp:TemplateField >             
        
</ Columns >
    
</ asp:GridView >

 

C#代码:

代码
         protected   void  grdData_RowEdit( object  sender, GridViewEditEventArgs e)
        {           
            
string  RowId  =  grdData.DataKeys[e.NewEditIndex].Value.ToString();           
            Response.Redirect(EditUrl(
" RowId " ,RowId,  " EditForm " ));
        }

        
protected   void  grdData_RowDeleting( object  sender, GridViewDeleteEventArgs e)
        {
            
if  (ListController.DeleteRow(TableName, grdData.DataKeys[e.RowIndex].Value.ToString()))
            {
                Common.ShowSuccessFacebox(
" 删除成功! " , Page,  0 );
            }
            
else
            {
                Common.ShowFailureFacebox(
" 删除失败! " , Page);
                
return ;
            }              
            grdData.EditIndex 
=   - 1 ;            
            BindData();
        }

 

 

 

 

 

你可能感兴趣的:(GridView)