Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Gridview
Hi,
 
I am using gridview and in that I placed link button. Now on Link button click I want to get value of 1st column of gridview and assign to hidden field How can I do this?
 

Thanks
sjs
Posted 6-Oct-10 21:15pm
sjs4u1.2K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Please wirte inside the .aspx.cs page
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbtnSetHidden = (LinkButton)e.Row.FindControl("lbtnSetValue");
            lbtnSetHidden.Attributes.Add("onclick", "return setValueInHidden('" + e.Row.Cells[0].Text + "')");
        }
    }
write inside the .aspx page
<script language="javascript" type="text/javascript">
   //<![CDATA[
function setValueInHidden(cellValue) {
           document.getElementById('hdField').value = cellValue;
           return false;
       }
   //]]>
   </script>
 
Please do let me know, if you have any doubt.
 
Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.Rose | [Rose]
 
Thanks,
Imdadhusen
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<asp:GridView ID="grd" OnRowCommand="grd_RowCommand">
<Columns>
<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="btn" runat="server" Text="Click" CommandName="Click" CommandArgument='<%#Eval("FirstColumnFieldName") %>'/>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
Code Behind File contain below code:
 
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Click":
        {
            string FirstColumnValue = e.CommandArgument.ToString();
            hiddenfield.Value = FirstColumnValue;
            break;
        }
        default:
            break;
    }
}
 
Please Vote if this helped you then.
  Permalink  
v3
Comments
Dalek Dave at 7-Oct-10 3:34am
   
Good Answer
ajithk444 at 9-Aug-12 4:50am
   
how and where have u defined this 'hiddenfield'.?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

On link button click event write the following code
//instead of string variable you can place hidden variable id
//i is column index 
string colText = grid.Columns[i].HeaderText;
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
            AllowPaging="True" PageSize="2" >
 
      <Columns>
 
    
 
                <asp:TemplateField HeaderText="Name" HeaderStyle-Font-Size ="Medium" HeaderStyle-Width ="150px" HeaderStyle-Font-Names ="Microsoft Sans Serif" >
 
                    <ItemTemplate>
                     <div style ="width :150px;font-size :medium ;font-family :Microsoft Sans Serif  ">
                        <asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Label>
                  </div>
                    </ItemTemplate>
                   <EditItemTemplate>
                    <div style ="width :150px;font-size :medium ;font-family :Microsoft Sans Serif  ">
                   <asp:TextBox runat ="server" ID="txtname" Text ='<%#DataBinder.Eval(Container.DataItem,"Name") %>'></asp:TextBox>
                   </div>
                   </EditItemTemplate>
 
<HeaderStyle Font-Names="Microsoft Sans Serif" Font-Size="Medium" Width="170px"></HeaderStyle>
 
                </asp:TemplateField>
<asp:templatefield xmlns:asp="#unknown">
          
          <itemtemplate>
          <asp:linkbutton text="<%# Eval("url") %>" runat="server" onclick="lnk_click" id="lnk" commandargument="<%#Eval("url") %>"></asp:linkbutton>
          </itemtemplate>
          </asp:templatefield>
 
 
In Cod Behind
 
Protected Sub lnk_click(ByVal sender As Object, ByVal e As System.EventArgs)
 
         
 
        Dim id As String = DirectCast(sender, LinkButton).CommandArgument
 
        Response.Redirect(id)
 
    End Sub
  Permalink  
Comments
Sandeep Mewara at 7-May-12 2:47am
   
What is this? It does not do as asked.

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



Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 7 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100