我在将值从我的用户控件传递到 aspx 页面时遇到了问题。在用户控件中有 2 个文本框,带有一个用于搜索客户的按钮和一个用于显示结果的 GridView 。单击选择按钮时,我想将值传递给 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>
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 放在了更新面板中:
比起我在用户控制 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();
}
.....................................................
任务完成!
最佳答案
首先要记住的是,您正在使用部分帖子更新页面,因此主页上文本框的服务器端显示不会更新,这可能是您问题的一部分。假设这不是唯一的问题,我过去这样做的最佳方法是使用控件可以引发的委托(delegate)事件,并且页面在控件上订阅。
首先,在您的主页上,确保您在更新面板内有您想要更新的文本框,该面板也标有条件更新:
<asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtTitolo" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
在主页的代码隐藏中(在命名空间级别,而不是在页面级别),为事件添加委托(delegate):
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);
}
}
接下来,您需要将处理程序添加回页面以响应(订阅)事件。在主页面加载中,添加事件:
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();
}
这应该可以解决您的问题。如果回复有任何不清楚或令人困惑的地方,请告诉我。
关于c# - 将值从用户控件传递到主页 aspx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161170/