简体   繁体   English

如何在不使用隐藏控件的情况下使用__DoPostBack定位代码隐藏方法

[英]how to target codebehind method using __DoPostBack without hidden control

I want to call a server side method using __DoPostBack, but i don't want a hidden ASP runat server control in my page. 我想使用__DoPostBack调用服务器端方法,但是我不希望页面中有隐藏的ASP Runat服务器控件。 Is it possible to call a server side method by its name and not by the name of the control that triggers it? 是否可以通过名称而不是触发它的控件名称来调用服务器端方法?

The problem is, I have an asp button on my aspx page with onclick="ExportButton_Click", and when the button is clicked, it calls the ExportButton_Click codebehind (server side) method. 问题是,我在aspx页面上有一个带onclick =“ ExportButton_Click”的asp按钮,单击该按钮时,它将调用Exportback_Click代码隐藏(服务器端)方法。 My problem is, I want to get rid of the asp button, because I am trying to create a button dynamically, which when clicked, will do the same thing. 我的问题是,我想摆脱asp按钮,因为我试图动态创建一个按钮,单击该按钮将执行相同的操作。 Right now, my dynamically created button is calling the doPostBack javascript function which targets the asp Button which triggers ExportButton_Click. 现在,我动态创建的按钮正在调用doPostBack javascript函数,该函数的目标是触发ExportButton_Click的asp按钮。 So... is it possible to call the ExportButton_Click codebehind method with __doPostBack, without having an asp button? 因此...可以在没有ASP按钮的情况下使用__doPostBack调用ExportButton_Click代码隐藏方法吗? Thanks in advance. 提前致谢。

Pass the event target argument in to the __DoPostBack('myEvent') method, like that. 像这样将事件目标参数传递给__DoPostBack('myEvent')方法。

Then in your code-behind Page_Load() , have somewhere code like this: 然后,在您的Page_Load()背后的代码中,放置以下代码:

if (Request.Form["__EVENTTARGET"] == "myEvent")
{
    //call your button click function, and pass the button to it (can pass null as the EventArgs)
    Button1_Click(Button1, null);
}

These two methods are your server-side friends: 这两种方法是您的服务器端朋友:

this.Page.ClientScript.GetPostBackEventReference
this.Page.ClientScript.GetPostBackClientHyperlink

They can be used to generate javascript to link to your server side events. 它们可用于生成JavaScript以链接到您的服务器端事件。

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

相关问题 如何使用三元运算符用aspx方法替换codebehind方法 - How to replace a codebehind method with an aspx method using ternary operator 使用不带代码隐藏的Azure AD - Using Azure AD without codebehind 如何在不使用TryFindResource的情况下从代码背后获取StaticResource的实际对象? - How to get the actual object of StaticResource from codebehind without using TryFindResource? 如何在不使用代码隐藏的情况下将XDocument对象绑定到Treeview? - How to bind a XDocument object to a Treeview without using codebehind? 导航到代码中没有OnNavigateTo方法的参数的页面 - Navigate to page with parameter without OnNavigateTo method in codebehind 如何处理在代码背后实例化的控件的事件 - How to handle events of a control instantiated in codebehind 如何调用__doPostBack方法?调用方法在哪里? - how does the __doPostBack method get called? Where is the calling method? 如何在不使用x:Name的情况下在自定义控件中使用公共方法 - How to use a public method in a custom control without using x:Name 如何将onclientclick添加到在代码背后动态创建的控件中? - How to add onclientclick to a control that is dynamically created in the codebehind? 不使用代码隐藏,从文本框中删除“无” - Remove 'None' from textbox without using codebehind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM