C# 如何动态更改 MasterPage 上 aspnetForm 的 Action 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Action attribute of the aspnetForm on MasterPage dynamically
提问by Danish Khan
We are trying to modify the Action attribute of the main ASP.NET form in the master page dynamically.
我们正在尝试动态修改母版页中主 ASP.NET 表单的 Action 属性。
The page has a radio button, user selects one of the options and clicks on submit, on postback, based on the selection the Form's action attribute should be set and the form should be submitted again automatically.
该页面有一个单选按钮,用户选择其中一个选项并单击提交,在回发时,基于应设置表单的操作属性的选择,表单应自动再次提交。
We were trying to use JavaScript for the same.
我们试图使用 JavaScript 来做同样的事情。
document.forms[0].action = "option1.aspx";
document.forms[0].submit();
But this doesn't seem to be working, there is no impact on the action attribute.
但这似乎不起作用,对 action 属性没有影响。
if we don't use a master page, this can be achieved easily by using
如果我们不使用母版页,这可以通过使用轻松实现
this.Form.Action = "option1.aspx";
ClientScript.RegisterStartupScript(this.GetType(),"test1",
"document.form[0].submit();",true);
Sadly, we cant remove the master page .. any pointers on how can this be achieved.. ?
可悲的是,我们无法删除母版页.. 关于如何实现这一点的任何指示..?
采纳答案by REA_ANDREW
This is something which I have read they wish they had not done. The Form tag gets its action attribute hardcoded. You have to use a Control Adapter in order to control its construction at runtime. I use it especially for URL Rewriting, when I require the postback URL to be the rewritten one I have created. Scott Gu made the code for it and you can find it here:
这是我读过的他们希望他们没有做过的事情。Form 标签获取其硬编码的 action 属性。您必须使用控制适配器才能在运行时控制其构造。我特别将它用于 URL 重写,当我需要回发 URL 是我创建的重写 URL 时。Scott Gu 为它制作了代码,你可以在这里找到它:
http://www.scottgu.com/blogposts/urlrewrite/UrlRewrite_HttpModule1.zip
http://www.scottgu.com/blogposts/urlrewrite/UrlRewrite_HttpModule1.zip
And the address for the article:
以及文章地址:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
回答by codemonkeh
Does the form need to pass the values to the resulting page? If not, why don't you just use Response.Redirect to the correct page? For example, presuming you are using a RadioButtonList called lstOptions:
表单是否需要将值传递给结果页面?如果没有,为什么不直接使用 Response.Redirect 到正确的页面?例如,假设您正在使用名为lstOptions的 RadioButtonList :
protected void btnSubmit_Click(object sender, EventArgs ags) {
switch (lstOptions.SelectedValue) {
case "option1":
Response.Redirect("~/option1.apsx");
break;
//etc
}
}
If you must pass the values why are you even triggering a post back at all? It sounds like you could accomplish what you want by just using javascript. For example presuming your form is named form1and your radio buttons have a name of options:
如果你必须传递这些值,你为什么还要触发回帖?听起来你可以通过使用 javascript 来完成你想要的。例如,假设您的表单名为form1并且您的单选按钮的名称为options:
<input type="submit" value="Submit" onclick="javascript:submitForm()" />
<script type="text/javascript">
function submitForm() {
for (var i=0; i < document.form1.options.length; i++) {
if (document.form1.options[i].checked) {
document.forms[0].action = "option" + i + ".aspx";
document.forms[0].submit();
}
}
}
</script>