C# C代码隐藏控制表单执行
我有一个表单,我想在添加新记录和编辑现有记录时重用它。当我从GridView中选择一条记录时,我能够成功地加载包含相关数据的页面,并且我能够适当地更新db记录。然而,我的问题是尝试将表单用于两种执行。下面是我在代码隐藏中的逻辑:当我在GridView中单击行时,我分配了一个会话变量,这确实有效C# C代码隐藏控制表单执行,c#,asp.net,gridview,webforms,C#,Asp.net,Gridview,Webforms,我有一个表单,我想在添加新记录和编辑现有记录时重用它。当我从GridView中选择一条记录时,我能够成功地加载包含相关数据的页面,并且我能够适当地更新db记录。然而,我的问题是尝试将表单用于两种执行。下面是我在代码隐藏中的逻辑:当我在GridView中单击行时,我分配了一个会话变量,这确实有效 protected void Page_Load(object sender, EventArgs e) { resultOutput.Visible = false;//Output resul
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;
}
}
我在ifSession[editID]上有问题空行-我总是移动到else逻辑,而if逻辑从不运行
以下是我在GridView中的单击方法:
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事件期间控制执行,以便调用适当的方法。谢谢 您考虑过使用
if(Page.IsPostBack)
{
code here
}
检测您是否正在发回页面?然后您可以检查项目的价值。我看不出代码不应该出现在会话变量中的任何原因-您是否尝试过在其中放置断点以查看代码是否实际出现在其中
您的addRecord.aspx是否也只是添加记录?如果是这样,只需在这个类中添加记录,但使用回发检查查看。您能否确保您也在正确的上下文中保存:
// Outside of Web Forms page class, use HttpContext.Current.
HttpContext context = HttpContext.Current;
context.Session["editID"] = index;
...
int Id = (string)(context.Session["editID"]);
我能够解决我的问题——这实际上变成了两个问题。首先,我必须将我的页面加载逻辑放入if!IsPostBack语句,因为我无法编辑该页。原因是我在页面加载时将最初发布的数据加载到表单中,页面加载在我的逻辑之前执行。添加if!IsPostBack控制语句修复了此问题。从那时起,我仍然使用会话变量来控制逻辑背后的代码,只是我确保会话变量只在表单和gridview之间。基本上,当gridview加载且不是回发时,会话变量被清除。这让我在单击一行时设置一个新的会话变量,然后在返回网格查看结果时清除会话变量。谢谢你的帮助 我忘了我在上面的代码中包含了一个调用,以便在加载文本后删除会话变量。这让我看到会话变量出现在前面提到的if语句中,但由于某种原因,我无法再从web表单OnClick中提取更改后的值???正在处理它…你会有其他页面遵循类似的模式吗?我绝对希望有最少的代码量。我不想创建一个单独的表单来捕获相同的内容,我希望使用相同的方法从文本框中提取数据,从而尝试通过基于会话变量的if语句来控制执行。我对更好的方法持开放态度,尽管这可能超出了我的问题范围。