单选按钮列表选择的值在asp.net中不是持久的

时间:2013-01-16 19:03:00

标签: c#

我有两个动态填充的单选按钮列表。第一个按钮单击时填充第一个,第一个单选按钮列表的更改事件中的另一个填充。问题只出在第二个列表中。问题是我无法在InsertButton_Click方法中检索第二个单选按钮列表的更改值(标记为**)。它总是返回默认索引值,即0.我在page_load事件中没有任何内容。我读了很多类似的问题,但似乎都没有帮助。请指导。下面是相同的asp和c#代码:

ASP:

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

       <!--<hr />-->
       <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#:

 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(); 
    }

1 个答案:

答案 0 :(得分:0)

它与行:

    ListItem li = new ListItem(dar["tableName"].ToString(), dar["categoryId"].ToString());

categoryId是一个恒定的值,因此问题,我的坏。 感谢