0

ユーザーが新しい行を直接追加できる動的テーブルを開発しようとしています。挿入された質問の ID を取得するためのコード ビハインドに問題がありますが、取得方法がわかりません。では、どうやって手に入れるのですか?

参考までに、私が持っているデータベース設計は次のとおりです。質問テーブル:QuestionID、Question、QuestionOrder、AnswerExplanation

ASP.NET コード:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Table ID="questionsList" runat="server" Width="50%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366; font-size:17px; font-weight:bold;">
        </asp:Table>

        <asp:Table ID="Table1" runat="server" Width="50%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366;">
            <asp:TableFooterRow>
                <asp:TableCell>Add</asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TBWE" runat="server"
                                    TargetControlID="Question" WatermarkText="Type the Question here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>
                <asp:TableCell>
                    <span style="margin:3px 10px 0px 0px;">
                        <asp:ImageButton ID="addButton" runat="server" ImageUrl="Images/tick_small.png" OnClick="addQuestion" />
                    </span>
                </asp:TableCell>
            </asp:TableFooterRow>
        </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel> 

コード ビハインド:

public void fill_Questions()
    {
        string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string selectQuery = "SELECT * FROM Questions";
        SqlCommand cmd = new SqlCommand(selectQuery, conn);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;

        Table table = (Table)UpdatePanel1.FindControl("questionsList");
        try
        {
            //open the connection
            conn.Open();
            //filling the dataset with the data
            sda.Fill(ds);
            //close the connection
            conn.Close();
        }
        catch
        {
            table.Rows.Clear();
        }


        //Loading the data into the table
        if (ds.Tables.Count > 0)
        {
            int rowsNum = ds.Tables[0].Rows.Count;
            for (int i = 0; i < rowsNum; i++) 
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);

                for (int colCount = 0; colCount < 4; colCount++)
                {
                    TableCell cell = new TableCell();
                    row.Cells.Add(cell);

                    if (colCount == 0)
                    {
                        int n = i + 1;
                        cell.Controls.Add(new LiteralControl(n.ToString()));
                    }
                    else if (colCount == 1)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["Question"].ToString()));
                    }
                    else if (colCount == 2)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["QuestionOrder"].ToString()));
                    }
                    else if (colCount == 3)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["AnswerExplanation"].ToString()));
                    }
                    else if (colCount == 4)
                    {
                        ImageButton deleteButton = new ImageButton();
                        deleteButton.ImageUrl = "Images/DeleteRed.png";
                        deleteButton.ID = ds.Tables[0].Rows[i]["Question"].ToString() + "_" + i;
                        deleteButton.Click += new ImageClickEventHandler(deleteQuestion);
                        cell.Controls.Add(deleteButton);

                    }
                } //End for loop
            }//End OUTER for loop

        }//End if statement

    }


    //For deleting question
    public void deleteQuestion(object sender, EventArgs e) 
    {
        //int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split()[0]);
        try 
        {
            string connString = ConfigurationManager.ConnectionStrings["QuizSysDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string deleteQuery = "DELETE FROM Question WHERE ID = @id";
            SqlCommand cmd = new SqlCommand(deleteQuery,conn);
            cmd.Parameters.AddWithValue("@id", ID);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            fill_Questions();
        }
        catch(SqlException se){}
    }

私の問題は、deleteQuestion メソッド内の次の行とまったく同じです。

 //int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split()[0]);
4

1 に答える 1

0

この方法でimagebuttonidを取得できます

int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split('_')[1]);
于 2012-07-18T08:12:26.043 に答える