How to fetch value of hidden filed from selected row in gridview

Posted by Sp under ASP.NET on 9/5/2010 | Points: 10 | Views : 13243 | Status : [Member] | Replies : 4
How to fetch value of hidden filed from selected row in gridview.
Scenario:
I have a grid view.. the grid view contains hidden field.
I want when a particular row of the gridview is selected then the value of the hidden field should be retained in viewstate and then when Update button is clicked i want to pass this value as a query string . How to do this. Will using viewstate be good in this case???




Responses

Posted by: Shankul2784 on: 9/6/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,

In the buttons command argument you can pass the value & on rowcommand event you can catch that button. I thing there no need of any viewstate from my side.

I hope, this will help you.

Thanks & Regards,

Shailesh

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Syedshakeer on: 9/6/2010 [Member] Starter | Points: 25

Up
0
Down
Hi,
use the follwoing code
<asp:GridView runat="Server" id="Gridivew1">

<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:TemplateField HeaderText ="ProductName">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ProductId") %>' />

<asp:Label ID ="lblProductName1" runat ="server" Text ='<%#Eval("ProductName")%>'>
</asp:Label>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ProductName">
<ItemTemplate>
<asp:Label ID ="lblQuantityPerUnit" runat ="server" Text ='<%#Eval("QuantityPerUnit")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</aps;Gridview>



--------------------
Select your Gridview Events.Double click on SelectedIndexChanged Event and write the Below code in .asp.cs page:
protected void Gridivew1_SelectedIndexChanged(object sender, EventArgs e)

{
string strValue = ((HiddenField)Gridivew1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value;
string strValue2 = ((Label)Gridivew1.SelectedRow.Cells[1].FindControl("lblProductName1")).Text ;

Response.Write("Product Id=" + strValue + "product=" + strValue2);

}


Syed Shakeer Hussain

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Macupryk on: 8/12/2012 [Member] Starter | Points: 25

Up
0
Down
Don't use the selectedindexchange.
rotected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "RowClick") // set EnablePostBackOnRowClick property of RadGrid ClientSettings as 'True' to fire this
{

HiddenField1.Value = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["OrderID"].ToString();
//save the selected row index in a HiddenField
Session["OrderID"] = HiddenField1.Value;

}

}

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/13/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this.
  

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="updt" CommandArgument='<%#Eval("UniqueID_Column_Name") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column Header1">
<ItemTemplate>
<%# Eval("Column_Name1") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column Header2">
<ItemTemplate>
<%# Eval("Column_Name2")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{
string Unique_ID = e.CommandArgument.ToString();
if (e.CommandName.ToString() == "updt")
{
// Call Update Query.
}
}


Sp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response