最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Using Javascript OnChange Event to Conditionally Cancel AutoPostBack on Textbox - Stack Overflow

programmeradmin1浏览0评论

I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack using a javascript onchange event. I want to do this because it would be easy for me to check some conditions on the client side that would negate the necessity of doing a post-back.

I have something like this generated in the client HTML:

<input name="tb1" type="text" onchange="return DoMyCheck(); setTimeout('__doPostBack(\'tb1\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="tb1"/>

<script type='text/javascript'>
    function DoMyCheck()
    {
        if(something)
        {
             return false;
        }else
        {
             return true;
        }
    }
</script>

I thought this would either go ahead with the postback if DoMyCheck() returned true or cancel it if DoMyCheck() returns false. Apparently it cancels every time no matter what. Keep in mind that I simplified the long .NET IDs and the setTimeout() and OnKeyPress() stuff was autogenerated when i set AutoPostBack = true. The only code that I really put in there was the onchange event.

I saw one solution posted here: .aspx

but this is unacceptable in my mind because I do not want to have to manually hook into internal .NET javascript to do the postback if I can help it.

Does anyone have a better solution? I greatly appreciate any help you can give.

I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack using a javascript onchange event. I want to do this because it would be easy for me to check some conditions on the client side that would negate the necessity of doing a post-back.

I have something like this generated in the client HTML:

<input name="tb1" type="text" onchange="return DoMyCheck(); setTimeout('__doPostBack(\'tb1\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="tb1"/>

<script type='text/javascript'>
    function DoMyCheck()
    {
        if(something)
        {
             return false;
        }else
        {
             return true;
        }
    }
</script>

I thought this would either go ahead with the postback if DoMyCheck() returned true or cancel it if DoMyCheck() returns false. Apparently it cancels every time no matter what. Keep in mind that I simplified the long .NET IDs and the setTimeout() and OnKeyPress() stuff was autogenerated when i set AutoPostBack = true. The only code that I really put in there was the onchange event.

I saw one solution posted here: http://www.eggheadcafe./munity/aspnet/3/10042963/try-this.aspx

but this is unacceptable in my mind because I do not want to have to manually hook into internal .NET javascript to do the postback if I can help it.

Does anyone have a better solution? I greatly appreciate any help you can give.

Share Improve this question asked Jul 9, 2009 at 14:32 jakejgordonjakejgordon 4,0987 gold badges38 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You are always returning, so postback never gets invoked. You should only return false (cancel the event) if your function returns false...

onchange="if(!DoMyCheck()) return false; setTimeout('__doPostBack(\'tb1\',\'\')', 0)" 
发布评论

评论列表(0)

  1. 暂无评论