Link to home
Start Free TrialLog in
Avatar of JessyRobinson1234
JessyRobinson1234

asked on

Gridview needs to call different subs at rowlevel

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="" 
                    SortDescendingImageUrl="">
                    <Columns>
                         <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" />
                    </Columns>

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
        objConn.Open()
        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
            rdr.Read()
            Response.Clear()
            Response.ContentType = "application/vnd.ms-excel"
            Response.BinaryWrite(CType(rdr("File"), Byte()))
            Response.End()
        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 & "")
        Else
            Response.Redirect(String.Format("~/Foxconn/CT/Metrics_Detail.aspx?Key={0}", ProductID))
        End If

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jtdebeer
jtdebeer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Jessy,

Basically Gridview's event sequence is like that only. If you put any command oriented control i.e. button, link or inbuilt command parameter in the gird then it would "always" go through row_command event handler and then to appropriate event handler method(Multicast delegate).

The workaround could be:
Checking at the start of Row_Command method as if the command is generated for "Select" then exit the sub. And then page control will reach to the "Selected_Index_Changed" automatically.

 (Check the comment with 'Aksh)
Protected Sub CompleteGridview1_RowCommand(ByVal sender .....
       'Aksh :Checking the command name and determine the behaviour
       'Aksh : Code Starts
       if e.CommandName.ToUpper() = "SELECT"
            Exit Sub;
       'Aksh: Code ends
        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 & "")
        Else
            Response.Redirect(String.Format("~/Foxconn/CT/Metrics_Detail.aspx?Key={0}", ProductID))
        End If
    End Sub

Happy coding...
Aksh