将值从用户控件传递到主页aspx
本文关键字:主页 aspx 控件 用户 | 更新日期: 2023-09-27 17:58:03
我在将值从用户控件传递到aspx页面时遇到问题。在用户控件中,有两个文本框,其中一个按钮用于搜索客户,另一个网格视图用于显示结果。单击选择按钮时,我想将值传递到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天。
提前谢谢。
-----编辑----谢谢Brian,我对你的帖子做了一些修改,但现在终于奏效了。
正如你所说,我把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;
.........................
现在,当我点击网格视图的选择按钮时,该功能启动:
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();
}
.....................................................
任务完成!
首先要记住的是,您正在使用部分post更新页面,因此在主页面上不会更新文本框的服务器端显示,这可能是问题的一部分。假设这不是唯一的问题,我过去做过的最好的方法是使用控件可以引发的委派事件,并且页面在控件上订阅这些委派事件。
首先,在主页上,确保您在更新面板中有想要更新的文本框,该面板也标有条件更新:
<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)
{
//....
}
}
接下来,将报告程序事件添加到要引发它的事件中(在您的案例中,gvCommittent_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);
}
}
接下来,您需要将处理程序添加回页面以响应(订阅)事件。在主页面_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();
}
这应该能解决你的问题。如果有关回应的任何内容不清楚或令人困惑,请告诉我。