一括で取得

Page.Request.Formをぐるぐる回して、PostBack時に値をごっそり取得しようと思ったら、EnableがFalseのコントロールは取得できないことが発覚。当たり前だ・・・。ということでこちらを作成したのでメモ。

ボタンクリック時に、テキストボックスの値とコントロール名を取得してラベル(lblResult)に表示するだけ。

protected void Button1_Click(object sender, EventArgs e)
{
    string allTextBoxValues = "";
    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
                allTextBoxValues += ((TextBox)childc).UniqueID + "【" + ((TextBox)childc).Text + "】";
            }
        }
    }
    if (allTextBoxValues != "")
    {
        lblResult.Text = allTextBoxValues;
    }
}