Skip to main content
 首页 » 编程设计

c#之查询字符串值始终为 0

2024年02月13日21artech

这可能很简单,但我看不出有什么问题..

我已将此字符串添加到我的 url:/projects.aspx?pid=3

在我后面的代码中,我试图像这样获取这个值:

public partial class Projects : System.Web.UI.Page 
{ 
    public int pid { get; set; } 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            pid = Convert.ToInt32(Request.QueryString["pid"]); //this is returning 0 when it should have a value. 
            DataBind(true); 
 
            PopulateTestSuiteList(); 
        } 
 
        TestSuitesDataList.ItemCommand += new RepeaterCommandEventHandler(this.Item_Command); 
    } 

当我尝试在其他函数中查看 pid 的值时,它总是给我“0”。我不明白为什么它不能像它应该的那样给我 3?

以下是示例函数:

private void ExecuteInsert(string TestSuiteName, string TestSuiteDescription, int ProjectID) 
    { 
        SqlConnection conn = new SqlConnection(GetConnectionString()); 
        string sql = "INSERT INTO TestSuites (ProjectID, TestSuiteName, TestSuiteDescription) VALUES " + " (@ProjectID,@TestSuiteName,@TestSuiteDescription)"; 
        try 
        { 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
 
            SqlParameter[] param = new SqlParameter[3]; 
 
            param[0] = new SqlParameter("@TestSuiteName", SqlDbType.NVarChar, 50); 
            param[1] = new SqlParameter("@TestSuiteDescription", SqlDbType.NVarChar, 200); 
            param[2] = new SqlParameter("@ProjectID", SqlDbType.Int); 
 
            param[0].Value = TestSuiteName; 
            param[1].Value = TestSuiteDescription; 
            param[2].Value = pid; 
 
            for (int i = 0; i < param.Length; i++) 
            { 
                cmd.Parameters.Add(param[i]); 
            } 
 
            cmd.CommandType = CommandType.Text; 
            cmd.ExecuteNonQuery(); 
        } 
        catch (System.Data.SqlClient.SqlException ex) 
        { 
            string msg = "Insert Error:"; 
            msg += ex.Message; 
            throw new Exception(msg); 
        } 
        finally 
        { 
            conn.Close(); 
        } 
    } 
 
    protected void AddTestSuiteButton_Click(object sender, EventArgs e) 
    { 
        //checks whether the test suite name already exists in the database and stops duplicates from being added for the same project 
        string checkExistsQuery = "SELECT TestSuiteName FROM TestSuites WHERE TestSuiteName = @TestSuiteName and @ProjectID = " + pid; 
 
        SqlConnection conn = new SqlConnection(GetConnectionString()); 
        SqlCommand checkExistsCmd = new SqlCommand(checkExistsQuery, conn); 
 
        SqlParameter c1 = new SqlParameter("TestSuiteName", TxtTestSuiteName.Text); 
        SqlParameter c2 = new SqlParameter("ProjectID", pid); 
 
        checkExistsCmd.Parameters.Add(c1); 
        checkExistsCmd.Parameters.Add(c2); 
 
        conn.Open(); 
 
        SqlDataReader rd = checkExistsCmd.ExecuteReader(); 
 
        if (rd.HasRows) 
        { 
            lblAddTestSuiteMessage.Text = "Oops, a Project with that name already exists."; 
            rd.Close(); 
            conn.Close(); 
        } 
        else 
        { 
            string FormattedTestSuiteDescription = TxtTestSuiteDescription.Text.Replace("\r\n", "<br />"); 
            //call the method to execute insert to the database 
            ExecuteInsert(TxtTestSuiteName.Text, FormattedTestSuiteDescription, pid); 
 
            lblAddTestSuiteMessage.Text = "Project has been added successfully"; 
            ClearControls(Page); 
        } 
 
        rd.Close(); 
        conn.Close(); 
        Response.Redirect(Request.RawUrl); 
    } 

在相同的代码后面,这个函数似乎很高兴地提取正确的数据并将其显示在我的页面上——它也在使用 PID??

        private void PopulateTestSuiteList() 
    { 
        SqlConnection conn = new SqlConnection(GetConnectionString()); 
        conn.Open(); 
        string sql = "SELECT TestSuiteID, ProjectID, TestSuiteName, TestSuiteDescription FROM TestSuites WHERE ProjectID = " + pid + "ORDER BY TestSuiteName ASC"; 
        SqlCommand cmd = new SqlCommand(sql, conn); 
 
        SqlDataReader rd = cmd.ExecuteReader(); 
        while (rd.Read()) 
        { 
            TestSuitesDataList.DataSource = rd; 
            TestSuitesDataList.DataBind(); 
        } 
        conn.Close(); 
    } 

请您参考如下方法:

这可能是由于没有完全理解 page life cycle 造成的.您在 Page_Load 上设置了 pid,但可能在页面尚未加载或重新加载时的某处使用它(例如,在回发之后)。

如果您从查询字符串中获取pid 值并需要在回发时使用它,则需要将其存储在 session 变量 或隐藏字段中 在页面上。将它分配给页面类变量不会像您在此处所做的那样持久化它。