LinkButton中使用CommandArgument传递参数

开发中经常需要将值存起来,当点击某一项时以便知道点击了哪一项。这里使用LinkButton的CommandArgument保存参数方式作个小结。

 

一、传递单一参数,注意下边代码中加粗部分:

代码
                         < asp:ListView  ID ="lvWorkFlowList"  runat ="server" >
                            
< LayoutTemplate >
                                
< asp:PlaceHolder  runat ="server"  ID ="itemPlaceholder"   />
                            
</ LayoutTemplate >
                            
< ItemTemplate >
                                
< div  class ="listViewStyle" >                                    
                                    
< <asp:LinkButton  ID ="lbnWorkFlowItem"  runat ="server"  OnClick ="lbnWorkFlowItem_Click"
                                        CommandArgument = '<%#((BPM.Foundation.WFDB.WF_Template_V)Container.DataItem).ID  %> '>
                                   < img  src ="../Resources/images/flow_icon.jpg"  alt ="" />  
                                   
<% #((BPM.Foundation.WFDB.WF_Template_V)Container.DataItem).templateName %>
                                    
</ asp:LinkButton >
                                
</ div >
                            
</ ItemTemplate >
                        
</ asp:ListView >


二、传递多个参数,使用json形式:

1、在页面上组合json,注意下边代码中加粗部分组合了带两个参数的json形式:

代码
                         < asp:ListView  ID ="lvWorkFlowList"  runat ="server" >
                            
< LayoutTemplate >
                                
< asp:PlaceHolder  runat ="server"  ID ="itemPlaceholder"   />
                            
</ LayoutTemplate >
                            
< ItemTemplate >
                                
< div  class ="listViewStyle" >
                                    
< asp:LinkButton  ID ="lbnWorkFlowItem"  runat ="server"  OnClick ="lbnWorkFlowItem_Click"
                                        CommandArgument = '<%#"{TID:\""  + ((BPM.Foundation.WFDB.WF_Template_V)Container.DataItem).templateID + "\",TVer:\""
         + ((BPM.Foundation.WFDB.WF_Template_V)Container.DataItem).templateEdition +"\"}"%>
'>  
                        
< img  src ="../Resources/images/flow_icon.jpg"  alt =""   />  
<% #((BPM.Foundation.WFDB.WF_Template_V)Container.DataItem).templateName %>
                                    
</ asp:LinkButton >
                                
</ div >
                            
</ ItemTemplate >
                        
</ asp:ListView >

2、在后组合json的形式。

代码
             // 通过循环给每个listview增加json形式的参数
             foreach  (ListViewItem lvItem  in  lvWorkFlowList.Items)
            {
                Control conLinkButton  =  lvItem.FindControl( " lbnWorkFlowItem " );
                
if  (conLinkButton  !=   null )
                {
                    LinkButton lbnItem  =  (LinkButton)conLinkButton;
                    
string  sID  =  lbnItem.CommandArgument;
                    var vTemplateItem  =
                        from wfp  in  iWorkflowPowerV
                        .Where(wfp  =>  wfp.ID.ToString()  ==  sID)
                        select  new  { wfp.templateID, wfp.templateEdition, wfp.templateName, wfp.businessType };
                    
string  sJson  =   " Dpt:\ " { 0 }\ " ,Dty:\ " { 1 }\ " ,tptID:\ " { 2 }\ " ,tptEdition:\ " { 3 }\ " ,tptName:\ " { 4 }\ " ,tptType:\ " { 5 }\ "" ;
                    
foreach  (var vObj  in  vTemplateItem)
                    {
                        sJson  =   string .Format(sJson, sDptId, sDtyId, vObj.templateID.ToString(), vObj.templateEdition.ToString(),
                            vObj.templateName.ToString(), vObj.businessType.ToString());
                        lbnItem.CommandArgument  =   " { "   +  sJson  +   " } " ;
                        
break ;
                    }
                }
            }

 

后台调用参数方法:

代码
using  Newtonsoft.Json;   // 调用json类

        
protected   void  lbnWorkFlowItem_Click( object  sender, EventArgs e)
        {
            
string  sArg  =  ((LinkButton)sender).CommandArgument.ToString();
            
// 得到json值
            Newtonsoft.Json.Linq.JObject jJsonObj  =  Newtonsoft.Json.Linq.JObject.Parse(sArg);
            
// 得到参数1
             string  sTemplateID  =  ( string )jJsonObj[ " TID " ];
            hfTemplateID.Value  =  sTemplateID;
            
// 得到参数2
             string  sTemplateEdition  =  ( string )jJsonObj[ " TVer " ];
            hfTemplateEdition.Value  =  sTemplateEdition;
        }

 


 

你可能感兴趣的:(command)