如何在.ASHX文件上创建按钮单击事件

问题描述:



我正在处理一项要求,我们必须在包含按钮的.ashx文件上创建UI.
我能够添加按钮,但不会触发onClick.以下是.Ashx文件的代码.请帮助!

Hi,

I am working on a requirement where we have to create a UI on the .ashx file which includes buttons.
I was able to add the button but the onClick is not getting triggered.Below is the code of the .Ashx file.Please help!

<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ WebHandler Language="C#" Class="SPHandler" %>

using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls ;
using Microsoft.SharePoint;
using System.Drawing;

public partial class SPHandler:  IHttpHandler
    {
        //private Button btnaccept;

        public void ProcessRequest(HttpContext context)
        {

            //Panel p = new Panel();
            //Label lblmessage = new Label();
            //lblmessage.Text = "This is from Handler";
            // btnaccept = new Button();
            //btnaccept.Text = "Accept";
            //btnaccept.Click += new System.EventHandler(this.btnaccept_Click);
            //p.Controls.Add(lblmessage);
            //p.Controls.Add(btnaccept);
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter writer = new HtmlTextWriter(sw);
            //p.RenderControl(writer);
            //context.Response.Write(sw.ToString());


            ManageForm(context);

        }

        //public void btnaccept_Click(object sender, EventArgs e)
        //{
        ////HttpContext.Current.("This from the Accept button click event");
        //}


        public void ManageForm(HttpContext context)
        {
             context.Response.Write("<html><body><form Method='Link'Action='http://yahoo.com'>");
            context.Response.Write( "<input type=button value='Accept' >");
            
            context.Response.Write(""); 
            context.Response.Write("</form></body></html>");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
}

我可以在您当前的代码中看到一些问题,如下所示...

您尚未在按钮内编写任何代码,例如onClick="btnaccept_Click".

并且以下代码仅对具有runat="server"
的一个服务器端控件执行
I can see some problems in your current code like below...

You have not written any code inside the button like onClick="btnaccept_Click".

And the below code will be executed only for one server side control with an runat="server"
public void btnaccept_Click(object sender, EventArgs e)
{
    HttpContext.Current.("This from the Accept button click event");
}


,而您的按钮是一个客户端html控件,例如...


, whereas your button is one client-side html control like...

<input type="button" value="Accept"></input>



这就是为什么您的活动没有触发的原因.

可能有两种解决方案
-------------------------------
1.尝试将一个onclick事件处理程序代码添加到html控件中,并将该事件写为javaScript.
2.添加一个服务器端按钮控件而不是html控件,并为此定义事件.

祝你好运...
谢谢...



That''s why your event is not firing.

Two solutions can be possible
-------------------------------
1. Try adding one onclick event handler code to the html control and write that event is javaScript.
2. Add one server side button control instead of html control and define event for that.

Good luck...
Thanks...