1

私は自分自身を Web プログラマーとは考えていませんが、かなりの量の Web プログラミングを行ってきたので、以下のコードの何が問題なのかを尋ねるのはほとんど恥ずかしいことです。ASP.NET には、欠けているに違いない基本的なものがあります。

source.aspx と destination.aspx の 2 つのページがあります。

source.aspx - html:

<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
    <input id="Text1" type="text" />
    <input id="Text2" type="text" />
    <input id="Checkbox1" type="checkbox" />
    <input id="Submit1" type="submit" value="submit" />
</form>
</body>

destination.aspx - コード ビハインド:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Below variable gets assigned null.
        string text1 = Request.Form["Text1"];
    }

source.aspx フォームを送信すると、destination.aspx フォームに到達すると、FORM 変数に情報がありません。フォーム 'runat="server" ' により、ASP.NET ページ パイプラインにたどり着くことが保証され、実際にこのコードをステップ実行できると考えました。viewstate 以外に POST されたフォーム変数はなく、PARAMs コレクションには、装飾されたコントロール名に対応するものでさえも、コントロール データに対応するものはありません。問題は、少なくとも目的のページで、POST された変数が「消える」原因は何ですか?

これを機能させるための代替手段を探しているわけではありません (つまり、コントロール サーバー コントロールを runat="server" で作成するなど)。問題を解決できます。私が判断しようとしているのは、「私のコントロールが宛先ページで受信できないように見えるのは、ASP.NET の何ですか」ということです。ありがとう - 私は HTTP をかなりよく理解していると思っていましたが、私が見ていない ASP.NET のおかげでちょっとした手品があるようです。

4

2 に答える 2

5

You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.

It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.

Consult HTML input - name vs. id for more information on id vs. name

Good luck

于 2013-08-01T06:54:25.140 に答える
3

Request.Form は要素の name 属性を使用します。したがって、各 html 要素に name 属性を記述する必要があります。

<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
    <input id="Text1" name="Text1" type="text" />
    <input id="Text2" name="Text2" type="text" />
    <input id="Checkbox1" name="Checkbox1" type="checkbox" />
    <input id="Submit1" type="submit" value="submit" />
</form>
</body>
于 2013-09-23T02:50:13.870 に答える