[Resolved] How to get value of gridview buttonfield (linkbutton)
I have a gridview and I am able to get cell values for bound fields like this:
Code:
If e.CommandName = "description" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow
mySelectedRow = GridView1.Rows(index)
Dim mySelectedCell As TableCell
mySelectedCell = mySelectedRow.Cells(3)
Dim jobtext As String
jobtext = mySelectedCell.Text
End If
Now I need to get the value for a Button Field (header column name is "description") and not sure how to do this. Here is the code I have:
Code:
Dim index = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
Dim myButtonField As LinkButton = mySelectedRow.FindControl("description")
Dim jobtext As String = myButtonField.Text.ToString
Get error (last line of code): Object reference not set to an instance of an object.
Can somebody help please. Thank you.
Re: How to get value of gridview buttonfield (linkbutton)
If it's a button field, then you don't know the actual ID of the button in the column. The header title is not the ID of the button. You'll need to look at it using, say, mySelectedRow.Controls(1) instead. Or (0). Or (2).
Re: How to get value of gridview buttonfield (linkbutton)
The column is defined as a Button Field of Type = Link
Made following changes:
myLinkButton = mySelectedRow.FindControl("description") - removed
myLinkButton = mySelectedRow.Controls(0) - inserted
Get error:
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.LinkButton'.
Re: How to get value of gridview buttonfield (linkbutton)
Step through your code and have a look at .Controls(1) and so on. Check to see if any of them actually contain the button or if one of them is the button itself. Note how .Controls(0) is of type DataControlFieldCell. When you do a quickwatch, you will be able to see the types of the various controls.
Re: How to get value of gridview buttonfield (linkbutton)
Stepped through every control (from 0 to 3). The button is defined in cell 3.
They all render:
"Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.LinkButton"
Code:
<asp:GridView ID="GridView1" runat="server" Style="z-index: 10; left: 6px;
position: relative; top: 43px; border-right: gray thin solid; border-top: gray thin solid; border-left: gray thin solid; border-bottom: gray thin solid; font-size: 12px;" Width="349px" AllowPaging="True" AllowSorting="True" Font-Size="Small" AutoGenerateColumns="False" CellSpacing="2"
HorizontalAlign="Left" BorderColor="LightGray" BorderStyle="None" PageSize="14" OnSorting="GridView1_Sorting" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField HeaderText="Detail" ShowSelectButton="True">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" Width="30px" />
</asp:CommandField>
<asp:BoundField DataField="CompanyItemID" HeaderText="Cost Code" SortExpression="CompanyItemID">
<ItemStyle HorizontalAlign="Left" Width="70px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Ttl_Qty" HeaderText="Total Quantity" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Right" Width="50px" />
</asp:BoundField>
<asp:ButtonField CommandName="description" DataTextField="description" HeaderText="description"
Text="description">
<ItemStyle Width="100px" Wrap="False" />
</asp:ButtonField>
Re: How to get value of gridview buttonfield (linkbutton)
Finally, got the code working:
Code:
If e.CommandName = "description" Then
Dim index = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
Dim myLinkButton As LinkButton
myLinkButton = mySelectedRow.Cells(3).Controls(0)
Dim jobtext As String = myLinkButton.Text
End If
Mendhak, thank you for your help.
Re: [Resolved] How to get value of gridview buttonfield (linkbutton)
Apologies, I forgot about the row. But you did get the idea, so that's good.