Get value from GridView when a button is clicked - c#

I have a GridView , and the GridView has an item template. Now I need to get the value of the first cell which contains the value I need but I have tried the following and does not work, it eturns a ""
int id = Convert.ToInt32(GridView.Rows[index].Cells[0].Text);
Here is the code I have in the gridview
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblDepartmentID" Text='<%#DataBinder.Eval(Container.DataItem,"DepartmentID")%>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
I have to emphazise that I do not want to use the GridView_RowCommand or other GridView events.. I need to pull this value upon clicking a button on the same page.
How can I do this?

If you are trying to access the text value of Label then you have to first get the reference to Label control like this:
Label lbl = (Label)GridView.Rows[index].FindControl("lblDepartmentID");
and then use it's Text property to get the value required:
int id = Convert.ToInt32(lbl.Text);

Related

asp.net c# gridview label value disappear when sorting

I have a gridview that contain label, the value of this label is assigned in code behind,
I enabled sorting in my gridview and when I click the header to sort the value of the label disappear but the other DataFields doesnt.
<asp:TemplateField HeaderText="<%$Resources:mj.resource, req_category%>" SortExpression="category_id">
<ItemTemplate>
<asp:Label ID="category_id" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Have you set EnableViewState="false" on the GridView or at the page level?
Are you using DataSourceControl (like SqlDataSource) to bind data to the GridView?
If so, you'd have to manually sort and bind data again in the Sorting event.

Hyperlink in datagrid

I am inserting record in datagrid by binding it with data table and I had set autogenerate field ON for datagrid. Now all I want is first column which will comes in datagrid should be hyperlink field and when I will click on link it should redirect to another page. Please help
do like :
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
Edit:
You need to change the column type to a Hyperlink column.
and where to set is your job Sandesh
<Columns>
<asp:hyperlinkfield headertext="NewsHeadline"
datatextfield="NewsHeadline"
datanavigateurlfield="NewsURL"
datanavigateurlformatstring="http://{0}" />
</Columns>

Disable a TextBox in GridView and retain its value c#

I have a TextBox in a GridView which takes a user input. The problem is that it loses its value when I click on a Button placed in the GridView footer which adds an empty row to the GridView,How can I fix it?
Also, this happens only when I set its Disabled attribute to false in RowDataBound event. But if I set the ReadOnly attribute, everything works fine.
This is the TextBox in GridView.Bound to a DB data:
<asp:TemplateField HeaderText="Shift Code">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="txtingrid" ID="lblShiftCode" Text='<%# Eval("SHIFTCODE") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
this is what I do in the RowDataBound:
if (e.Row.RowType == DataControlRowType.DataRow)
{
lblShiftCode.Attributes.Add("disabled", "true");
}
Please help.:)
lblShiftCode.Attributes.Add("disabled", "disabled");
If you are doing a post back on the button click, you will have to set the UseSubmitBehavior attribute to "false" on the button to retain values of the disabled controls.
More info here
Its very simple just rebind the gridview after clicking the button. Suppose you are using gridbind(); method to bind your gridview.So you have to call this method again after button click.

.NET framework 4.5 loads default edit template on GridView?

I've created a gridview with edit feature. On clicking the gridview row edit image, it makes the custom edit panel visible and the user can edit and save the gridview row data or s/he can click the cancel button without any updates. It works perfectly fine with .NET framwork 2.0.
But, the problem lies with the .NET framework 4.5 -
when the user clicks the cancel or save button, instead of loading the
normal gridview, it loads the default edit template on the top of
gridview row where the user had clicked before.
So, how can I solve this problem? This happens in all the Gridviews used in our ASP.NET website.
Here's the original Gridview Row: Original GridRow
Here's the custom edit panel with save/cancel: Custom Edit Panel
Here's error: Default Template loaded
I usually achieve what you are trying to do in the row databound event.
Try setting up your grid view like this
<asp:Gridview id="ClientManagementGridView runat="server"/>
<columns>
<asp:TemplateField>
<itemtemplate>
//put edit button here
</itemtemplate>
<edititemtemplate>
// put your update and cancel buttons here
</edititemtemplate>
</asp:Templatefield>
<asp:templatefield>
<itemtemplate>
<aspTextBox Text='<#Eval("ClientId") %>' runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:Panel id="EditPanel">
//your panel contents
</asp:Panel>
</edititemtemplate>
</asp:templateField>
<asp:BoundField DataField="ClientName" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Name" />
</columns>
<asp:GridView>
Now in your row data bound event of your gridview put this magic piece of code
If e.Row.RowState.ToString().Contains("Edit") Then
Dim editGrid As GridView = TryCast(sender, GridView)
Dim colSpan As Integer = editGrid.Columns.Count
e.Row.Cells(0).Visible = True
e.Row.Cells(1).Visible = True
For i As Integer = 2 To colSpan - 1
e.Row.Cells(i).Visible = False
e.Row.Cells(i).Controls.Clear()
Next
e.Row.Cells(1).Attributes("ColSpan") = (colSpan).ToString()
End if
As you can see I am overriding the default edit item template by clearing its default controls and then changing the column span of the cell to fit my custom edit panel.As well as hiding cells I want to be hidden.

Binding a Button to a GridView

This a pretty simple question, I'm just not sure how to do it exactly. I would like to bind a Button or perhaps ImageButton to a GridView in ASP.NET/C#. Currently, the GridView has two columns and is bound to a DataTable with two columns. I want to add a third column to the GridView, which will include the Button.
I know GridView has ButtonField, but I'm not too sure how to go about using it to do what I want. I want to dynamically generate these Buttons and add them to the GridView.
Here is how my GridView looks right now:
<asp:GridView
ID="GridView1"
Runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="Display Name"
DataNavigateUrlFields="DISPNAME"
DataNavigateUrlFormatString="ViewItem.aspx"
DataTextField="DISPNAME">
<ItemStyle Width="70%" />
</asp:HyperLinkField>
<asp:BoundField
DataField="TypeDisp"
HeaderText="Type">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
You can use a template field like the following,
<TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="image url" CommandName="SomeCommand" CommandArgument='<%# Eval("Id") %>'/>
</ItemTemplate>
</TemplateField>
Then you can handle the RowCommand event of the GridView and check the e.CommandName to see what command to be executed and you can get the e.CommandArgument as well which could be the row Id like I used in the code above.
If we are talking a button that's always present, you can use ButtonField, or even use a TemplateField and provide the template with the button, and bind the data to the button (sounds like you may want to bind data to the attributes of the button?)
If you are looking to dynamically generate buttons in the UI, tap into the RowCreated event and add the button the GridView. You'd have to do this on every page load; the GridView won't remember a button created programmatically.
HTH.

Categories