博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf通过控件名查找控件_查找使回发变得简单的控件
阅读量:2534 次
发布时间:2019-05-11

本文共 5077 字,大约阅读时间需要 16 分钟。

wpf通过控件名查找控件

介绍 (Introduction)

在开发Web应用程序时,单个页面可能包含许多区域,每个区域可能包含许多具有执行回发功能的控件。 很多时候,您可能需要根据导致回发发生的控件对ASP.NET回发执行一些操作。 例如,该操作可能是将焦点重新设置到导致回发的控件上,或者将其他控件加载到导致回发的控件上。

In this article, we will look into the JavaScript __doPostBack function and then as a little walkthrough we will write one small program to determine the control that causes the postback

在本文中,我们将研究JavaScript

Let us take a look at the __doPostBack function shown below:

让我们看一下下面显示的__doPostBack函数:

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;theForm.__EVENTARGUMENT.value = eventArgument;theForm.submit();}}

The __doPostBack function takes two arguments eventTarget, and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. One thng should be noted that the two hidden fields _EVENTTARGET and _EVENTARGUMENT are automatically declared. the value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from code behind using the forms/params collection.

__doPostBack函数采用两个参数eventTarget和eventArgument。

查找导致PostBack的控件: (Finding the control that caused the PostBack:)

您可以使用以下代码从表单参数集合中检索_EVENTTARGET隐藏变量的值,从而找到引起回发的控件的ID:
protected void Page_Load(object sender, EventArgs e)        {
string controlName = Request.Params.Get("__EVENTTARGET"); Response.Write("control caused the postback is: " + controlName); }

尝试使上面的代码起作用: (Try putting the above code to work:)

1个 (1)

在页面上添加三个DropDownList控件,并将所有三个控件的AutoPostBack属性设置为true。 保留所有控件的默认ID,即DropDownList1,DropDownList2,DropDownList3。

2 (2)

用一些伪数据填充每个dropdownlist控件。

3 (3)

在Page_Load事件中编写以下代码。
protected void Page_Load(object sender, EventArgs e)        {
string controlName = Request.Params.Get("__EVENTTARGET"); Response.Write("control caused the postback is: " + controlName); }

4 (4)

运行代码以验证输出程序将显示导致回发的controlName。 如果查看页面的源代码,则可以看到DropDownList的onchange事件调用__doPostBack函数。 控件
DropDownList1DropDownList1

忙于处理按钮和图像按钮的回发 (Wroking with Buttons and ImageButtons to Handle Postbacks)

If you see the code generated by Button (or ImageButton) <input type="submit" name="Button1" value="Do PostBack" id="Button1" /> the button control (or the ImageButton control) does not call the __doPostBack function, and hence the _EVENTTARGET will always be empty. However, you can findout the ID of the Button by looping through the form controls collection.

如果您看到由Button(或ImageButton)生成的代码<input type =“ submit” name =“ Button1” value =“ Do PostBack” id =“ Button1” />,则按钮控件(或ImageButton控件)不会调用__doPostBack函数,因此_EVENTTARGET始终为空。 但是,您可以通过遍历表单控件集合来找到Button的ID。

I have written one small function GetPostBackControl that returns control name as string datatype that caused the postback. All the statements inside the function are self explanatory. Comments have been included where-ever necessary.

我编写了一个小函数

试试看: (Try it out:)

1个 (1)

在页面上添加不同的控件,例如DropDownLists,CheckBoxes,Buttons。 对于DropDownLists或CheckBoxes,请将AutoPostBack属性设置为true。 我在页面中添加了三个DropDownLists,四个Button和三个CheckBox。 相应的.ASPX代码如下所示:
    
Item11
Item12
Item13
Item14
Item11
Item12
Item13
Item14
Item11
Item12
Item13
Item14
 
 
 
 
 

2 (2)

在代码隐藏页面中编写以下函数:
public string GetPostBackControl(Page page)        {
string controlName = page.Request.Params.Get("__EVENTTARGET"); if (controlName == string.Empty) {
//Means, some Button or ImageButton has been clicked. foreach (string controlItem in page.Request.Form) {
Control c = Page.FindControl(controlItem); if (c is Button) {
controlName = c.ID; break; } } } return controlName; }

3 (3)

To use the above function, write the following code in the Page_Load event:

若要使用上述功能,请在Page_Load事件中编写以下代码:

protected void Page_Load(object sender, EventArgs e)        {
string control = GetPostBackControl(this.Page); Response.Write("Control caused postback:" + control); }

4 (4)

现在,运行上面的代码,并检查导致回发的控件。

Hope this article helps to all the readers. Happy programming!

希望本文对所有读者有所帮助。 编程愉快!

翻译自:

wpf通过控件名查找控件

转载地址:http://ggqzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>