Radio button list selected value not persistent in asp.net
<p>I have two dynamically populated radio button lists. The first one gets populated on a button click, the other one on the change event of the first radio button list. The problem is only with the second list. The issue is that I am not able to retrieve the changed value of the second radio button list in the InsertButton_Click method(marked by **). It always returns the default index value i.e 0. I don't have anything in page_load event. I read quite a few similar questions but none seem to help. Please guide. Below is the asp and c# code for the same:</p>

<p>ASP:</p>

<pre><code>&lt;asp:Button id="SaveButton" 
           Text="Save"
           runat="server" onclick="SaveButton_Click"&gt;
       &lt;/asp:Button&gt;  
       &amp;nbsp;&amp;nbsp;
       &lt;asp:Button id="VisualiseButton" 
           Text="Visualise"
           runat="server" onclick="VisualiseButton_Click"&gt;
       &lt;/asp:Button&gt;  

       &lt;!--&lt;hr /&gt;--&gt;
       &lt;asp:RadioButtonList id="RadioButtonList2" runat="server" Visible="false" 
            onselectedindexchanged="RadioButtonList2_SelectedIndexChanged" &gt;&lt;/asp:RadioButtonList&gt;

        &lt;asp:RadioButtonList id="RadioButtonList1" runat="server" Visible="false" &gt;&lt;/asp:RadioButtonList&gt;
         &lt;asp:Button id="SaveToDBButton" 
           Text="Insert"
           runat="server" Visible="false" onclick="InsertButton_Click"&gt;
</code></pre>

<p>C#:</p>

<pre><code> protected void SaveButton_Click(object sender, EventArgs e)
    {
        String selQuery = "SELECT id, name FROM categories";
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(selQuery, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            RadioButtonList2.DataSource = dt;
            RadioButtonList2.DataTextField = "name";
            RadioButtonList2.DataValueField = "id";
            RadioButtonList2.DataBind();
            RadioButtonList2.RepeatColumns = 3;
            RadioButtonList2.AutoPostBack = true;
            RadioButtonList2.Visible = true;
        }

        catch (SqlException ex)
        {

        }

        finally
        {

            if (con != null)
            {

                con.Close();

            }
        }

    }
    protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        String catId = RadioButtonList2.SelectedValue;
        SqlCommand cmdselect = new SqlCommand("SELECT DISTINCT categoryId, linkTablesCategories.tableName, tableDescription FROM linkTablesCategories, tableDescriptions where linkTablesCategories.tableName = tableDescriptions.tableName and categoryId ='" + catId + "'");
        RadioButtonList1.Items.Clear();
        try
        {
            con.Open();
            cmdselect.Connection = con;
            SqlDataReader dar = cmdselect.ExecuteReader();
            if (dar.HasRows)
            {
                while (dar.Read())
                {
                    ListItem li = new ListItem(dar["tableName"].ToString(), dar["categoryId"].ToString());
                    li.Attributes.Add("title", dar["tableDescription"].ToString());
                    RadioButtonList1.Items.Add(li);
                }
            }
            RadioButtonList1.Visible = true;
            SaveToDBButton.Visible = true;
        }
        catch (SqlException ex)
        {
            //lblMessage.Text = ex.Message;
        }

        finally
        {
            cmdselect.Dispose();
            if (con != null)
            {
                con.Close();
            }
        }
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
        String tableId="";
        **tableId = RadioButtonList1.SelectedItem.Text;**

        String path = Server.MapPath("~/");
        string filepath = path + Session["filepath"].ToString();
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(',');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }
        while (!sr.EndOfStream)
        {
            value = sr.ReadLine().Split(',');
            if (value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }
        SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
        bc.DestinationTableName = tableId;
        bc.BatchSize = dt.Rows.Count;
        con.Open();
        bc.WriteToServer(dt);
        bc.Close();
        con.Close(); 
    }
</code></pre>
