如何从.cs文件调用.aspx方法 [英] How to call method of .aspx from .cs file

查看:74
本文介绍了如何从.cs文件调用.aspx方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生您好,
我创建网页的原因是我在.aspx文件中使用了javascript.我需要执行以下操作.:

我有**"save" **按钮,但是在源代码中,我将Java脚本用于** save **按钮,在其中我声明了一个名为** OnClientClick ="javascript:validateTextTest()" **的函数,并在其中源代码的头我称为此函数validateTextTest().

以下是源代码中的保存按钮:

< asp:Button ID ="Save" runat ="server" onclick ="Save_Click" Text ="Save" OnClientClick ="javascript:validateTextTest()"
宽度="63px"/>

现在我需要的是我可以在**.cs文件**的保存按钮中调用函数** validateTextTest **()吗?因为有两到三个文本框,如果我在三个文本框中保留一个texbox,则不应将其插入DB.

因此,请告诉我如何在.cs文件中调用该函数.请为我提供帮助.
谢谢,
Pradeep

Hello sir,
I created web page in that i used javascript in .aspx file.I need followings to be done.:

I am having **"save"** button,but in the source code i used java script for **save** button,where i declared a function called **OnClientClick="javascript : validateTextTest()"** and in the head of source code i called this function validateTextTest().

Below is the save button in source code:

<asp:Button ID="Save" runat="server" onclick="Save_Click" Text="Save" OnClientClick="javascript : validateTextTest()"
Width="63px" />

Now i need is can i call a function **validateTextTest**() in save button of **.cs file**.Because am having two to three textboxs,if i leave one texbox out of three textbox it should not insert into DB.

So please tel me how to call the function in .cs file .Please to help me.
Thanks,
Pradeep

推荐答案

为button_click编写服务器端事件,在此事件中,将javascript称为

Write a server side event for the button_click, in this event call javascript as

string script = "<script language="'javascript'">validateTextTest();</script>";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), script, false);



谢谢
SP



Thanks
SP




如果您需要更复杂的回发行为,则可以包含一个包含值的隐藏项,该值包含所需的回发行为.

Hi,

If you need a more complicated post back behaviour you can include a hidden which contains a value which includes your desired post back behaviour.

<asp:hidden id="hdnAction" runat="Server" xmlns:asp="#unknown" />



更新您的javascript方法以分配操作并提交表单:



Update your javascript method to assign an action and submit the form:

function validateTextTest() {

   // validation code

   document.getElementById('hdnAction').value = 'MyAction';
   document.forms[0].submit();

}



然后在您的页面中加载:



Then in your page load:

protected void Page_Load(Object sender, EventArgs e)
{

  if(IsPostBack)
  {
    switch(hdnAction.Value)
    {
      case "MyAction":
        //do action code
        break;
    }
  }

}



然后,从按钮中删除服务器事件绑定:



You then remove the server event binding from your button:

<asp:button id="Save" runat="server" text="Save" onclientclick="javascript : validateTextTest()" xmlns:asp="#unknown">
Width="63px" /></asp:button>


这篇关于如何从.cs文件调用.aspx方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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