0

我有两个动态填充的单选按钮列表。第一个在按钮单击时填充,另一个在第一个单选按钮列表的更改事件中填充。问题仅在于第二个列表。问题是我无法在 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(); 
    }
4

1 回答 1

0

它是这样的:

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

categoryId 是一个常量值,因此也是我的问题。谢谢

于 2013-02-05T22:01:30.383 回答