ユーザー入力を収集するページからユーザーがリダイレクトできるようにする必要があります。ユーザーがリダイレクトした場合、ページに戻ると、フォームにはユーザーが既に入力した値を入力する必要があります。辞書キーからフォームフィールド値を復元する方法
私は以下を完了しましたが、これを行うにはよりよい方法があると思います。
On RedirectEvent()
{
Dictionary<string, string> form = new Dictionary<string, string>();
foreach (string key in Request.Form.AllKeys)
{
if (key != null)
form.Add(key, Request.Form[key]);
}
Session["requestFormKeys"] = form;
Response.Redirect(url);
}
On Page_Load(object sender, EventArgs e)
{
if (Session["requestFormKeys"] != null)
{
Dictionary<string, string> form = Session["requestFormKeys"] as Dictionary<string, string>;
// I tried using 'Request.Form.AllKeys' here but it was always null
foreach (KeyValuePair<string, string>pair in form)
{
// cannot use a switch because switch requires a constant (value must be known at compile time)
if (pair.Key.Contains("txtName"))
txtName.Text = lblNameView.Text = pair.Value;
else if (pair.Key.Contains("ddlType"))
ddlType.SelectedValue = pair.Value;
else if (pair.Key.Contains("ddlPriority"))
ddlPriority.SelectedValue = pair.Value;
.
.
.
//this is a tedious process and should be streamlined
.
.
.
else if (pair.Key.Contains("txtDateStart"))
txtDateStart.Text = pair.Value;
else if (pair.Key.Contains("txtDateEnd"))
txtDateEnd.Text = pair.Value;
}
}
Session.Remove("requestFormKeys");
}
}
助けてください。
が、私の質問はどのように保存されています辞書の状態..あなたはおそらくセッションオブジェクトを使用してSession.Addメソッドを調べることができますちょうどとアイデア..またはクッキー..?大量のテキスト/データを保存していない限り、隠しフィールドやViewStateを見ることができます。これは私が個人的に使うべきではない場合は個人的には使用しないでしょう.. Session.varsを使用したり保存したりできるGlobal.asaxセクションがあります。Webを扱うときは一般的にSession変数を使用します。選択肢.. – MethodMan
「辞書の状態をどのように保存していますか」と言うと、辞書をどのように保存しているのか聞いていますか?もしそうなら、それをOnRedirectEvent()メソッドのSessionに保存しています – Bengal
私は、オブジェクトがnullになるはずですが、個人的にはセッションやCookies Alansの例がそのトリックを行うべきであるというポストバックのために、 – MethodMan