用户控件在aspx页面中回发吗? [英] User control Postback in aspx page?

查看:55
本文介绍了用户控件在aspx页面中回发吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在用户控制中有一个按钮控件,我在.aspx页面中使用了按钮控件,并且我想在.aspx页面中进行按钮控件的回发?如何做到这一点可以帮助我..?尽快 ...

提前致谢

Hi,,

I have one button control in user control ,i have taken button control in .aspx page and i want to do postback for button control in .aspx page?how to do this can any one help me..?ASAP...

Thanks In advance

推荐答案

在usercontrol上有一个Submit动作时,整个页面将被提交,并且还将再次构造"aspx".
简单来说,您可以在主aspx页和usercontrol ascx页的page_load方法上设置一个断点.看到按钮单击后,将为该页面调用所有页面加载.

在这里查看页面生命周期的工作原理:
ASP的完整生命周期.网页和控件 [ ASP.NET应用程序和页面生命周期 [ ^ ]
MSDN:ASP.NET页面生命周期概述 [
When there is a submit action on usercontrol, whole page will be submitted and it will also construct the ''aspx'' again.
In simple terms on what you can easily see, put a break point on the page_load methods of main aspx page and usercontrol ascx page. See on button click all the page loads are called for that page.

Look here and see how page life cycle works:
Complete Lifecycle of an ASP.Net page and controls[^]
ASP.NET Application and Page Life Cycle[^]
MSDN: ASP.NET Page Life Cycle Overview[^]


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Dalegate_WebUserControl" %>

<asp:Button ID="btnTest" runat="server" Text="I am Inside User Control" OnClick="btnTest_Click" />

Fig – (1) WebUserControl.ascx

            On WebUserControl.ascx.cs I have written simple delegate and event handler as shown below,

public partial class Dalegate_WebUserControl : System.Web.UI.UserControl
{

    // Delegate declaration
    public delegate void OnButtonClick(string strValue);

    // Event declaration
    public event OnButtonClick btnHandler;

    // Page load
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
           // Check if event is null
           if (btnHandler != null)
               btnHandler(string.Empty);

           // Write some text to output
           Response.Write("User Control’s Button Click <BR/>");
    }
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Dalegate_Default" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1″ %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1″ runat="server">

    <div>

        <asp:Label ID="lblText" Text="I am On Main Page : "

                   runat="server"></asp:Label>

        <asp:DropDownList ID="ddlTemp" runat="server">

            <asp:ListItem>Chirag</asp:ListItem>

            <asp:ListItem>Dipak</asp:ListItem>

            <asp:ListItem>Shailesh</asp:ListItem>

        </asp:DropDownList>

        <br />

        <br />

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



    </div>

    </form>

</body>

</html>


图–(3)Default.aspx

Default.aspx具有一个下拉列表和一个用户控件,如上面的代码所示.让我们看一下cs文件,


Fig – (3) Default.aspx

Default.aspx has one drop down list and a user control as shown in above code. Lets look at cs file,

public partial class Dalegate_Default : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
      {
             // Declare and Define Event of User Control. When User Clicks on button
             (which is inside UserControl)
             // below event is raised as I have called raised that event on Button Click
             WebUserControl1.btnHandler += new
             Dalegate_WebUserControl.OnButtonClick(WebUserControl1_btnHandler);

      }

      void WebUserControl1_btnHandler(string strValue)
      {
             Response.Write("Main Page Event<BR/>Selected Value: " +
                                     ddlTemp.SelectedItem.Text + "<BR/>");
      }
}


在应用程序代码中添加新类

add new class in appcode

public class ClsUserControl : UserControl
{
    public delegate void EventHandler(object sender, EventArgs e);
    public event EventHandler UserBtnClick;

    public ClsUserControl()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void UserBtn(object sender, EventArgs e)
    {
        if (UserBtnClick != null) UserBtnClick(sender, e);
        FunUserBtnClick(sender);
    }
    public virtual void FunUserBtnClick(object sender)
    {
        //do nothing
    }
}



使用按钮添加用户控件



add a user control with button

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />



在控件cs中添加此行



in control cs add this line

public partial class WebUserControl : ClsUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserBtn(sender, e);
    }
}




在aspx页面中添加这样的用户控件




in your aspx page add user control like this

<uc1:webusercontrol id="WebUserControl1" runat="server" onuserbtnclick="UserControl_Click" xmlns:uc1="#unknown" />



在您的aspx页面中,cs为此事件添加代码



in your this aspx page cs add your code for this event

public void UserControl_Click(object sender, EventArgs e)
    {
        //Response.Write("comein");
        saveData();
    }


这篇关于用户控件在aspx页面中回发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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