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

您可以通过这种方式获取 imagebutton id

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