网格视图:使用commanArgument传递多个值 [英] grid view : Pass more than one value with commanArgument
问题描述
我要做的就是,当我单击链接按钮时,将两个值传递给后面的代码.
ASPX代码:
< asp:GridView ID =" ="span> runat =" 服务器" OnPageIndexChanging EmpgridView_PageIndexChanging" DataKeyNames =" > ;
< 列 >
< asp:TemplateField HeaderText =" ItemStyle-HorizontalAlign HeaderStyle-Font-Underline =" >
< ItemTemplate >
< asp:LinkButton ID =" runat 服务器" 文本 <% #Eval(" )%>' CommandArgument =' <%#Eval( Comp_Id")+ ;" + Eval( MS_User_Id")% >' OnClick =" Emp_Name_Click" / >
< /ItemTemplate >
< /asp:TemplateField >
< /列 >
< /asp:GridView >
受保护的 子 Emp_Name_Click( ByVal 发件人 As 对象, ByVal e As EventArgs)
Dim b As LinkButton = DirectCast (发送方,LinkButton)
Dim 参数 As String = b.CommandArgument
Dim args As 字符串()=参数.Split(" c)
Dim Comp_id As string = args( 0 )
Dim Ms_User_Id As 字符串 = args( 1 )
结束 子
错误:从字符串;"转换键入"Double"无效. "CommandArgument =''<%#Eval(" Comp_Id)+";"+ Eval(" MS_User_Id)%>''""
我试图在CommandArgument上传递两个值,但是当我单击它时,会出现上述错误.
您可以将行索引作为命令参数传递并获取行后面代码中的rowcommand事件中的数字,并访问整个GridViewRow.查看您的代码,您需要在gridview中添加一些额外的列,例如name,comp_id,ms_user_id,如果您不希望其可见,则只需隐藏这些列即可.
< asp:templatefield 标题文字 =" 控件 样式 -宽度 =" 可见 =" xmlns:asp =" #unknown" < itemtemplate > < asp:button id =" runat 服务器" 文本 编辑行" 命令参数 =" <%#((GridViewRow)Container).RowIndex%> ;" 命令名 AddLine" / > < /itemtemplate > < 控件 样式 宽度 =" 100%" < 样式 > < /asp:templatefield >
在rowcommand事件中的代码背后
如果(例如,CommandName == <" ) { int index = Int32 .Parse(e.CommandArgument.ToString()); GridViewRow行= grdViewJobDetail.Rows [index]; 字符串 empName = row.Cells [ 0 ].Text.Trim(); }
看这个 ^ ]
您可以为此目的使用datakey,您必须获取rowindex才能获取datakey的值.而且您还使用了datakey.
< asp:GridView id = " DataKeyNames = " Comp_Id,MS_User_Id"
GridView1.DataKeys(RowIndex).Values(" ); GridView1.DataKeys(RowIndex).Values(" );
您可以按照solution1获取rowindex.我已经投票了.您可以使用此解决方案来获取rowindex,而不是使用行索引,可以像我描述的那样通过datakey获取值
All I Want to do is , when i click on link button pass two values to code behind.
ASPX CODE:
<asp:GridView ID="DataviewEmployee" runat="server" OnPageIndexChanging="EmpgridView_PageIndexChanging" DataKeyNames="Comp_Id, MS_User_Id,Name "> <Columns> <asp:TemplateField HeaderText="Employee Name" ItemStyle-HorizontalAlign="left" HeaderStyle-Font-Underline="true"> <ItemTemplate> <asp:LinkButton ID="Emp_Name" runat="server" Text='<%#Eval("Name")%>' CommandArgument='<%# Eval("Comp_Id") + ";" + Eval("MS_User_Id") %>' OnClick="Emp_Name_Click" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Protected Sub Emp_Name_Click(ByVal sender As Object, ByVal e As EventArgs) Dim b As LinkButton = DirectCast(sender, LinkButton) Dim arguments As String = b.CommandArgument Dim args As String() = arguments.Split(";"c) Dim Comp_id As string= args(0) Dim Ms_User_Id As String = args(1) End Sub
ERROR: Conversion from string ";" to type ''Double'' is not valid. "CommandArgument=''<%# Eval("Comp_Id") + ";" + Eval("MS_User_Id") %>''"
I am trying to pass two value on CommandArgument , but when i click on it give above error.解决方案You can pass row index as the command argument and get the row number in rowcommand event in codebehind and access the whole GridViewRow. Looking at your code you need to add few extra colums to your gridview like name,comp_id,ms_user_id and if you don''t want it to be visible just hide those columns.
<asp:templatefield headertext="Edit" controlstyle-width="100%" visible="false" xmlns:asp="#unknown"> <itemtemplate> <asp:button id="btnEdit" runat="server" text="Edit Line" commandargument="<%#((GridViewRow)Container).RowIndex%>" commandname="AddLine" /> </itemtemplate> <controlstyle width="100%"></controlstyle> </asp:templatefield>
In your codebehind in rowcommand event
if(e.CommandName=="AddLine") { int index = Int32.Parse(e.CommandArgument.ToString()); GridViewRow row = grdViewJobDetail.Rows[index]; string empName = row.Cells[0].Text.Trim(); }
look this CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^]
u can use datakey for this purpose for this you have to get rowindex to get value of datakey. and you have also used datakey.
<asp:GridView id="GridView1" DataKeyNames="Comp_Id,MS_User_Id"
GridView1.DataKeys(RowIndex).Values("Comp_Id"); GridView1.DataKeys(RowIndex).Values("MS_User_Id");
u can get rowindex as per solution1. and i have upvote it. u can use this solution to get rowindex than using row index u can get value by datakey as i describe
这篇关于网格视图:使用commanArgument传递多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!