2011-06-30 5 views
-2
I have got this code: 

public partial class Default2 : System.Web.UI.Page 
{ 
    public bool MyProperty { get; set; } 
    Button newBTN; 
    GenerateMe gm; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     newBTN = new Button(); 
     newBTN.Text = "Button 1"; 


     gm = new GenerateMe(PlaceHolder1, newBTN); 
     gm.ExecuteAll();  
     Response.Write(gm.ResponseWrite()); 

    } 


} 

class GenerateMe 
{ 
    PlaceHolder holder; 
    Button button; 
    RadioButton b = new RadioButton(); 
    string buttonPressed; 

    public GenerateMe(PlaceHolder h, Button b) 
    { 
     holder = h; 
     button = b; 
    } 

    public void ExecuteAll() 
    { 

     Table t = new Table(); 
     TableRow tr = new TableRow(); 
     TableCell tc = new TableCell(); 
     tc.Controls.Add(button); 
     tr.Cells.Add(tc); 
     t.Rows.Add(tr); 
     holder.Controls.Add(t); 
     holder.Controls.Add(b); 
     if (b.Checked)//This is always false 
     { 
      buttonPressed = b.Checked.ToString(); 
     } 

    } 

    public string ResponseWrite() 
    { 
     return buttonPressed; 
    } 

} 

ラジオボックスがチェックされていることがわかります。しかし、チェックされたプロパティはポストバック間で常にfalseです。それが押された場合、ラジオボックスの状態を取得するにはどうすればいいですか。ポストバック間でのラジオボックス状態の取得

ps。私はradioboxesイベントを使用したくない。ポストバック後に押された場合、プロパティを取得したい。

+0

コード内のCheckBoxはどこにありますか?ラジオボタンについて話していますか? – Illuminati

+0

申し訳ありませんラジオボックスです...前にチェックボックスを使用しました..私はそれを変更しました –

+0

ラジオボタンのステータスを確認するのはいつですか?コードでコメントなどを指定します。 – Illuminati

答えて

1

変数をセッション変数に保存する。

string check=Session["buttonPressed"].ToString(); 

EDIT:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 

     newBTN = new Button(); 
     newBTN.Text = "Button 1"; 


     gm = new GenerateMe(PlaceHolder1, newBTN); 
     gm.ExecuteAll();  
     Response.Write(gm.ResponseWrite()); 
     } 
    } 

コントロールを生成するために使用PREINITイベント、他のページで

Session["buttonPressed"]="checked"; 

は、としてそれを呼び出します。

+0

@Dmitryあなたは別のページでそれをフェッチしたいですか? ViewStateは単純な値をフォームに保存するのにはかなり良いですが、より複雑なデータを保存してページ間で保持したい場合は、Cookieやセッションの使用方法を調べる必要があります。 ://asp.net-tutorials.com/state/viewstate/) – Jayesh

+0

b.Checked.ToStringは常にfalseです!..それはポストバックの原因です。ボタンをクリックすると、ポストバックが発生します。私はそれがチェックされたかどうかを取ることができません...しかし、チェックボックスは、ポストバックの後にチェックされているようです(実際にはラジオボックス..ソロリー) –

+0

boolについて何がとても複雑です:( –

関連する問題