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">
<Columns>
<asp:TemplateField HeaderText="RowNumber">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="CatCode" OnSelectedIndexChanged ="CatCode_SelectedIndexChanged" AutoPostBack="true" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDesc" runat="server" Text='<%# Eval("Column2")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QUANTITY">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" EnableViewState="false" Text='<%# Eval("Column4")%>' CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount(%)">
<ItemTemplate>
<asp:TextBox ID="txtDiscPercent" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount Amount">
<ItemTemplate>
<asp:TextBox ID="txtDiscAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
<asp:Button ID="Save" runat="server" Text="Save" OnClick="btnSave_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<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:ModalPopupExtender>
<asp:Panel ID="pnlpop" runat="server" BackColor="White" Height="269px" Width="900px" Style="display: none">
<asp:GridView ID="ProdGView" runat="server" AutoGenerateColumns="False">
<Columns>
<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:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" Text="Select" runat="server" OnClick="SelectRow" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</asp:Panel>
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
SetInitialRow()
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)
sqlda.Fill(dt)
dd.DataTextField = "CatName"
dd.DataValueField = "CatName"
dd.DataSource = dt
dd.DataBind()
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()
sda.Fill(dt)
Me.ProdGView.DataSource = dt
Me.ProdGView.DataBind()
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))})
Else
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
Me.SalesGView.DataBind()
ViewState("DataTable") = dt
End Sub
Protected Sub CatCode_SelectedIndexChanged(sender As Object, e As EventArgs)
Me.ModalPopupExtender1.Show()
BindProdGrid()
End Sub
End Class
Any help will be appreciated.