2011-12-29 47 views
1

我從數據庫中讀取數據,並將其顯示在頁面編輯:asp.net編輯數據

<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和IM無法更新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); 
    } 

會是怎樣以正確的方式做這個?

+1

你必須使用Page_PreRender? – 2011-12-29 22:41:54

+1

Page生命週期表明Page_Init應該用來'初始化控制屬性',它看起來像你正在做的事情...另外,看起來你在那個事件中填充了太多的邏輯。有點醜。 – 2011-12-29 22:45:07

+0

@ subt13:你是對的,即時通訊新的asp.net,在哪裏放置邏輯的好地方? – hs2d 2011-12-29 22:49:51

回答

1

ASP.NET Page Life Cycle指出Page_Init應該用來'初始化控制屬性',它看起來像你在做什麼。

另外,通常會將這些大部分代碼分解爲更小的重構方法。儘量保持事件處理程序中直接放置的代碼量最小。

您可以通過右鍵單擊突出顯示的代碼段在Visual Studio中開始 - >重構 - >提取方法

另外,如果你需要更多的幫助,瞭解如何提高你的代碼,你應該問一個問題指點在代碼審查站點上的這個問題:here

1

您正在重新綁定您的Page_PreRender方法中的下拉列表(並因此刪除'SelectedValue')。將方法包裝在

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

它應該工作。