Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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.
 
Share this answer
 
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;


Edit: Added code block.
 
Share this answer
 
v2
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.
 
Share this answer
 
v2
Comments
Sandeep Mewara 7-Apr-11 10:22am    
Does not look like an answer. You should have edited and updated your question.
r verma 7-Apr-11 22:38pm    
In above code you are trying to find UserControl with 'Label' with it's actual class name which will not work. Use 'UserControl' instead of 'LabelUserControl'. Also to find second UserControl do not directly use 'FindControl' instead use this.Page.FindControl like what I wrote in my last reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900