简体   繁体   English

ASP Image按钮和按钮未触发事件,而是在URL上发布了参数

[英]ASP Image button and buttons are not firing the event instead it posts the parameter on URL

I wanna create a button/image button to pop up a panel. 我想创建一个按钮/图像按钮来弹出一个面板。 I have tried both image button and button and also in button both onserverclick and onclick. 我尝试过图像按钮和按钮,也都在按钮上onserverclick和onclick。 Anyways it is not hitting the event and it posts the parameters(randomly) in the URL like "ctl00%24MainContent%24btnNewDoc=Add+New". 无论如何,它都不会触发事件,它会(随机)在URL中发布参数,例如“ ctl00%24MainContent%24btnNewDoc = Add + New”。

 <asp:Button id="btnNewDoc" onserverclick="btnNewDoc_Click" Text="Add New" runat="server"/> 

 protected void btnNewDoc_Click(object sender, EventArgs e)
        {
            PnlDoc.Visible = true;
        }

Its just a panel and it should not pass the parameter for me. 它只是一个面板,不应为我传递参数。 And don't know the reason for this parameter passing. 而且不知道此参数传递的原因。 For this form alone, this is happening. 仅对于这种形式,就是这种情况。 Any idea on this. 任何想法。

Suppose you drag a button into your view. 假设您将一个按钮拖动到视图中。 When you click the properties. 当您单击属性时。 There is OnClick event. 有OnClick事件。 Just double click it and The code generated will be 只需双击它,生成的代码将是

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

So in your case it should be OnClick instead of onserverClick which you are using. 因此,在您的情况下,它应该是OnClick而不是您使用的onserverClick。

(Just to provide an answer based on comments above to help future readers...) (只是根据以上评论提供答案,以帮助将来的读者...)

In comments on the question you specify: 在您对问题的评论中指定:

i had added one more form tag 我又添加了一个表单标签

and: 和:

<form class="form-horizontal">

This is what's causing the confusion, for a couple of reasons: 造成这种混乱的原因有两个:

  1. The default form method, unless otherwise specified, is GET. 除非另有说明,否则默认的表单方法是GET。 This is why the key/value pair for the button you're clicking is being added to the URL. 这就是将您所单击按钮的键/值对添加到URL的原因。 And the default action is the current page, so it's just reloading the page with that URL value. 而且默认操作是当前页面,因此它只是使用该URL值重新加载页面。
  2. Nesting a form within a form is effectively invalid HTML, so the actual behavior of what form submits what data is likely undefined and potentially browser-specific. form内嵌套form实际上是无效的HTML,因此,哪种表单提交哪些数据的实际行为可能是未定义的,并且可能是特定于浏览器的。 form elements need to be separate in HTML. form元素在HTML中需要分开。
  3. Adding a form element at all in WebForms probably isn't going to work well with the framework. 在WebForms中根本不添加form元素可能无法很好地与框架配合使用。 Unless things have changed since I last used WebForms, generally the framework wraps the entire page in a single form with runat="server" . 除非自从我上次使用WebForms以来情况没有改变,否则通常框架会使用runat="server" 将整个页面包装为单一form This form posts back to the page with all of the form information on the page every time, including a variety of hidden form elements used by the framework to track control state. 该表单每次都将页面上的所有表单信息回发到页面,包括框架用来跟踪控件状态的各种hidden表单元素。 Creating your own form posts/actions would undermine that and cause the framework to behave in unexpected ways. 创建自己的表单发布/操作将破坏这种情况,并导致框架以意外的方式运行。

Basically you should be able to simply remove your custom form and allow the button to use the framework's form at the page level. 基本上,您应该能够简单地删除自定义form并允许按钮在页面级别使用框架的form This should cause a standard post-back in the framework and allow the framework to use all of the associated information to map the action to the server-side click handling event. 这应该在框架中引起标准的回发,并允许框架使用所有相关信息将操作映射到服务器端单击处理事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM