Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net编辑数据_Asp.net_Page Lifecycle - Fatal编程技术网

asp.net编辑数据

asp.net编辑数据,asp.net,page-lifecycle,Asp.net,Page Lifecycle,我正在从数据库中读取数据,并将其显示在页面中进行编辑: <h2>Create new topic: <asp:Label ID="_lblTopicName" runat="server" Text=""></asp:Label></h2> <p> Edit Level: <br/> <asp:DropDownList ID="_dtlEditRole"

我正在从数据库中读取数据,并将其显示在页面中进行编辑:

    <h2>Create new topic: 
    <asp:Label ID="_lblTopicName" runat="server" Text=""></asp:Label></h2>
      <p>
      Edit Level:
      <br/>
      <asp:DropDownList ID="_dtlEditRole" runat="server"></asp:DropDownList>
      <br/>
      View Level:
      <br/>
      <asp:DropDownList ID="_dtlViewRole" runat="server"></asp:DropDownList>
      <br/>
      <asp:TextBox ID="_tbxTopicText" TextMode="MultiLine" runat="server" Height="204px" 
        Width="885px"></asp:TextBox>
    </p>
  <asp:Button ID="_btnSaveTopic" runat="server" Text="Save" onclick="_btnSaveTopic_Click" />
但现在,当我单击
\u btnSaveTopic
按钮时,字段:

    private string _topicString;
    private Topic _topic = null;
    private Topics_GetTopicByTopicResult _findTopicResults = null;
它们都是空的,我无法更新aything

以下是我的按钮点击事件:

    protected void _btnSaveTopic_Click(object sender, EventArgs e)
    {
            _topic.UpdateTopic(_findTopicResults.ID, _findTopicResults.Topic, _tbxTopicText.Text,
                               _dtlViewRole.SelectedItem.Text, _dtlEditRole.SelectedItem.Text);
            Response.Redirect("~/ViewPage.aspx?Topic=" + _topicString);
    }

这样做的正确方式是什么

您正在重新绑定页面\u PreRender方法中的下拉列表(从而清除“SelectedValue”)。将该方法封装在

protected void Page_PreRender(object sender, EventArgs e)
{
  if( !IsPostBack){
    //your current code
  }
}
它应该可以工作。

说明应该使用Page_Init来“初始化控件属性”,这与您正在执行的操作类似

另外,将如此大的代码段分解成更小的重构方法通常是一种好的做法。尽量将直接放置在事件处理程序中的代码量保持在最低限度

您可以在VisualStudio->refactor->extract method中右键单击高亮显示的代码部分开始


此外,如果您需要了解如何改进代码的更多帮助,您应该在代码审阅网站上提出一个指向此问题的问题:

Page life cycle指出,Page_Init应用于“初始化控件属性”,这看起来像您正在做的事情……而且,在这种情况下,您似乎塞满了太多的逻辑。有点难看。@subt13:你说得对,我是asp.net新手,哪里是放置逻辑的好地方?
protected void Page_PreRender(object sender, EventArgs e)
{
  if( !IsPostBack){
    //your current code
  }
}