0

2 つの動的に設定されたラジオ ボタン リストがあります。最初のものはボタンのクリックで生成され、もう 1 つは最初のラジオ ボタン リストの変更イベントで生成されます。問題は 2 番目のリストだけです。問題は、InsertButton_Click メソッド (** でマーク) の 2 番目のラジオ ボタン リストの変更された値を取得できないことです。常にデフォルトのインデックス値、つまり 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 に答える