首页  ·  知识 ·  云计算
LinkButton中使用CommandArgument传递参数
网友  博客园   综合  编辑:dezai   图片来源:网络
开发中经常需要将值存起来,当点击某一项时以便知道点击了哪一项。这里使用LinkButton的CommandArgument保存参数方式作个小结。/p&g

开发中经常需要将值存起来,当点击某一项时以便知道点击了哪一项。这里使用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;
}
本文作者:网友 来源:博客园
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读