We help IT Professionals succeed at work.
Get Started

Gridview needs to call different subs at rowlevel

Last Modified: 2013-11-07
I have a gridview. The rows consist of a buttonfield and a hyperlinkfield.
When clicking on the Buttonfield <asp:ButtonField ButtonType="link" CommandName="CT_Key" Text="Edit" />  it needs to call Protected Sub CompleteGridview1_RowCommand

When clicking on the hyperlinkfield <asp:CommandField ShowSelectButton="True" SelectText="View Manifest"
                            SortExpression="File" HeaderText="Manifest" /> it needs to call Protected Sub CompleteGridview1_selectedindexchanged.

As of now both fields go straigh to Protected Sub CompleteGridview1_RowCommand. How can I avoid this?

<asp:CompleteGridView ID="CompleteGridView1" DataKeyNames="CT_Key" 
                    runat="server" CellPadding="4" DataSourceID="sdsMetrics"
                    ForeColor="#333333" GridLines="None" AllowSorting="True" AutoGenerateColumns="False"
                    ShowFilter="True" ShowFooter="True" OnLoad="MyOnLoadHandler" Width="950px" 
                    ShowUnselect="True" CssClass="form" AllowPaging="True" 
                    OnRowDataBound="DataGrid1_RowDataBound" SortAscendingImageUrl="" 
                         <asp:ButtonField ButtonType="link" CommandName="CT_Key" Text="Edit" /> 
                        <asp:BoundField DataField="CT_Key" HeaderText="CT_Key" InsertVisible="False" ReadOnly="True"
                            SortExpression="CT_Key" Visible="False" />
                        <asp:BoundField DataField="CT_Date" HeaderText="CT Date" SortExpression="CT_Date" />
                        <asp:BoundField DataField="Ship_Origin" HeaderText="Ship Origin" SortExpression="Ship_Origin" />
                        <asp:BoundField DataField="Trailer_nbr" HeaderText="Trailer Nbr" SortExpression="Trailer_nbr" />
                        <asp:BoundField DataField="Pull_request" HeaderText="Pull Request" SortExpression="Pull_request" />
                        <asp:BoundField DataField="Pull_Req_Time" HeaderText="Pull Req Time" SortExpression="Pull_Req_Time" />
                        <asp:BoundField DataField="Pull_Req_Time_Two" HeaderText="Pull Req Time" SortExpression="Pull_Req_Time_Two" />
                        <asp:BoundField DataField="Pull_Req_Time_Three" HeaderText="Pull Req Time" SortExpression="Pull_Req_Time_Three" />
                        <asp:BoundField DataField="Pull_Req_Time_Four" HeaderText="Pull Req Time" SortExpression="Pull_Req_Time_Four" />                        
                        <asp:BoundField DataField="Fill_Rate" HeaderText="Fill Rate" SortExpression="Fill_Rate" />
                        <asp:BoundField DataField="Driver_Depart_Time" HeaderText="Driver Depart Time" SortExpression="Driver_Depart_Time" />
                        <asp:BoundField DataField="Customs_Time" HeaderText="Customs Time" SortExpression="Customs_Time" />
                        <asp:BoundField DataField="Border_Depart" HeaderText="Border Depart" SortExpression="Border_Depart" />
                        <asp:BoundField DataField="Trailer_Arriv_Time" HeaderText="Trailer Arriv" SortExpression="Trailer_Arriv_Time" />
                        <asp:BoundField DataField="Cycle_Time" HeaderText="Cycle Time" SortExpression="Cycle_Time" />
                        <asp:CommandField ShowSelectButton="True" SelectText="View Manifest" 
                            SortExpression="File" HeaderText="Manifest" />

Open in new window

Protected Sub CompleteGridview1_selectedindexchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CompleteGridView1.SelectedIndexChanged
        Dim conString As String = ConfigurationManager.ConnectionStrings("BusDevConnectionString").ToString
        Dim objConn As SqlConnection = New SqlConnection(conString)
        Dim rdr As SqlDataReader
        Dim sqlstr As String

        Dim myCommandDV As SqlDataAdapter = New SqlDataAdapter
        Dim strID As String
        strID = CompleteGridView1.SelectedValue

        sqlstr = "select [File] from Cycle_Time where  CT_Key='" & strID & "'"

        Dim cmd = New SqlCommand(sqlstr, objConn)

        Dim result As Byte() = Nothing

        rdr = cmd.ExecuteReader()
        If rdr.HasRows Then
            Response.ContentType = "application/vnd.ms-excel"
            Response.BinaryWrite(CType(rdr("File"), Byte()))
        End If


    End Sub

    Protected Sub CompleteGridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles CompleteGridView1.RowCommand
        Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)

        Dim ProductID As String = CompleteGridView1.DataKeys(currentRowIndex).Value
        If Roles.IsUserInRole(Membership.GetUser.UserName.ToString, "ELP_Transport") Then
            Response.Redirect("~/Foxconn/CT_Transport/Metrics_Detail_Transport.aspx?Key=" & ProductID & "")
            Response.Redirect(String.Format("~/Foxconn/CT/Metrics_Detail.aspx?Key={0}", ProductID))
        End If

    End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE