Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

ASP:
ASP.NET
<asp:Button id="SaveButton"
           Text="Save"
           runat="server" onclick="SaveButton_Click">
       </asp:Button>
         
       <asp:Button id="VisualiseButton"
           Text="Visualise"
           runat="server" onclick="VisualiseButton_Click">
       </asp:Button>
       <asp:RadioButtonList id="RadioButtonList2" runat="server" Visible="false"
            onselectedindexchanged="RadioButtonList2_SelectedIndexChanged" ></asp:RadioButtonList>

        <asp:RadioButtonList id="RadioButtonList1" runat="server" Visible="false" ></asp:RadioButtonList>
         <asp:Button id="SaveToDBButton"
           Text="Insert"
           runat="server" Visible="false" onclick="InsertButton_Click">



C#:
C#
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(); 
            }
Posted
Updated 4-Feb-13 8:38am
v2
Comments
Richard C Bishop 4-Feb-13 14:03pm    
You never specified your problem. Use the "Improve Question" widget to explain what exactly your problem is.
Member 9810509 4-Feb-13 14:39pm    
Sorry, my bad. Have updated it now. Thanks for pointing it out.
Richard C Bishop 4-Feb-13 14:49pm    
No worries.
Richard C Bishop 4-Feb-13 14:53pm    
If you are posting back multiple times, you may be losing the value you are expecting in the process. You might try putting the "RadioButtonList1.SelectedItem.Text;" in session right when it is assigned and then pull it out in the InsertButton_Click event.
Member 9810509 4-Feb-13 16:21pm    
Assigned as in, I am not assigning it, I just need to track its value as I need it in the InserButton_Click. I have tried a lot of things, none seem to work. Also, the first radio list, which is also dynamic, does not cause any issues. I tried using the index_change event to store the value in session, but there also I get the same default value and not the newly selected value.

1 solution

Hey just got the issue, it was with the line:
C#
ListItem li = new ListItem(dar["tableName"].ToString(), dar["categoryId"].ToString());

The categoryId was a constant value and thus the issue, again my bad.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900