开发者

How to tell ASP.NET not to insert in the request the value of a control?

开发者 https://www.devze.com 2023-03-16 19:35 出处:网络
My question might sound strange, however, it is something I need to do. Consider having a textbox, and you want it not to go ser开发者_JS百科ver side, that must stay there, in the client, that control

My question might sound strange, however, it is something I need to do. Consider having a textbox, and you want it not to go ser开发者_JS百科ver side, that must stay there, in the client, that control must not partecipate to the request.

How to do this?

Thankyou


You can't have a server control that doesn't post back. The only way to do it would be to use a <input type="text"/> and put it outside the asp.net form. I can't imagine what you want to do it for though.

EDIT:

It has occurred to me you could also use javascript to move the textbox outside the asp.net form, but leave it as an <asp:TextBox/> so you can set properties server side.


How about deleting the textbox out of the DOM with JavaScript on form submission?

Here's a jQuery example. Tag the textbox with a class, e.g. doNotSubmit

<asp:TextBox ID="TextBox1" runat="server" CssClass="doNotSubmit" />

Then make the form's submit event remove that textbox.

<script type="text/javascript">
    $(document).ready(function () {
        $("form").submit(function (e) {
            $(".doNotSubmit").remove();
        });
    });
</script>

Checked this in Firebug and the textbox is not being submitted. However you have introduced a JavaScript dependency, if someone is running without script, the textbox will still be submitted. Don't know if this is a problem for you or not.


To do this you want to use an plain HTML control as previously mentioned, but this will still be part of the HTTP Request and be available through Request.Form. I believe disabled controls are not included in the HTTP request so either have the control disabled from the start, or disable it via javascript on form submission.

Edit: Worth a shot would be disabling a asp:TextBox with EnableViewState=false, on form submission.


In the ASP (markup) code of the TextBox, set EnableViewState="false".

<asp:TextBox id="foo"  EnableViewState="false" />

When you check foo.Text in the code-behind, it will be the initial value (empty string).

If you don't want the control exist at all on the server, just use normal HTML instead of ASP.NET markup:

<input type="text" />

http://www.w3schools.com/tags/tag_input.asp

However, the textbox value is still technically included in the HTTP POST (postback). The browser always sends the form values on POST.

To exclude some element from POST, you can do a selective post using AJAX.

0

精彩评论

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