开发者

ASP.NET How to read HTML Form Element?

开发者 https://www.devze.com 2022-12-20 05:29 出处:网络
I have an ASP.NET web form where I have an hidden field, like this: <form id=\"form1\" runat=\"server\" action=\"http://localhost/fa/Default.aspx\">

I have an ASP.NET web form where I have an hidden field, like this:

<form id="form1" runat="server" action="http://localhost/fa/Default.aspx">
        <div>
    <input id="requestData" type="hidden" name="requestData" value="" runat="server" />
    <asp:Button ID="btnPOST" Text="POST" runat="server" OnClick="do_POST" />
        </div>
    </form>

On the method do_POST I have this:

    protected void do_POST(object sender, EventArgs e)
        {
//requestDataField is of the type protected global::System.Web.UI.HtmlControls.HtmlInputHidden requestData;

            requestDataField.Text = "FOO!";
        }

When I submit the form开发者_运维技巧 (by pressing the button), it goes to the server (an handler) wheer I have this:

string requestData = context.Request.Form["requestData"];

I get an empty string..

But if I assign a value like this:

<input id="requestData" type="hidden" name="requestData" value="FOO" runat="server" />

I get the "FOO"

What am I missing?


The reason why it's not doing it is because the method is called after the page has been post back. Meaning, it is actually working if you change .Text to .Value unfortunately by that time you have already read your form and it was an empty value. I remember working on a project where you could tell your form not to submit until a function has been run (but it was with a javascript that needed to run an complete before aspx submitted). You should try to see if there is a way to force your form to run your function BEFORE doing the postback.


Your do_POST method runs on the server, not on the client, and so is setting the value of the server-side object which represents the <input> control. Your context.Request.Form["requestData"] gets the value of the field from the client side data submitted in the POST request, which was never set, so it is blank.

If you want the onClick to be a client-side function, then you need to do it a little differently. Use the OnClientClick attribute (instead of onClick). Then create a javascript method to set the field value:

<asp:Button ID="btnPOST" Text="POST" runat="server" OnClientClick="do_POST" /> 

<script>
  function do_POST() {
    document.getElementById("requestData").value = "FOO!";
  }
</script>


I tried your code and did few changes to it.

  1. Change requestDataField.Text = "FOO!"; to requestData.Value = "FOO";

  2. Also I added two buttons. One for do_POST function and the UseSubmitBehaviour property is set as False. The other one was to submit the form.

If you want to set it on client side then you will have to use Javascript.


Use "Value" instead of "Text" property for HtmlInputHidden control:

requestDataField.Value = "FOO!";

instead of

requestDataField.Text = "FOO!";

0

精彩评论

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

关注公众号