开发者

Getting the radiobox state between postbacks

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

}

I can s开发者_Python百科ee that the radiobox is checked..But its checked property is false all the time between post backs. How do i get the radiobox state if it was pressed..

ps. I dont want to use the radioboxes events.. I want to get a property if it was pressed after a postback


Keep the variable in session variable.

Session["buttonPressed"]="checked";

in other page call it as:

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

Use PreInit event to generate the controls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜