2

Apologies for the slightly convoluted title.

Basically, I have a project where I have a database connected to it. I'm displaying some of the contents of the database using a GridView, which is all up and running, but the way I'm designing the page I need to be able to click a button in each row that in essence exports the value of one of the cells in the row to a subroutine.

I've tried googling it quite a bit and trying related things I found here, but nothing I could find would function like I would like it to.

The relevant code is below.

Markup:

<asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" OnRowCommand="Staffgv_RowCommand" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" >

   <Columns>

         <asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart">
                <HeaderStyle Width="70px"   CssClass="hdrGvStart"/>
                <ItemTemplate>

                         <asp:Label ID="lblDateTimeStart" runat="server" Text='<%# Bind("DateTimeStart", "{0:t}") %>'></asp:Label>

                </ItemTemplate>      

          </asp:TemplateField>

          <asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd">
                 <HeaderStyle Width="70px"   CssClass="hdrGvFinish"/>
                 <ItemTemplate>

                         <asp:Label ID="lblDateTimeEnd" runat="server" Text='<%# Bind("DateTimeEnd", "{0:t}") %>'></asp:Label>

                 </ItemTemplate>

          </asp:TemplateField>

          <asp:TemplateField HeaderText="Forename" SortExpression="Forename">
                 <HeaderStyle Width="140px"  CssClass="hdrGvForename"/>
                 <ItemTemplate>

                         <asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>

                 </ItemTemplate>               

          </asp:TemplateField>  

          <asp:TemplateField HeaderText="Surname" SortExpression="Surname">
                  <HeaderStyle Width="140px"   CssClass="hdrGvSurname"/>
                  <ItemTemplate>

                         <asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>

                  </ItemTemplate>               

          </asp:TemplateField>  

          <asp:TemplateField>
                  <HeaderStyle CssClass="gvHeaderEdit" />
                  <ItemTemplate>

                         <asp:Button ID="Btnapptid" runat="server" Text=""  CssClass="btnGVEdit"  CommandName="FillStaffTables" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>

                  </ItemTemplate>
          </asp:TemplateField>

     </Columns>
 </asp:GridView>

And the relevant VB code:

Private Sub Staffgv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Staffgv.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim row = DirectCast(e.Row.DataItem, DataRowView)
            Dim apptid As Integer = Integer.Parse(row("AppointmentID").ToString)
            Dim Btnapptid = DirectCast(e.Row.FindControl("Btnapptid"), Button)
            'Btnapptid.Text = apptid
            Btnapptid.ToolTip = apptid
    End Select
End Sub

Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
    If (e.CommandName = "FillStaffTables") Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)

        lblTxtTester.Text = "AppointmentID"
        TextboxTester.Text = index
    End If
End Sub

If anyone would like more code like the code used to fill the GridView I'll post it, just didn't want to post too much irrelevant code.

Thanks

1 Answer 1

1

Solution A

When you only want 1 value from the gridview row when the button is clicked.

You could simply utilize the CommandArgument of your button (Btnapptid). This assumes that you don't need the gridview row index for when the button is clicked. If you do need this, then please see Solution B, otherwise continue here.

First, you'd need to modify your button's CommandArgument in the aspx page

ASPX

<asp:Button ID="Btnapptid" runat="server" Text=""  CssClass="btnGVEdit"
    CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>'/>

Then you should be able to grab the AppointmentID like this

VB (inside Staffgv_RowCommand())

If (e.CommandName = "FillStaffTables") Then
    txtAppointmentID.Text = e.CommandArgument
End If

Solution B

When you need more than 1 value from the gridview row when the button is clicked

Please note that this solution requires a couple changes on your end

  1. Create an additional control (in the gridview) which should hold the value that you want to get when the button is clicked.
  2. Fill said control with the value you want (via Bind() in the UI or in RowDataBound in the codebehind).

Next, in your RowCommand you'd be able to grab the newly created control with the help of the index variable (from your example) like so

Staffgv.Rows(index).FindControl("YOUR_CONTROLS_ID")

Example

Say that you decide to create a HiddenField control in which to store the value from your database

ASPX (hidden field should be somewhere inside the gridview - right under Btnapptid should be fine)

<asp:HiddenField ID="hfMyControl" runat="server" Visible="False" 
    Value='<%# Bind("SOME_DB_COLUMN") %>'/>

VB (inside Staffgv_RowCommand())

Dim hfMyControl As HiddenField = _
    DirectCast(Staffgv.Rows(index).FindControl("hfMyControl"), HiddenField)

Then simply use the value of hfMyControl

hfMyControl.Value

You could also repeat all this using multiple controls in order to potentially access multiple DB values stored in controls in the gridview.

Sign up to request clarification or add additional context in comments.

22 Comments

In reply to your first comment: in your case, yes, Solution A would be ideal. It would lead to less code too. For your second comment: I'm wondering if hfMyControl is not being filled with a value in the first place. Try setting a breakpoint right after the line Dim hfMyControl As HiddenField = DirectCast(Staffgv.Rows(index).FindControl("hfMyControl"), HiddenField). Check that 1) The variable hfMyControl is not Nothing and 2) hfMyControl.Value has something in it.
Is it correct to assume that you want AppointmentID when Btnapptid is clicked?
I've edited Solution A based on the assumption that you're looking for AppointmentID.
It's difficult to tell what's going on just based on this, but here are a few things to try: try adding this to your button UseSubmitBehavior="False" If that doesn't work, undo that change and try flipping your button into a LinkButton instead, see if that gets you anywhere. See related answers: here & here.
Make sure that if you do flip it to a LinkButton, that you fill-in the empty string in the Text="" part! I don't think anything will display if this text attribute remains empty like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.