2011-05-28 98 views
2

我有一个问题需要将我的用户控件的值传递给aspx页面。在用户控件中,有2个文本框带有一个按钮来搜索客户和一个gridview来显示结果。当选择按钮被点击时,我想将值传递给page.aspx。有一个按钮,单击时,出现一个modalpopupextender。氏是代码:将用户控件的值传递到主页面aspx

<%@ 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天我在这个封锁。

在此先感谢。

-----编辑---- 感谢布赖恩, 我已经做了一些改动,以您的文章,但现在它终于工作。

至于你说我把ThextoBox在联合国的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(); 
       } 
..................................................... 

任务完成!

+0

为什么你为你的GridView做EnableViewState =“False”? – ibram 2011-05-28 11:36:45

+0

否则过滤器(txtNomeReferente,txtNomeCommittente和btnSearch)不起作用。 – AlessandroG 2011-05-28 12:16:15

回答

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(); 
} 

这应该解决您的问题。让我知道是否有任何关于答复的内容不清楚或混淆。

相关问题