我有一个表单,我想重复用于添加新记录和编辑现有记录。当我从GridView中选择记录时,我能够成功加载包含相关数据的页面,并且我能够正确地更新db记录。但是,我的问题是尝试将表单用于两个执行。这是我的代码背后的逻辑:(当我点击GridView中的行时,我分配了一个会话变量,这确实有效)
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 and assigning to a variable in order to remove the session variable
var id = Convert.ToInt32(Session["editID"]);
Session.Remove("editID");
//Call method to retrieve db data
editRecipe = db.SelectRecord(id);
//Populate results to text boxes
recordID.Text = 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.Visible = false;
changeRecord.Visible = true;
//Change Title Text
addEditTitle.Text = "Edit Recipe";
}
}
protected void submitRecord_Click(object sender, EventArgs e)
{
//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 = new Recipe();
recipe.Name = addName.Text;
recipe.Meal = addTypeDDL.Text;
recipe.Difficulty = addDifficultyDDL.Text;
recipe.Cook_Time = int.Parse(addCookTime.Text);
recipe.Directions = addDirections.Text;
//Creating object to access insert method
dbCRUD newRecord = new dbCRUD();
//Checking to see if the page is loaded for edit or new addition
if (Session["editID"] != null)
{
//If recordID exists, recipe will be passed as to UpdateRecord method
recipe.Recipe_ID = int.Parse(recordID.Text);
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;
}
}
我在if(Session [“editID”]!= null)行上有问题 - 我总是转移到else逻辑,if逻辑永远不会运行。
这是我在GridView中的click方法:
protected void Grid_Recipe_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(Grid_Recipe.SelectedDataKey.Value);
Session["editID"] = index;
Response.Redirect("addRecord.aspx");
}
我的问题是如何在submitRecord_Click事件期间控制执行,以便我调用适当的方法。谢谢!
答案 0 :(得分:1)
您是否考虑过使用
if(Page.IsPostBack)
{
code here
}
要检测您是否要回发到该页面?然后你可以检查你的物品价值。我认为没有理由代码不应该在Session变量中 - 你是否尝试在那里放一个断点来查看代码是否真的进入那里?
你的addRecord.aspx也只是添加记录吗?如果是这样,只需在此类中添加记录,但使用PostBack检查查看。您能否确保在正确的背景下保存:
// Outside of Web Forms page class, use HttpContext.Current.
HttpContext context = HttpContext.Current;
context.Session["editID"] = index;
...
int Id = (string)(context.Session["editID"]);
答案 1 :(得分:0)
我能够找出我的问题 - 实际上变成了两个问题。首先,我必须将我的页面加载逻辑放在if(!IsPostBack)语句中,因为我无法编辑页面。原因是我正在将最初发布的数据加载到页面加载的表单上,该表单在我的逻辑之前执行。添加if(!IsPostBack)控制语句修复了此问题。从那里开始,我仍然使用会话变量来控制逻辑背后的代码,只是我确保只在表单和gridview之间保留我的会话变量。基本上,当gridview加载并且它不是回发时,会话变量被清除。这让我在单击一行时设置一个新的会话变量,然后一旦我返回网格查看结果就清除会话变量。谢谢你的帮助!