Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Hi,

Please wirte inside the .aspx.cs page
C#
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
XML
<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:

Thanks,
Imdadhusen
 
Share this answer
 
XML
<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:

C#
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.
 
Share this answer
 
v3
Comments
Dalek Dave 7-Oct-10 3:34am    
Good Answer
ajithk444 9-Aug-12 4:50am    
how and where have u defined this 'hiddenfield'.?
On link button click event write the following code
C#
//instead of string variable you can place hidden variable id
//i is column index 
string colText = grid.Columns[i].HeaderText;
 
Share this answer
 
v3
XML
<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

VB
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
 
Share this answer
 
Comments
Sandeep Mewara 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)



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