开发者

Passing value from a user control to the main page aspx

I've a problem to pass a value from my user control to the aspx page. In the user control there is 2 textbox with a button to searh a customer and a gridview to show the results. When the select button is clicked I want to pass the value to the page.aspx. There is a button that when clicked, a modalpopupextender appears. Thi is the code:

<%@ 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>

I've tried with this event, but nothing happens:

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

Where txtTitolo is a textbox on the main aspx page.

Someone can help me that are 3 days I'm blocked on this.

Thanks in advance.

-----EDIT---- Thanks Brian, I've made some changes to your post, but now it's finally working.

As you said I put the ThextoBox in un updatepanel:

Than I created inside the user Control the Delgate (I d'ont know but if I done as you wrote me, Visual studio 2010 went frozen):

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

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

Now when I click on the select button of the gridview, this function starts:

protected void gvCommittenti_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
    {

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

In this way the main page update the textbox using the registered event that fires the procedure 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);
        开发者_如何学JAVA        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();
                }
.....................................................

Mission completed!


First thing to remember is that you are updating the page using a partial post, so the server-side display of the textbox is not updated on the main page, which could be part of your problem. Assuming that is not the only problem, the best way I've done this in the past is to use delegated events that the control can raise and the page subscribes to on the control.

To start, on your main page, make sure that you have the textbox you want updated inside of an update panel that is also marked with conditional update:

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

in the code-behind on the main page (at namespace level, NOT at page level), add the delegate for the event:

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.
        }
    }
}

Next, go into the user control that you want to talk to the page (in your case the one with the gridview) and add the event to be raised:

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)
    {
        //....
    }
}

Next, add the reporter event to the event you want to raise it (in your case, the gvCommittenti_SelectedIndexChanged event:

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

Next, you need to add the handler back on the page to respond (subscribe) to the event. In the main page_load, add the event:

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

Finally, you need to handle the event that is raised by the user control in the main page. Since the user control is doing a partial-page postback, you'll want to make sure to update the panel that contains the text as mentioned at the start of my response:

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

This should solve your problems. Let me know if anything about the response is unclear or confusing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜