我有一個問題需要將我的用戶控件的值傳遞給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>
Committente :
<asp:TextBox ID="txtNomeCommittente" runat="server" AutoPostBack="true"></asp:TextBox>
<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();
}
.....................................................
任務完成!
爲什麼你爲你的GridView做EnableViewState =「False」? – ibram 2011-05-28 11:36:45
否則過濾器(txtNomeReferente,txtNomeCommittente和btnSearch)不起作用。 – AlessandroG 2011-05-28 12:16:15