如何将值从WebUserControl.ascx页转换为.aspx页 [英] How to get the value from a WebUserControl.ascx page into .aspx page

查看:61
本文介绍了如何将值从WebUserControl.ascx页转换为.aspx页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在WebUserControl.ascx页面中有一个组合.我已将其用于2个不同的.aspx页面.
在我的一个.aspx页面中,我有一个标签.如果我从组合中选择一项,则标签文本将根据组合所选值进行更改.
[组合位于WebUserControl中,标签位于.aspx页中.]

在此先感谢您.

Suppose I have a combo in WebUserControl.ascx page. I have used it onto 2 different .aspx pages.
In my one .aspx page I have a Label. If I select one item from combo then the Label text will be change according to combo selected value.
[Combo is in WebUserControl and Label is in .aspx page.]

Thanks in advance.

推荐答案

我建​​议您在用户控件中公开一个属性.此属性将保存组合框的选定值.将财产公开,以便外部可以访问.

现在,在您的ASPX页面上,在需要时使用usercontrolname.exposedProerty.因此,以类型安全的形式访问ASPX中usercontrol的值.
I would suggest you to expose a property in your usercontrol. This property would hold the selected value of combobox. Make the property public such that it is accessible outside.

Now, in your ASPX page, use the usercontrolname.exposedProerty whenever needed. Thus, accessing the value of usercontrol in ASPX with type-safe form.


这是一个很好的问题.如果我重新措词,它将类似于以下内容.

如何将某个类的某些内部信息传递给其父类.
假设Class A 包含Class b并且在Class B 中发生了什么,它将如何将信息传递给Class A.

最好通过事件来实现. B类会暴露一些事件,而A类会消耗该事件.
例如,当一个组合框中选择,它提升我们捉的.aspx页面上的<6>事件.

同样,您必须在UserControl类中创建事件OnComboSelected .
您的aspx页面将捕获此事件,事情就完成了.

在您遇到的问题中.
在webuserControl类中
这是DropDown

This is a good Question. If I re-phrase it will be somehow like below.

How to pass some internal information of a Class to its parent Class.
Suppose Class A contains Class b and something occurs in Class B , how would it pass information to Class A.

This is best achieved with Events. Class B would expose some event and Class A consumes that Event.
For instance when a combo Box is Selected, it raise a OnselectedIndexChanged event which we catch on .aspx page.

In same manner you have to make a event OnComboSelected in the UserControl Class.
Your aspx page will catch this event and the things is done.

In your problem context.
In webuserControl Class
This is the DropDown

<asp:DropDownList ID="DropDownList1" runat="server"

    onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>



用户控件背后的代码



Usercontrol Code-behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class ComboEventArgs : EventArgs
{
    public ListItem Item;
    public ComboEventArgs(ListItem _L)
    {
        Item = _L;
    }
}
public partial class WebUserControl : System.Web.UI.UserControl
{
    public delegate void Comboselected(object sender, ComboEventArgs e);
    protected Comboselected Comboselected1;
    public event Comboselected EComboSelected
    {
        add
        {
            Comboselected1 += value;
        }
        remove
        {
            Comboselected1 -= value; 
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Comboselected1 != null)
        {
            Comboselected1(this, new ComboEventArgs(DropDownList1.SelectedItem));
        }
    }
}



.aspx页面



The .aspx page

<uc1:WebUserControl ID="WebUserControl1" runat="server" OnEComboSelected="ComboSelected" />



.aspx页面代码隐藏



.aspx page code-behind

protected void ComboSelected(object sender, ComboEventArgs e)
   {
       Response.Write(e.Item);
   }




即使您不将DropDownList的AUTOPOSTBACK属性设置为True.当其他控件进行回发时,事件仍将引发,并且页面将显示Selected值.




Even If you dont make AUTOPOSTBACK property of the DropDownList to True. the Event will still raise and your page would show the Selected value when the postback occurs by some other control.


这篇关于如何将值从WebUserControl.ascx页转换为.aspx页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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