2011-12-29 18 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); 
      } 
     } 
    } 

しかし、彼らはすべてNULLです

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

なくaything更新できてイム:今私は_btnSaveTopicボタンをフィールドをクリックしたとき。

は、ここに私のボタンクリックイベントです:

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を使用する必要がありますか? –

+1

ページライフサイクルには、Page_Initを使用して、コントロールのプロパティを初期化する必要があることが記載されています...また、そのイベントであまりにも多くのロジックを詰め込んだようです。キンダー醜い。 –

+0

@ subt13:あなたは正しかった、私はasp.netに新しい、どこにロジックを置くのに良い場所でしょうか? – hs2d

答えて

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

これはうまくいくはずです。

関連する問題