I have two GridView(SalesGView and ProdGView). SalesGView contains dropdowlist and textboxes.ProdGView is wrapped with ModalPopup and is populated by dropdownlist selection from ProdGView.

The issue is when i click button select on ProdGView, the dropdownlist in the SalesGView resets to default value (Electronics) instead of selected value.




aspx code:

<asp:GridView ID="SalesGView" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="609px" GridLines="None">

                <asp:TemplateField HeaderText="RowNumber">
                        <%# Container.DataItemIndex + 1 %>


                <asp:TemplateField HeaderText="Product">

                        <asp:DropDownList ID="CatCode"  OnSelectedIndexChanged ="CatCode_SelectedIndexChanged"   AutoPostBack="true" runat="server" />


                <asp:TemplateField HeaderText="Description">


                        <asp:TextBox ID="txtDesc" runat="server" Text='<%# Eval("Column2")%>' />



                <asp:TemplateField HeaderText="QUANTITY">


                        <asp:TextBox ID="txtQty" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>



                <asp:TemplateField HeaderText="Unit Price">


                        <asp:TextBox ID="txtPrice" runat="server" EnableViewState="false" Text='<%# Eval("Column4")%>' CssClass="round default-width-input" Width="80"></asp:TextBox>



                <asp:TemplateField HeaderText="Amount">


                        <asp:TextBox ID="txtAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>



                <asp:TemplateField HeaderText="Discount(%)">


                        <asp:TextBox ID="txtDiscPercent" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>



                <asp:TemplateField HeaderText="Discount Amount">


                        <asp:TextBox ID="txtDiscAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>


                    <FooterStyle HorizontalAlign="Right" />


                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                        <asp:Button ID="Save" runat="server" Text="Save" OnClick="btnSave_Click" />


                        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />


<asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>

<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Label8" PopupControlID="pnlpop"
    CancelControlID="btnCancel" BehaviorID="mpe" BackgroundCssClass="modalBackground">

<asp:Panel ID="pnlpop" runat="server" BackColor="White" Height="269px" Width="900px" Style="display: none">

    <asp:GridView ID="ProdGView" runat="server" AutoGenerateColumns="False">

            <asp:BoundField DataField="CatID" HeaderText="CATID" ItemStyle-Width="150" />
            <asp:BoundField DataField="PName" HeaderText="Product Name" ItemStyle-Width="100" />
            <asp:BoundField DataField="PDesc" HeaderText="Description" ItemStyle-Width="150" />
            <asp:BoundField DataField="USP" HeaderText="USP" ItemStyle-Width="150" />
            <asp:BoundField DataField="CatName" HeaderText="CAT NAME" ItemStyle-Width="150" />


            <asp:Button ID="btnSelect" Text="Select"  runat="server" OnClick="SelectRow"  /> 



    <asp:Button ID="btnCancel" runat="server" Text="Cancel" />


Code Behind

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Caching
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Linq

Public Class pos
    Inherits System.Web.UI.Page

    Dim CatCode As Object

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then

        End If

    End Sub

Private Sub SalesGView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles SalesGView.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim ctrl As Control = e.Row.FindControl("CatCode")
            If (Not (ctrl) Is Nothing) Then
                Dim dd As DropDownList = CType(ctrl, DropDownList)
                Dim connStr As String = ConfigurationManager.ConnectionStrings("SY_InventoryConnectionString").ConnectionString
                Dim sqlda As SqlDataAdapter = New SqlDataAdapter
                Dim com As SqlCommand = New SqlCommand
                Dim dt As DataTable

                Dim conn As SqlConnection = New SqlConnection(connStr)
                dt = New DataTable
                com.Connection = conn
                com.CommandText = "SELECT CatName FROM Prod_Category"
                sqlda = New SqlDataAdapter(com)

                dd.DataTextField = "CatName"
                dd.DataValueField = "CatName"
                dd.DataSource = dt

            End If
        End If

    End Sub

Private Sub BindProdGrid()
        Dim conString As String = ConfigurationManager.ConnectionStrings("SY_InventoryConnectionString").ConnectionString
        Dim rowIndex As Integer = 0
        Dim catname As DropDownList = CType(SalesGView.Rows(rowIndex).Cells(1).FindControl("CatCode"), DropDownList)

        Using con As New SqlConnection(conString)
            Using cmd As New SqlCommand("select Product.CatID,Product.PName,Product.PDesc, " _
                 & "  Product_Details.USP, Prod_Category.CatName " _
                 & "  from Product inner join Product_Details on Product.CatID= Product_Details.CatID " _
                 & "  inner join Prod_Category on Product_Details.CatID=Prod_Category.CatID where Prod_Category.CatName='" & (catname.SelectedItem.Text) & "' ")

                Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Dim dt As New DataTable()
                    Me.ProdGView.DataSource = dt
                End Using
            End Using
        End Using

    End Sub

Protected Sub SelectRow(sender As Object, e As EventArgs)

        Dim dt As New DataTable()
        If ViewState("DataTable") Is Nothing Then
            dt = New DataTable()
            dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Column2"), New DataColumn("Column4", GetType(String))})
            dt = DirectCast(ViewState("DataTable"), DataTable)
        End If
        Dim row As GridViewRow = TryCast(TryCast(sender, Button).NamingContainer, GridViewRow)

        Dim desc As String = row.Cells(2).Text
        Dim price As String = row.Cells(3).Text
        dt.Rows.Add(desc, price)
        Me.SalesGView.DataSource = dt
        ViewState("DataTable") = dt

    End Sub

 Protected Sub CatCode_SelectedIndexChanged(sender As Object, e As EventArgs)


    End Sub
    End Class

Any help will be appreciated.

use getter and setter to get the value from SalesGView and bring it back when you exit ProdGView


Public Property storedtype As String
            Dim o As Object = ViewState("storedtype")
            If Not o Is Nothing Then Return DirectCast(o, String)
            Return 0
        End Get
        Set(ByVal value As String)
            ViewState("storedtype") = value
        End Set
End Property
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.