Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All I Want to do is , when i click on link button pass two values to code behind.

ASPX CODE:

ASP.NET
<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>


VB
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.
Posted
Updated 21-Jun-12 19:14pm
v4

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.NET
<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

C#
 if(e.CommandName=="AddLine")
{
     int index = Int32.Parse(e.CommandArgument.ToString());
     GridViewRow row = grdViewJobDetail.Rows[index];
     string empName = row.Cells[0].Text.Trim();
}
 
Share this answer
 
Comments
Prosan 22-Jun-12 1:31am    
good answer my 5
anjumnavid 27-Jun-12 10:58am    
Thanks for reply . I am getting no value in any my variable.

Can you help me y ?

If e.CommandName = "AddLine" Then
Dim rowindex As Integer = Int32.Parse(e.CommandArgument.ToString())
Dim row As GridViewRow = GV_Location.Rows(rowindex)

Dim Location_Name As String = row.Cells(0).Text.Trim()
Dim Comp_City As String = row.Cells(0).Text.Trim()
Dim Comp_State As String = row.Cells(0).Text.Trim()
Dim Comp_Address_One As String = row.Cells(0).Text.Trim()
Dim Comp_Address_Two As String = row.Cells(0).Text.Trim()
Dim Comp_Zip As String = row.Cells(0).Text.Trim()

End If
virang_21 27-Jun-12 17:05pm    
You need to add columns to your gridview. And you are trying to access all the values from cells(0). It has to be like Cells(0),Cells(1),Cells(2) etc. Think of it as a table with number of rows and cells.

Dim Location_Name As String = row.Cells(0).Text.Trim()
Dim Comp_City As String = row.Cells(1).Text.Trim()
Dim Comp_State As String = row.Cells(2).Text.Trim()

Then your GridView needs to have those columns

<gridview ......="">
<columns>
<asp:boundfield datafield="Comp_City" headertext="Comp_City">
<asp:boundfield datafield="Comp_State" headertext="Comp_State">
<asp:boundfield datafield="Comp_Address_One" headertext="Comp_Address_One">
anjumnavid 9-Jul-12 11:33am    
Thanks every one helping me.

I have a new question. I have a Button called "Get all" , which is not part of GridView . I want get the index of all the row ,which check box is clicked.

Detail:

After my grid View load. User can click as many as check box or all the check box from the grid view. Then there is button out side the grid View called "Get All". So when user click on "Get ALL" button , i want to get all the check box clicked row index.
virang_21 9-Jul-12 16:50pm    
Iterate through your gridview row and see if your checkbox is checked or not to get Id.

foreach(GridViewRow row in grdViewId)
{
if( row.FindControl("myCheckboxId")!=null)
{
Checkbox chkBox = (Checkbox)row.FindControl("myCheckboxId");
if(chkBox.Checked)
{
//Process checked row
}

}
}

You need to have a template column in your griview that contains Checkbox with ID=myCheckboxId.
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.

VB
<asp:GridView
        id="GridView1"  DataKeyNames="Comp_Id,MS_User_Id"



C#
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
 
Share this answer
 
v3
Comments
anjumnavid 9-Jul-12 11:33am    
Thanks every one helping me.

I have a new question. I have a Button called "Get all" , which is not part of GridView . I want get the index of all the row ,which check box is clicked.

Detail:

After my grid View load. User can click as many as check box or all the check box from the grid view. Then there is button out side the grid View called "Get All". So when user click on "Get ALL" button , i want to get all the check box clicked row index.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900