2011-05-28 78 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(); 
} 

這應該解決您的問題。讓我知道是否有任何關於答覆的內容不清楚或混淆。

相關問題