在用户控件之间传递值 [英] Passing Values between user controls

查看:78
本文介绍了在用户控件之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在设计页面时遇到问题.我创建了两个用户控件.在一个用户控件中,我选择了下拉列表,在第二个用户控件中,我选择了标签.我在一个网页中添加了这两个用户控件.在下拉列表中,我选择了颜色.当我更改下拉列表中的值时,它将在第二个用户控件中根据该值显示颜色.而且一件事情是不要在网页上发生任何事件,这意味着按钮事件.如果可能的话,我该如何创建它.

你能给我任何想法吗?如果可能,请提供示例代码.对我来说非常有用.或提供任何最佳教程站点名称,因为我是asp.net的新手

问候,
G.Manikanta.

Hi,

I have a problem in desining a page. I created two user controls. in one user control i took the dropdown list, in second user control i took the label. i added these two user controls in one webpage. In drop down list i took the colors. When i change the value in drop down list it display the color according to that value in the second user control. and onething is dont take any events in the webpage it means button events. If it is possible How can I create this.

Can you Give me any idea. If it is possible give the sample code. It is very useful to me. or give any best tutorial site name because i new to the asp.net

Regards,
G.Manikanta.

推荐答案

给出具有Web控件的TwoColumn.master

]]>
]]>
< asp:content id ="Content1" contentplaceholderid ="cph1" runat ="server" xmlns:asp =#unknown">
< asp:contentplaceholder id ="cph2" runat ="server">
< test:ctrlsearch id ="ctrlSearch" runat ="server" xmlns:test =#unknown">

-------------------------------------------------- -------------------------------------------------- -
在我的Web控件ctrlSearch中,我在ctrlSearch.ascx.cs
中声明了以下内容
命名空间OmegaLove.Web.UI
{
公共局部类ctrlSearch:OmegaLoveBasePageUserControl
{
公共字符串age1//下拉列表为search_age_start
{
得到{return search_age_start.Text; }
设置{search_age_start.Text =值; }
}
}
在ctrlSearch用户界面页面ascx中,我具有以下按钮:

< asp:button cssclass ="button-search" id ="imgSearch" runat ="server">
PostBackUrl =〜/Secure/SearchResults.aspx" Font-Bold ="True"/>
当我发布到SearchResults.aspx
我无法检索这些值:
-------------------------------------------------- -----------

命名空间OmegaLove.Web.UI.Pages
{
公共局部类SearchResults:OmegaLoveBasePage
{
公共静态字符串ConnnectionString = ConfigurationManager.ConnectionStrings ["omegaloveConnectionString"].ToString();
private int计数器= 0;

受保护的void Page_Load(对象发送者,EventArgs e)
{
ctrlSearch objTestControl =(ctrlSearch)Page.FindControl("ctrlSearch"); ->返回null
DropDownList objDropDownList =(DropDownList)objTestControl.FindControl("search_age_start");
任何帮助都会很棒.
Given TwoColumn.master that has a web control

]]>
]]>
<asp:content id="Content1" contentplaceholderid="cph1" runat="server" xmlns:asp="#unknown">
<asp:contentplaceholder id="cph2" runat="server">
<test:ctrlsearch id="ctrlSearch" runat="server" xmlns:test="#unknown">

-----------------------------------------------------------------------------------------------------
In my web control ctrlSearch I declared the following in ctrlSearch.ascx.cs

namespace OmegaLove.Web.UI
{
public partial class ctrlSearch : OmegaLoveBasePageUserControl
{
public string age1 // Dropdownlist is search_age_start
{
get { return search_age_start.Text; }
set { search_age_start.Text = value; }
}
}
In the ctrlSearch UI page ascx I have the following button:

<asp:button cssclass="button-search" id="imgSearch" runat="server">
PostBackUrl="~/Secure/SearchResults.aspx" Font-Bold="True" />
when I post to SearchResults.aspx
I cannot retrieve the values:
-------------------------------------------------------------

namespace OmegaLove.Web.UI.Pages
{
public partial class SearchResults : OmegaLoveBasePage
{
public static string ConnnectionString = ConfigurationManager.ConnectionStrings["omegaloveConnectionString"].ToString();
private int counter = 0;

protected void Page_Load(object sender, EventArgs e)
{
ctrlSearch objTestControl = (ctrlSearch)Page.FindControl("ctrlSearch"); --> returns null
DropDownList objDropDownList = (DropDownList)objTestControl.FindControl("search_age_start");
Any help woud be great.


如果我误解了您的问题,请纠正我.您不希望在ASPX页中定义任何事件吗?因为一种常见的解决方案是使用委托和事件在用户控件之间通过(使用公共属性)在值之间转移值.

另一种情况是您试图仅在第一个UserControl中编写完整的逻辑(一个带有Drop Down的逻辑).在这种情况下,如果"Selected_Index"发生更改,可以通过在当前页面中找到第二个用户控件并设置其文本来实现.大致的代码如下所示:

Correct me if I misunderstood your problem. You don''t want any event to be defined in your ASPX page? Because one common solution is to use delegates and events to trasnfer values between your User Controls through that (using public properties).

Other case is that you are trying to write complete logic in your first UserControl only (one with Drop Down). In that case in the event for ''Selected_Index'' change this can be achieved by finding your second User Control in current Page and setting it''s Text. A rough code will look like:

UserControl label = this.Page.FindControl("UserControl21") as UserControl;
        Label lbl = label.FindControl("Label1") as Label;
        lbl.Text = DropDownList1.SelectedValue;



添加了代码块.



Added code block.


你好varma
感谢您提供解决方案.我尝试了您的解决方案,但无法正常工作.我认为我可能在代码中写了一些错误,您可以纠正我吗?我将发送我的代码.寄到这封邮件

我在dropdownusercontrol.ascx.cs
中编写了代码
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;

命名空间UserControlsExamples
{
公共局部类DropDownUserControl:System.Web.UI.UserControl
{
受保护的void Page_Load(对象发送者,EventArgs e)
{

}

受保护的void DDColors_SelectedIndexChanged(对象发送者,EventArgs e)
{
LabelUserControl lblCtrl =(LabelUserControl)(FindControl("labelcontrol")));

标签lbl =(标签)(lblCtrl.FindControl("labelcontrol")));

//lbl.BackColor = System.Drawing.Color.FromName(DDColors.SelectedValue);
lbl.Text = DDColors.SelectedItem.Text;

}
}
}





//LabelUsercontrol.ascx.cs中的代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;

命名空间UserControlsExamples
{
公共局部类LabelUserControl:System.Web.UI.UserControl
{
公共标签Display_Label
{
获取
{
返回this.DisplayLabel;

}
设置
{
this.DisplayLabel =值;
}
}
受保护的void Page_Load(对象发送者,EventArgs e)
{

}
}
}

您能否看一下这段代码,并告诉我我做错了什么

而且我没有在.aspx.cs文件中编写任何代码.
Hello varma
Thank you for giving the solution. I tried your solution but it wont work. i think i may be write some mistake in my code can you correct me? i will send my code. to this mail

i wrote the code in dropdownusercontrol.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserControlsExamples
{
public partial class DropDownUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void DDColors_SelectedIndexChanged(object sender, EventArgs e)
{
LabelUserControl lblCtrl = (LabelUserControl)(FindControl("labelcontrol"));

Label lbl = (Label)(lblCtrl.FindControl("labelcontrol"));

//lbl.BackColor = System.Drawing.Color.FromName(DDColors.SelectedValue);
lbl.Text = DDColors.SelectedItem.Text;

}
}
}





//The code in LabelUsercontrol.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserControlsExamples
{
public partial class LabelUserControl : System.Web.UI.UserControl
{
public Label Display_Label
{
get
{
return this.DisplayLabel;

}
set
{
this.DisplayLabel = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{

}
}
}

can you look at this code and tell me what i did wrong

and i didnt write any code in .aspx.cs file.


这篇关于在用户控件之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆