将值从用户控件传递到主页面aspx

时间:2011-05-28 10:50:40

标签: c# asp.net ajaxcontroltoolkit

将问题从我的用户控件传递到aspx页面时遇到了问题。在用户控件中,有2个文本框,其中包含用于搜索客户的按钮和用于显示结果的网格视图。单击选择按钮时,我想将值传递给page.aspx。有一个按钮,当点击时,会出现一个modalpopupextender。这是代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="searchCommittente.ascx.cs" Inherits="Assistenze_ControlliCustom_searchCommittente" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


<asp:TextBox runat="server" ID="lblNomeCommittente"/>
<asp:Button runat="server" ID="btnShow" Text="Cerca Committente" />
<asp:ModalPopupExtender runat="server" Y="155" ID="mpeCercaCommittente" TargetControlID="btnShow" OkControlID="btnSearch" 
    PopupControlID="pnlPopupContainer" CancelControlID="spnClose" BehaviorID="mpeCercaCommittente"/>
<asp:Panel runat="server" ID="pnlPopupContainer" CssClass="pnlPopupContainer">
 <span id="spnClose"></span>
    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
         <ContentTemplate>
         <h2>Find Customer</h2>
         <%=DateTime.Now%>
            <asp:Panel runat="server" >
                Referente :
                <asp:TextBox ID="txtNomeReferente" runat="server" AutoPostBack="true"></asp:TextBox>
                &nbsp;Committente :
                <asp:TextBox ID="txtNomeCommittente" runat="server" AutoPostBack="true"></asp:TextBox>
                &nbsp;
                <asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click"  
                     CausesValidation="false"  UseSubmitBehavior="false" />

             </asp:Panel>
             <p />
            <asp:GridView ID="gvCommittenti" runat="server" AutoGenerateColumns="False"  
                 AllowPaging="true"  EnableViewState="False" 
                 DataKeyNames="CustomerID" 
                 DataSourceID="EntityDataSourceCommittenti" 
                 OnSelectedIndexChanged="gvCommittenti_SelectedIndexChanged">

                 <Columns>
                    <asp:CommandField ShowSelectButton="True"  />
                    <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" SortExpression="CustomerID"/>
                    <asp:BoundField DataField="CompanyName" HeaderText="Company Name" SortExpression="CompanyName" />
                    <asp:BoundField DataField="ContactName" HeaderText="Contact Name" SortExpression="ContactName" />
                    <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                 </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>

</asp:Panel>
<asp:EntityDataSource ID="EntityDataSourceCommittenti" runat="server" OrderBy="" 
    ConnectionString="name=NorthwindEntitiesCommittenti"
    DefaultContainerName="NorthwindEntitiesCommittenti" EntityTypeFilter=""
    EntitySetName="Customers" EnableFlattening="false" 
    Select="it.[CustomerID], it.[CompanyName], it.[ContactName], it.[Phone]"  Where="it.[CompanyName] like @CompanyName"
    AutoGenerateOrderByClause="True">
    <WhereParameters>
        <asp:FormParameter FormField="txtNomeCommittente" Name="CompanyName" DefaultValue="%" Type="String" />
    </WhereParameters>
    <OrderByParameters>
        <asp:Parameter DefaultValue="it.[CustomerID]" Name="prmCustomerID" 
            Type="String" />
    </OrderByParameters>

</asp:EntityDataSource>

我尝试过这个事件,但没有任何反应:

 protected void gvCommittenti_SelectedIndexChanged(object sender, GridViewSelectEventArgse)
    {
        ((TextBox)Page.FindControl("ctl00$Body$txtTitolo")).Text = (string)gvCustomers.DataKeys[e.NewSelectedIndex][0];
                    mpeCercaCommittente.Hide();
    }

其中txtTitolo是主aspx页面上的文本框。

有人可以帮助我3天我被阻止了。

提前致谢。

----- ---- EDIT 谢谢Brian, 我对你的帖子做了一些修改,但现在它终于有效了。

正如你所说,我把ThextoBox放在un updatepanel中:

                         

比我在用户控制中创建了Delgate(我知道但是如果我按照你的意思写了,Visual Studio 2010就冻结了):

public partial class Assistenze_ControlliCustom_searchCommittente : System.Web.UI.UserControl
    {
        public delegate void CommittenteSelectedHendler(string text);

        public event  CommittenteSelectedHendler custom;
.........................

现在,当我点击gridview的选择按钮时,此功能开始:

protected void gvCommittenti_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
    {

         if (custom != null)
        {
            string eventText = (string)gvCommittenti.DataKeys[e.NewSelectedIndex][0];
            custom(eventText);
        }
        mpeCercaCommittente.Hide();
    }

通过这种方式,主页面使用已注册的事件更新文本框 触发程序setTextBox:

    public partial class Assistenze_AssistenzeIngresso : System.Web.UI.Page
        {

            protected void Page_Load(object sender, EventArgs e)
            {
                ucCommittenteSearch.custom += new Assistenze_ControlliCustom_searchCommittente.CommittenteSelectedHendler(setTextBox);
                chkFattura.Attributes.Add("onclick", "return cmode_check(this)");
                Master.IdBody = "assistenze";
            }
            private void setTextBox(string text)
            {
                //set the text
                    txtCommittenteViewName.Text = text;
                    //update the page to reflect the change:
                    UpdatePanel1.Update();
                }
.....................................................

任务完成!

1 个答案:

答案 0 :(得分:1)

首先要记住的是,您正在使用部分帖子更新页面,因此文本框的服务器端显示不会在主页面上更新,这可能是您问题的一部分。假设这不是唯一的问题,我过去做过的最好的方法是使用控件可以引发的委托事件和页面在控件上订阅。

首先,在您的主页上,确保您在更新面板内有更新的文本框,该面板也标有条件更新:

<asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:TextBox ID="txtTitolo" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>

在主页面上的代码隐藏中(在命名空间级别,不在页面级别),添加事件的委托:

namespace WebUserControlTalksToPage
{
    //create the delegate to handle user-control updating page controls
    public delegate void setPageText(string text);

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // we'll add here in a second.
        }
    }
}

接下来,进入要与页面对话的用户控件(在您的情况下是带有gridview的用户控件)并添加要引发的事件:

public partial class SomeUserControl : System.Web.UI.UserControl
{
    //create a public event on the delegate type defined in the namespace:
    public event setPageText reporter;

    public void Page_Load(object sender, EventArgs e)
    {
        //....
    }
}

接下来,将记者事件添加到您要引发它的事件中(在您的情况下,gvCommittenti_SelectedIndexChanged事件:

protected void gvCommittenti_SelectedIndexChanged(object sender, GridViewSelectEventArgse)
{
    //NOTE: you can add this in any event where you want 
    //to raise the event to the subscribers.
    if (reporter != null)
    {
        //note: I separated this because I'm not testing your string.  You will need
        //      to make sure the string you are sending is correct:
        string eventText = (string)gvCustomers.DataKeys[e.NewSelectedIndex][0];

        //this will raise the event to all subscribers:
        reporter(eventText);
    }
}

接下来,您需要在页面上添加处理程序以响应(订阅)该事件。在主page_load中,添加事件:

protected void Page_Load(object sender, EventArgs e)
{
    //'reporter' is the name of the event on the control
    //setPageText is the name of the delegate event
    //setTextBox is the event we will write to handle the raised event from the user control
    //SomeUserControl1 is a variable name, replace with your UC name as defined in your page
    this.SomeUserControl1.reporter += new setPageText(setTextBox);
}

最后,您需要处理主页面中用户控件引发的事件。由于用户控件正在进行部分页面回发,因此您需要确保更新包含我在响应开始时提到的文本的面板:

 private void setTextBox(string text)
 {
    //set the text
    this.txtTitolo.Text = text;
    //update the page to reflect the change:
    upd1.Update();
 }

这应该可以解决你的问题。如果有任何关于回应的内容不清楚或令人困惑,请告诉我。