I have a datatable that I'm trying to use for storing a matchup between two columns of dynamic controls (a span holding data on the left is matched up to choices from dropdownlists on the right).

I want the datatable to save and store the data between postbacks but it seems to be resetting/not-saving.

The code I'm providing is a full working example (aspx and vb). On the page try picking something from one dropdownlist and see that it saves; then choose something from another dropdownlist and see that your new choice saves BUT your old choice for the other dropdownlist does not stay saved. Also try picking something in a dropdownlist (notice that it saves to the datatable), then click the "div 2" button and notice that the data resets. (I'm talking about what data that is shown in the gridview and not by the controls themselves... actually this bring up another problem: I'm trying to have the dynamic controls display what is saved in datatable, but they don't seem to be.)

Essentially what I'm trying to accomplish is the following:

If the datatable is empty (a.k.a. the page is loading for the first time) build the dynamic controls and try to automatch values.
If the datatable is filled with data, then build the dynamic controls and select values for the dropdownlist based off of what is saved in the datatable.
When a user chooses something in one of the dropdownlists AND that value hasn't already been picked in another dropdownlist save that value to the datatable
If the user chooses something that already has been picked, then display an error message and don't save the value to the datatable.

I've attached a working code sample for illustration purposes.

Someone from another forum was suspicious that it was the dynamic controls causing the problem.

But I've worked a lot with dynamic controls and everything with how I built them seemed to be working fine.

He gave a good suggestion though and so I've rebuilt the page with static controls to do some testing. Sure enough, even with static controls I'm still experiencing a problem with the datatable not saving.

Anyone think it might be that it's because my datatable is not attached to a datasource, like xml, access, sql server?

I think I'm going to try doing a write to xml and then bind to that xml file temporarily. I really didn't want to waste disk space and hdd read/write time for such a small transaction though. I would really prefer for it to stay in memory for the small time that it's needed.

Here's the aspx:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Testing_Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="Div1_div" runat="server" visible="false">
            <asp:Button ID="Div1_btn" runat="server" Text="Div 2" /><br />
            <asp:GridView ID="Page1_gv" runat="server">
                <AlternatingRowStyle BackColor="#F5F5F5" />
                <SelectedRowStyle BackColor="#FFFF00" />
            </asp:GridView>
            DDL Control Index:<asp:TextBox ID="DDLControlIndex_txtbx" runat="server"></asp:TextBox>
            DDL Item Index:<asp:TextBox ID="DDLItemIndex_txtbx" runat="server"></asp:TextBox>
            DDL Item Value:<asp:TextBox ID="DDLItemValue_txtbx" runat="server"></asp:TextBox><br />
            <asp:DropDownList ID="DDL0_ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DBHeader_ddl_SelectedIndexChanged">
                <asp:ListItem Text="" Value=""></asp:ListItem>
                <asp:ListItem Text="One" Value="One"></asp:ListItem>
                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                <asp:ListItem Text="Three" Value="Three"></asp:ListItem>
                <asp:ListItem Text="4" Value="4"></asp:ListItem>
                <asp:ListItem Text="Five" Value="Five"></asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="DDL1_ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DBHeader_ddl_SelectedIndexChanged">
                <asp:ListItem Text="" Value=""></asp:ListItem>
                <asp:ListItem Text="One" Value="One"></asp:ListItem>
                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                <asp:ListItem Text="Three" Value="Three"></asp:ListItem>
                <asp:ListItem Text="4" Value="4"></asp:ListItem>
                <asp:ListItem Text="Five" Value="Five"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <div id="Div2_div" runat="server" visible="false">
            <asp:Button ID="Div2_btn" runat="server" Text="Div 1" /><br />
            <asp:GridView ID="Div2_gv" runat="server">
                <AlternatingRowStyle BackColor="#F5F5F5" />
                <SelectedRowStyle BackColor="#FFFF00" />
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>

And here's the VB.Net:

Imports System.Data
Imports System.Data.SqlClient
Partial Class Testing_Default2
    Inherits System.Web.UI.Page
    Protected Numbers_dt As DataTable = New DataTable("Numbers")
    Protected Number_nr As DataRow
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.PreInit
        With Numbers_dt
            .Columns.Add("i_DDLItemIndex", System.Type.GetType("System.Int32"))
            .Columns.Add("u_DDLItemValue", System.Type.GetType("System.String"))
            Number_nr = .NewRow()
            Number_nr("i_DDLItemIndex") = 0
            Number_nr("u_DDLItemValue") = ""
            .Rows.Add(Number_nr)
            Number_nr = .NewRow()
            Number_nr("i_DDLItemIndex") = 0
            Number_nr("u_DDLItemValue") = ""
            .Rows.Add(Number_nr)
        End With
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        If Not (Page.IsPostBack) Then
            Div1_div.Visible = True
        End If
    End Sub
#Region " Div 1 "
    Protected Sub Div1_div_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Div1_div.Init
        Page1_gv.DataSource = Numbers_dt
        Page1_gv.DataBind()
    End Sub
    Protected Sub DBHeader_ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        Dim selectedIndex, rowNumber As Integer
        Dim selectedValue, selectedText As String
        With ddl
            selectedIndex = .SelectedIndex
            selectedValue = .SelectedValue
            selectedText = .SelectedItem.Text
            rowNumber = CInt(.ClientID.ToString().Replace("DDL", "").Replace("_ddl", ""))
        End With
        Numbers_dt.Rows(rowNumber).BeginEdit()
        Numbers_dt.Rows(rowNumber).Item("i_DDLItemIndex") = selectedIndex
        Numbers_dt.Rows(rowNumber).Item("u_DDLItemValue") = selectedValue
        Numbers_dt.Rows(rowNumber).EndEdit()
        Numbers_dt.Rows(rowNumber).AcceptChanges()
        DDLControlIndex_txtbx.Text = rowNumber.ToString()
        DDLItemIndex_txtbx.Text = Numbers_dt.Rows(rowNumber).Item("i_DDLItemIndex")
        DDLItemValue_txtbx.Text = Numbers_dt.Rows(rowNumber).Item("u_DDLItemValue")
        Page1_gv.DataBind()
    End Sub
    Protected Sub Page1_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Div1_btn.Click
        Div1_div.Visible = False
        Div2_div.Visible = True
    End Sub
#End Region
#Region " Div 2 "
    Protected Sub Page2_div_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Div2_div.Load
        Div2_gv.DataSource = Numbers_dt
        Div2_gv.DataBind()
    End Sub
    Protected Sub Div2_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Div2_btn.Click
        Div1_div.Visible = True
        Div2_div.Visible = False
    End Sub
#End Region
End Class

set the Autopostback property of the Dropdownlist to "false". In your code it is set to "true" which forces the page to be posted back to the server.

Set the Autopostback to false for all Dropdownlis boxes

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.