lundi 13 juin 2016

Website posts back when gridview row is selected, but doesn't when a row is selected after gridview has been filtered

I have created a website in VS 2010. It has a gridview, which can be filtered on a particular part of the data with a dropdown list. The gridview is also clickable - when you click a row, it highlights it and also populates a textbox with the id number of the data in the selected row.

My problem is - when the page is fresh and gridview is unfiltered, every time the gridview is clicked, the page is posted back. But, as soon as you filter the gridview from the dropdown list, when clicked, the page doesn't post back. This is what I want - I don't know what is causing the page to post back every time the gridview clicked when it is unfiltered.

I can't seem to pin what's happening. I think its something to do with the dropdown list/data binding with the gridview. Can you see from the code what's going wrong?

This is the code behind:

Imports System.Data

Imports System.Drawing



Partial Class About

Inherits System.Web.UI.Page





Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    TextBoxBOMNIIN.Text = CType(Session.Item("xyz"), String)

End Sub



Protected Sub DropDownListBOMList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownListBOMList.SelectedIndexChanged



    Me.GridViewBOMList.SelectedRowStyle.BackColor = Color.White

    Me.GridViewBOMList.SelectedRowStyle.ForeColor = Color.Black



    Dim currentSelection As String = DropDownListBOMList.SelectedValue()



    ViewState("BOMNIIN") = " SELECT DISTINCT [tblBCPBOM].[Material],[tblBCPMaterial].[Description],[tblBCPMaterial].[Material Type],[tblBCPMaterial].[NLIC],[tblBCPMaterial].[MMC] FROM [tblBCPBOM] INNER JOIN [tblBCPMaterial] ON [tblBCPBOM].[Material] = [tblBCPMaterial].[Material] WHERE [tblBCPBOM].[BOM Usage] IN ('A','C','D','H','I','K') AND [tblBCPBOM].[Item] ='0000' AND [tblBCPMaterial].[NLIC] ='" & currentSelection & "' ORDER BY [tblBCPBOM].[Material];"



    SqlDataSourceBOMList.SelectCommand = ViewState("BOMNIIN")

    GridViewBOMList.DataBind()

    SqlDataSourceBOMList.DataBind()





End Sub



'Makes gridview clickable

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridViewBOMList, "Select$" & e.Row.RowIndex)

        e.Row.Attributes("style") = "cursor:pointer"

        e.Row.ToolTip = "Click to select this row"

    End If

End Sub



'Sets style of clicked rows and sets the NIIN to the text box

Protected Sub OnSelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridViewBOMList.SelectedIndexChanged



    For Each row As GridViewRow In GridViewBOMList.Rows

        If row.RowIndex = GridViewBOMList.SelectedIndex Then

            row.BackColor = Color.SteelBlue

            row.ForeColor = Color.Black

            row.ToolTip = String.Empty

        Else

            row.BackColor = Color.White

            row.ToolTip = "Click to select this row"

        End If

    Next



    ViewState("BNText") = GridViewBOMList.SelectedRow.Cells(0).Text()

    TextBoxBOMNIIN.Text = ViewState("BNText")

    Session("xyz") = ViewState("BNText")



End Sub





End Class

This is the HTML code:

<style type="text/css">

    #divBOMList

    {

        height: 445px;

    }

</style>

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">



    <div class="BOMNIINInput">



    <asp:Label ID="LabelBOMNIIN" runat="server" Text="BOM/NIIN" ></asp:Label>

    <asp:TextBox ID="TextBoxBOMNIIN" runat="server" text = ></asp:TextBox>



    </div>



<h2>

    About</h2>



<div>

    <asp:DropDownList ID="DropDownListBOMList" runat="server" 

        DataSourceID="SqlDataSourceBOMListDropDown" DataTextField="NLIC" 

        DataValueField="NLIC" AutoPostBack="true" AppendDataBoundItems="true">

        <asp:ListItem Text="" Value=""></asp:ListItem>

    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSourceBOMListDropDown" runat="server" 

        ConnectionString="<%$ ConnectionStrings:AtomsConnectionString %>" SelectCommand="SELECT DISTINCT 

  [tblBCPMaterial].[NLIC]





  FROM [tblBCPBOM] INNER JOIN [tblBCPMaterial] ON [tblBCPBOM].[Material] =     [tblBCPMaterial].[Material]

  WHERE [tblBCPBOM].[BOM Usage] IN ('A','C','D','H','I','K') AND [tblBCPBOM].[Item] ='0000'

  ORDER BY  [tblBCPMaterial].[NLIC];"></asp:SqlDataSource>

</div>



<div id="divBOMNIIN">

</div>

<p>

</p>

<div id="divBOMList" 

    style="height: 400px; overflow:scroll; width:587px; overflow-x:hidden">

    <asp:GridView ID="GridViewBOMList" runat="server" AutoGenerateColumns="False" 

        DataSourceID="SqlDataSourceBOMList" BorderStyle="None" GridLines="None" 

        Width="569px" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged">

        <Columns>

            <asp:BoundField DataField="Material" HeaderText="Material" 

                SortExpression="Material" >

            <ControlStyle BorderStyle="None" />

            </asp:BoundField>

            <asp:BoundField DataField="Description" HeaderText="Description" 

                SortExpression="Description" >

            <ControlStyle Width="20px" />

            </asp:BoundField>

            <asp:BoundField DataField="Material Type" HeaderText="Material Type" 

                SortExpression="Material Type" />

            <asp:BoundField DataField="NLIC" HeaderText="NLIC" SortExpression="NLIC" />

            <asp:BoundField DataField="MMC" HeaderText="MMC" SortExpression="MMC" />

        </Columns>

        <SelectedRowStyle BackColor="#CCCCCC" ForeColor="#CCCCCC" />

    </asp:GridView>

    <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>

    <asp:SqlDataSource ID="SqlDataSourceBOMList" runat="server" 

        ConnectionString="<%$ ConnectionStrings:AtomsConnectionString %>" SelectCommand="SELECT DISTINCT [tblBCPBOM].[Material]

  ,[tblBCPMaterial].[Description]

,[tblBCPMaterial].[Material Type]

  ,[tblBCPMaterial].[NLIC]



      ,[tblBCPMaterial].[MMC]



  FROM [tblBCPBOM] INNER JOIN [tblBCPMaterial] ON [tblBCPBOM].[Material] = [tblBCPMaterial].[Material]

  WHERE [tblBCPBOM].[BOM Usage] IN ('A','C','D','H','I','K') AND [tblBCPBOM].[Item] ='0000'

  ORDER BY [tblBCPBOM].[Material];"></asp:SqlDataSource>

</div>

<p>

    Put content here.

</p>

Aucun commentaire:

Enregistrer un commentaire