How do I set the target frame for form submission in ASP.NET?

别等时光非礼了梦想. 提交于 2019-12-12 12:40:39

问题


I have an asp.net page in an iframe where all links target _blank

<base target="_blank" />

But I want the form on it to submit to _self (i.e. the iframe where the page is located) when the one button is clicked. The form is an <asp:Panel> with an <asp:Button> control for submitting it.

Where can I set the target for this form? Since there isn't a <form> tag or an <input> tag in the file (ASP.NET makes them when it renders the page), I don't know how to change the target to override my <base> tag.


回答1:


I'm not sure if I understood you right, but you can set the target in the form tag, like this:

<form method=post action="Page.aspx" target="_self">



回答2:


I just found this post on the asp.net forums by mokeefe that changes the target by javascript.

I just put it in my page and tried it. I had to make these modifications:

1. I'm using asp tags only, so I have <asp:Button> not <input type="button"> and my onclick has to be the server-side method I'm calling on submission. Therefore I put this new javascript in OnClientClick:

<asp:Button ID="cmdEmailSearch" runat="server" Text="Search"
            OnClick="cmdEmailSearch_Click"
            OnClientClick="javascript:pageSubmit()"/>

2. I removed the myForm.submit() since ASP.NET renders the page putting the WebForm_DoPostBackWithOptions() javascript in the button's onclick right after it puts my pageSubmit()

<script type="text/javascript">

    function pageSubmit(){
        // where form1 is the Parent Form Id
        var myForm = document.getElementById('form1');
        myForm.target = '_self';
    }// end function
</script>


来源:https://stackoverflow.com/questions/2274566/how-do-i-set-the-target-frame-for-form-submission-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!