在c#代码隐藏中从asp控件检索值

在c#代码隐藏中从asp控件检索值,c#,asp.net,C#,Asp.net,我已经广泛地寻找这个问题的答案,但我只是非常接近。我有一个用于添加和编辑记录的web表单。在gridview中选择记录时,将设置一个会话变量,然后在页面加载时使用该变量填充文本字段。如果未设置会话变量,则表单将为空,逻辑将作为新记录运行。我的问题是我可以成功地添加一条新记录-我调试并检查了asp控件,以确保将正确的值传递给代码隐藏-但我无法成功地编辑记录。由于某些原因,代码隐藏文件无法从文本框中检索正确的值。相反,它保留原始填充的值,因此无法达到编辑的目的。我想这是一个有约束力的问题,但我不确定

我已经广泛地寻找这个问题的答案,但我只是非常接近。我有一个用于添加和编辑记录的web表单。在gridview中选择记录时,将设置一个会话变量,然后在页面加载时使用该变量填充文本字段。如果未设置会话变量,则表单将为空,逻辑将作为新记录运行。我的问题是我可以成功地添加一条新记录-我调试并检查了asp控件,以确保将正确的值传递给代码隐藏-但我无法成功地编辑记录。由于某些原因,代码隐藏文件无法从文本框中检索正确的值。相反,它保留原始填充的值,因此无法达到编辑的目的。我想这是一个有约束力的问题,但我不确定,并已搜索结束。这是我的代码隐藏文件:

protected void Page_Load(object sender, EventArgs e)
{
    resultOutput.Visible = false;//Output results as to whether or not a record was added successfully is automatically hidden at page load

    //Checking to see if session variable has been created
    if (Session["editID"] != null)
    {               
        //Create objects to get recipe data
        dbCRUD db = new dbCRUD();
        Recipe editRecipe = new Recipe();

        //Grabbing session ID 
        var id = Convert.ToInt32(Session["editID"]);

        //Call method to retrieve db data
        editRecipe = db.SelectRecord(id);

        //Populate results to text boxes
        recordID.Text = editRecipe.Recipe_ID.ToString();
        addName.Text = editRecipe.Name;
        addTypeDDL.SelectedValue = editRecipe.Meal;
        addDifficultyDDL.SelectedValue = editRecipe.Difficulty;
        addCookTime.Text = editRecipe.Cook_Time.ToString();
        addDirections.Text = editRecipe.Directions;

        //Change Button Text
        submitRecord.Text = "Edit Record";

        //Change Title Text
        addEditTitle.Text = "Edit Recipe";

    }
}

protected void submitRecord_Click(object sender, EventArgs e) 
{
    Recipe recipe = new Recipe();
    dbCRUD newRecord = new dbCRUD();

    //Variables for execution results
    var modified = "";
    int returned = 0;

    //Creating the recipe Object to pull the values from the form and 
    //send the recipe object as a parameter to the method containing insert stored procedure
    //depending on Add or Edit
    //recipe.Recipe_ID = int.Parse(recordID.Text);
    recipe.Name = addName.Text.ToString();
    recipe.Meal = addTypeDDL.SelectedValue.ToString();
    recipe.Difficulty = addDifficultyDDL.SelectedValue.ToString();
    recipe.Cook_Time = int.Parse(addCookTime.Text);
    recipe.Directions = addDirections.Text.ToString();


    //Checking to see if the page is loaded for edit or new addition
    if (Session["editID"] != null)
    {
        recipe.Recipe_ID = Convert.ToInt32(Session["editID"]);
        //If recordID exists, recipe will be passed to UpdateRecord method
        returned = newRecord.UpdateRecord(recipe);
        modified = "has been edited.";
        Session.Remove("editID");
    }
    else
    {
        //If recordID does not exist, record will be passed to InsertRecord method (new recipe)
        returned = newRecord.InsertRecord(recipe);
        modified = "added";
    }

    //Method returns 0 if successful, 1 if sql error, 2 if other error
    if (returned == 1)
    {
        resultOutput.Text = "There was an sql exception";
        resultOutput.Visible = true;
    }
    else if (returned == 2)
    {
        resultOutput.Text = "There was a non sql exception";
        resultOutput.Visible = true;
    }
    else
    {
        resultOutput.Text = "\"" + addName.Text + "\" recipe " + modified;
        resultOutput.Visible = true;
    }
}

任何传递到我的编辑方法的对象都是成功的,但是,正如我提到的,它不会获取新更新的文本框值

我建议使用静态数据集并将其绑定到gridview控件的recordsource。每当您想要编辑记录时,请同时更新数据集并将其重新绑定到gridview控件…希望这有帮助:)

是否尝试检查
PostBack
属性,每次页面发回时,您的代码都会加载数据。因此,当您更新表单中的值并点击更新按钮时。首先调用Page_Load方法,它重新加载所有数据(替换表单上的更新值),然后点击更新按钮事件处理程序。所以每次保存旧值时


您可以从page_load方法中删除代码,并将其放在设置
会话[“EditId”]
值的位置。这将解决您的问题。

因此,is
db.SelectRecord(id)页面重新加载时调用?谢谢提示,Irfan。我将尝试上面发布的其他方法,因为它需要最少的代码更改,但是如果我不能得到预期的结果,我将使用静态数据集选项。你得到了!!愚蠢的错误,但是是的-我将页面加载时触发的所有代码放入if(!IsPostBack){}控制语句中,一切都很好!