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

I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged

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)" 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332450a4622944.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信