asp.net编辑数据

发布于 2024-12-23 10:04:35 字数 3013 浏览 0 评论 0原文

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

    <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" />

我像这样填写 Page_PreRender() 中的字段:

    private string _topicString;
    private Topic _topic = null;
    private Topics_GetTopicByTopicResult _findTopicResults = null;

    protected void Page_PreRender(object sender, EventArgs e)
    {
        // Load the User Roles into checkboxes.
        _dtlEditRole.DataSource = Roles.GetAllRoles();
        _dtlEditRole.DataBind();
        _dtlViewRole.DataSource = Roles.GetAllRoles();
        _dtlViewRole.DataBind();

        _topicString = Request.QueryString["Topic"];

        if (String.IsNullOrEmpty(_topicString))
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            _topic = new Topic();
            _findTopicResults = _topic.FindTopic(_topicString);

            if (_topic != null)
            {
                // Check if the user has permission to access
                if (RoleHelper.IsEditAllowed(_findTopicResults.ViewRoleName))
                {
                    _lblTopicName.Text = _findTopicResults.Topic;
                    _tbxTopicText.Text = _findTopicResults.Text;

                    _dtlEditRole.SelectedValue = _findTopicResults.EditRoleName;
                    _dtlViewRole.SelectedValue = _findTopicResults.ViewRoleName;
                }
                else
                {
                    Response.Redirect("~/Error.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));
                }
            }
            else
            {
                Response.Redirect("~/CreateTopic.aspx?Topic=" + _topicString);
            }
        }
    }

但是现在当我单击 _btnSaveTopic 按钮时,字段:

    private string _topicString;
    private Topic _topic = null;
    private Topics_GetTopicByTopicResult _findTopicResults = null;

它们都是 NULL 并且即时消息无法更新任何内容。

这是我的按钮单击事件:

    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);
    }

这样做的正确方法是什么?

I'm reading data from database and showing it in a page for editing:

    <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" />

I fill the fields in Page_PreRender() like so:

    private string _topicString;
    private Topic _topic = null;
    private Topics_GetTopicByTopicResult _findTopicResults = null;

    protected void Page_PreRender(object sender, EventArgs e)
    {
        // Load the User Roles into checkboxes.
        _dtlEditRole.DataSource = Roles.GetAllRoles();
        _dtlEditRole.DataBind();
        _dtlViewRole.DataSource = Roles.GetAllRoles();
        _dtlViewRole.DataBind();

        _topicString = Request.QueryString["Topic"];

        if (String.IsNullOrEmpty(_topicString))
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            _topic = new Topic();
            _findTopicResults = _topic.FindTopic(_topicString);

            if (_topic != null)
            {
                // Check if the user has permission to access
                if (RoleHelper.IsEditAllowed(_findTopicResults.ViewRoleName))
                {
                    _lblTopicName.Text = _findTopicResults.Topic;
                    _tbxTopicText.Text = _findTopicResults.Text;

                    _dtlEditRole.SelectedValue = _findTopicResults.EditRoleName;
                    _dtlViewRole.SelectedValue = _findTopicResults.ViewRoleName;
                }
                else
                {
                    Response.Redirect("~/Error.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));
                }
            }
            else
            {
                Response.Redirect("~/CreateTopic.aspx?Topic=" + _topicString);
            }
        }
    }

But now when i click _btnSaveTopic button the fields:

    private string _topicString;
    private Topic _topic = null;
    private Topics_GetTopicByTopicResult _findTopicResults = null;

They are all NULL and im not able to update aything.

Here's my button click event:

    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);
    }

What would be the right way doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2024-12-30 10:04:35

ASP.NET 页面生命周期 指出 Page_Init 应用于 '初始化控件属性',这看起来像您正在做的事情。

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

您可以通过右键单击 Visual Studio 中突出显示的代码部分开始 ->重构-> extract 方法

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

ASP.NET Page Life Cycle states that Page_Init should be used to 'initialize control properties' which looks like what you are doing.

Also, it's usually good practice to breakup such large sections of code into smaller refactored methods. Try to keep the amount of code directly placed in event handlers to a minimum.

You can start by right-clicking a section of highlighted code in visual studio -> refactor -> extract method

Also, if you need more help understanding how to improve your code, you should ask a question pointing to this question on the code review site: here

娇柔作态 2024-12-30 10:04:35

您正在 Page_PreRender 方法中重新绑定下拉列表(并因此清除“SelectedValue”)。将方法包装进去

protected void Page_PreRender(object sender, EventArgs e)
{
  if( !IsPostBack){
    //your current code
  }
}

,它应该可以工作。

You are re-binding the drop down list (and therefore wiping out the 'SelectedValue') in your Page_PreRender method. Wrap the method in

protected void Page_PreRender(object sender, EventArgs e)
{
  if( !IsPostBack){
    //your current code
  }
}

and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文