Hidden Field Value not finding value - c#

I have an hiddenfield field in my gridview but the code behind cant get its value maybe someone could find the problem.
HTML:
<asp:TemplateField HeaderText="TweetID" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="TweetID" runat="server" Value='<%#Eval("TweetID") %>' />
</ItemTemplate>
</asp:TemplateField>
.cs:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
HiddenField tid = GridView1.Rows[index].FindControl("TweetID") as HiddenField;
//Response.Write(tid.Value);
TweetHelper.RemoveTweet( Convert.ToInt32(tid.Value), 1);
}
by the way the response writes nothing.

Based on your code above what you are doing is overkill.
Either make TweetID a Gridview.DataKey.
Or if that's not an option, convert your Delete button to a template field and add TweetID as a CommandArgument to the Delete button.

Your code should work fine.However another way to find the control is
GridViewRow row = GridView1.Rows[e.RowIndex];
HiddenField hdn = (HiddenField)row.FindControl("TweetID");
string value = hdn.Value;
or simply
var tweetid = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("TweetID")).Value;

Related

Check if hyperlink in gridview has been clicked c#

Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}

How to get row data by clicking a image button in a row in an ASP.NET gridview

I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}

Using a Button click to execute ASP.net C# gridview

It seems like this would be simple, but for the life of me, I can't figure out how it would work.
I have a gridview.
I have a standard button.
How do I use the button click to display the gridview?
Any suggestions?
This is a brief example from the MSDN page: (https://msdn.microsoft.com/en-us/library/bb907626.aspx)
You might to add an asp:TemplateField inside your GridView and through the CommandArgument property in the Button, set the current row index.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
And in your code, in the RowCommand event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "AddToCart") {
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
Hope this help you.

getting GridView data from buttonField press?

So I have a GridView for a C# web application, that has a Buttonfield, and when that button is clicked, I need to get the value of one of the fields for that row and store it in a variable for processing in some way.
However, neither the GridView nor the ButtonField seem to possess any means of doing this.
Can anyone recommend a way of getting data from a GridView, or if this is not possible, a different type of view that does offer this functionality, while still displaying a whole table (eg, not a DetailsView)
You can Check this link: https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx.
Define the CommandName of the Button.
In the GridView Define the RowCommand Event and Check the CommandName.
Get the Index of the Row.
Get the Column with GridView.Rows[index](columnIndex)
If you are using asp:TemplateField like shown below then you can access the row content using RowCommand
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="AddCode" Text="Add New"/>
Code
protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "AddCode")
{
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var rows_lblCode = clickedRow.FindControl("lblCode") as Label;
// now you can acccess all the label properties. For example,
var temp = rows_lblCode.Text;
}
}

cell value in gridview is not getting in asp.net

im not getting the valu of specic row in row data bound event , value coming null;
<asp:TemplateField>
<HeaderTemplate>
Today's pos
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_TodayPos" runat="server" Text='<%# Eval("CurrentPosition") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
aspx.cs code
protected void GrdKeyWord_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = GrdKeyWord.Rows[rowindex].Cells[5].ToString();
}
The value you are looking for is stored in a label control not in a table cell. Therefore, you need to use FindControl on that row to access the lbl_TodayPos:
Label myLabel = (Label)GrdKeyWord.Rows[rowindex].FindControl("lbl_TodayPos");
string value = myLabel.Text;
If you autogenerate the columns in the gridview, or if you used 'BoundField' (instead of TemplateField) you could use .Cells[]. Because, in this case, you would have gridview rendered as pure html table with table cells.

Categories

Resources