我如何从jQuery(Sharepoint 2010)调用服务器端事件? [英] How can I invoke a server-side event from jQuery (Sharepoint 2010)?

查看:143
本文介绍了我如何从jQuery(Sharepoint 2010)调用服务器端事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Sharepoint 2010应用程序中,我使用jQuery处理大多数客户端事件。但是,为了将数据保存到Sharepoint列表以及使用该数据生成PDF文件,我想用C#处理服务器端。



我尝试以这种方式调用C#事件:

0)在我的项目的* .ascx文件中创建了一个HTML按钮:

 < button type =buttonname =saveDataid =saveData>保存数据< / button> 



<1>添加了jQuery来响应被点击的按钮(在同一个* .ascx文件中):

  $(#saveData)。click(function(){
$('#hiddenSave')。触发器('valuechanged');
});

2)在服务器端C#中创建一个隐藏元素(在* .ascx.cs文件中):

  HiddenField hiddenSave = null; 

protected void Page_Load(object sender,EventArgs e)
{
base.OnPreRender(e);

hiddenSave = new HiddenField();
hiddenSave.ID =hiddenSave;
hiddenSave.ValueChanged + = new EventHandler(hiddenSave_ValueChanged);
this.Controls.Add(hiddenSave);


protected void hiddenSave_ValueChanged(object sender,EventArgs e)
{
GeneratePDF();
}

private void GeneratePDF()
{
; // bla
}

但我从来没有达到ValueChanged事件处理程序; $(#saveData)。click()触发,但不触发hiddenSave_ValueChanged()。

那么我需要对此进行调整,或者采用完全不同的方法?如何在jQuery中尽可能多地执行客户端操作,还可以在Sharepoint 2010应用程序中运行服务器端/ C#代码?



UPDATE

h2>

稍微详细一点,以及我尝试过的其他内容:我在* .ascx.cs文件中动态地(在C#中)在Sharepoint页面上创建一个按钮:

 按钮btnSave = null; 

protected void Page_Load(object sender,EventArgs e)
{
base.OnPreRender(e);

this.Controls.Add(new LiteralControl(< br />>));
btnSave = new Button();
btnSave.ID =btnSave;
btnSave.Text =保存数据;
btnSave.Click + = new EventHandler(btnSave_Click);
btnSave.Visible = false;
this.Controls.Add(btnSave);


protected void btnSave_Click(object sender,EventArgs e)
{
btnSave.Text =你点了我!
PostTravelData ptd = new PostTravelData();

$ / code>

我在客户端jQuery的正确时间将其设置为可见* .ascx文件:

  $('#btnSave')。show(); 

但是,单击该按钮不会触及btnSave_Click()事件 - 断点永远不会到达,按钮的文字也没有改变。为什么不呢?



即使我没有将按钮设置为不可见(注释掉btnSave.Visible = false行),也没有达到点击处理程序...是Page_Load()太迟了?是否有更早的页面事件可以使用?



我也尝试将它从Page_Load()移动到OnPreRender(),如下所示:

  protected void OnPreRender(EventArgs e)
{
this.Controls.Add(new LiteralControl(< br / >中));
btnSave = new Button();
btnSave.ID =btnSave;
btnSave.Text =保存数据;
btnSave.Click + = new EventHandler(btnSave_Click);
//btnSave.Visible = false;
this.Controls.Add(btnSave);

...(和OnRender()),但按钮不甚至显示......

然后,尝试一种不同的方式,我将动态创建服务器端代码注释掉,并尝试附加到HTML中创建的按钮(*。 ascx文件):

 < button type =buttonname =saveDataid =saveDatarunat =server onclick =saveData_Click>保存数据< / button> 

(通过添加runat服务器和onclick处理程序),然后添加这个 (* .ascx.cs)):

  protected void saveData_Click(object sender,EventArgs e)
{
PostTravelData ptd = new PostTravelData();
SaveToList(ptd);
GeneratePDF(ptd);
}

...但在Mudville仍然没有快乐 - 没有达到处理程序。



另一个尝试是:



在* .ascx中:

 < asp:Button runat =serverid =saveDataname =saveDataonclick =saveData_ClickText =Bla /> 

在代码隐藏中:

  saveData.Click + = saveData_Click; 

创建Bla按钮,但点击它不会到达saveData_Click处理程序。



我甚至调整了 here 就像这样:

  Button btnSave = null; 

。 。 。

protected override CreateChildControls()
{
btnSave = new Button();
btnSave.Width =新单元(150,UnitType.Pixel);
btnSave.Text =你能看见我吗?;
btnSave.Click + = new EventHandler(btnSave_Click);
Controls.Add(btnSave);
}

...但我仍未达到protected void btnSave_Click(object发件人,EventArgs e)处理程序,当我点击按钮。

当然,有一种方法可以获得服务器端按钮处理并操作它(具体来说,响应它的点击事件)...?!?

解决方案

首先,据我所知,没有这样的事件输入类型隐藏在ascx页面中。如果您在ascx代码中创建了使用runat服务器隐藏的输入类型,那么您会看到,当您尝试添加此事件时,它不可用。不过,您还可以模拟其他类似OnClick的事件以获得所需的结果。


In my Sharepoint 2010 app, I'm handling most events client-side with jQuery. However, for the saving of data to the Sharepoint list, and the generation of a PDF file with that data, I want to handle that server-side, with C#.

I tried to invoke a C# event this way:

0) Aded an HTML button in my project's *.ascx file:

<button type="button" name="saveData" id="saveData">Save Data</button>

1) Added jQuery to respond to that button being clicked (in the same *.ascx file):

$("#saveData").click(function () {
      $('#hiddenSave').trigger('valuechanged');
  });

2) Created a hidden element in the server-side C# (in the *.ascx.cs file):

HiddenField hiddenSave = null;

protected void Page_Load(object sender, EventArgs e)
{
    base.OnPreRender(e);

    hiddenSave = new HiddenField();
    hiddenSave.ID = "hiddenSave";
    hiddenSave.ValueChanged += new EventHandler(hiddenSave_ValueChanged);
    this.Controls.Add(hiddenSave);
}

protected void hiddenSave_ValueChanged(object sender, EventArgs e)
{
    GeneratePDF();
}

private void GeneratePDF()
{
    ;//bla
}

But I never reach the "ValueChanged" event handler; $("#saveData").click() fires, but not hiddenSave_ValueChanged().

So do I need a tweak to this, or a completely different approach? How can I do as much as possible client-side with jQuery, but also run server-side/C# code where necessary, in a Sharepoint 2010 app?

UPDATE

A little more detail, and about additional things I've tried: I'm creating a button on a Sharepoint page dynamically (in C#) in my *.ascx.cs file:

Button btnSave = null;

protected void Page_Load(object sender, EventArgs e)
{
    base.OnPreRender(e);    

    this.Controls.Add(new LiteralControl("<br />"));
    btnSave = new Button();
    btnSave.ID = "btnSave";
    btnSave.Text = "Save the Data"; 
    btnSave.Click += new EventHandler(btnSave_Click);
    btnSave.Visible = false;
    this.Controls.Add(btnSave);
}

protected void btnSave_Click(object sender, EventArgs e)
{
    btnSave.Text = "You clicked me!"; 
    PostTravelData ptd = new PostTravelData();
}

I set it visible at the right time in the client-side jQuery in the *.ascx file:

$('#btnSave').show();

However, clicking the button does not reach the btnSave_Click() event - the breakpoint there is never reached, nor is the button's text changed. Why not?

Even when I don't set the button invisible (comment out the "btnSave.Visible = false;" line), the click handler isn't reached...is Page_Load() too late? Is there an earlier page event I can use that would work?

I tried moving it from Page_Load() to OnPreRender(), too, like this:

protected void OnPreRender(EventArgs e)
{
    this.Controls.Add(new LiteralControl("<br />"));
    btnSave = new Button();
    btnSave.ID = "btnSave";
    btnSave.Text = "Save the Data";
    btnSave.Click += new EventHandler(btnSave_Click);
    //btnSave.Visible = false;
    this.Controls.Add(btnSave);
}

...(and OnRender()) but the button doesn't even display...

And, trying a different tack, I commented out the dynamic creation server-side code and tried to attach to a button created in the HTML (*.ascx file):

<button type="button" name="saveData" id="saveData" runat="server" onclick="saveData_Click">Save Data</button>

(by adding the "runat server" and the onclick handler), and then adding this "code-behind" (*.ascx.cs)):

protected void saveData_Click(object sender, EventArgs e)
{
    PostTravelData ptd = new PostTravelData();
    SaveToList(ptd);
    GeneratePDF(ptd);
}        

...but there was still no joy in Mudville -- the breakpoint in the handler is not reached.

Yet another attempt was:

In the *.ascx:

<asp:Button runat="server" id="saveData" name="saveData" onclick="saveData_Click" Text="Bla" />

In the code-behind:

saveData.Click += saveData_Click;

The "Bla" button is created, but clicking on it reaches not the breakpoint in the "saveData_Click" handler.

I even adapted some code from here like so:

Button btnSave = null;

. . .

protected override void CreateChildControls()
{
    btnSave = new Button();
    btnSave.Width = new Unit(150, UnitType.Pixel);
    btnSave.Text = "Can you see me?";
    btnSave.Click += new EventHandler(btnSave_Click);
    Controls.Add(btnSave);
}

...but I still do not reach the "protected void btnSave_Click(object sender, EventArgs e)" handler when I click the button.

Surely there's a way to get a handle on the button server-side and manipulate it (specifically, respond to its click event)...?!?

解决方案

First of all, as far as I know, there is no such event for input type hidden in a ascx page. If you create the input type hidden with runat server in the ascx code then you'll see that when you try to add this event it's not available. However there are other events like OnClick that you can simulate to get the desired result.

这篇关于我如何从jQuery(Sharepoint 2010)调用服务器端事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆