I am trying to access DropDownList selected value from user control which is placed inside Repeater's ItemTemplate.
string ddlBeneficiaryTeam2 = ((UserControl_TeamFilter)(rptBeneficiaries.FindControl("ucBeneficiaryTeam"))).TeamSelectedValue;
rptBeneficiaries -Repeater control
ucBeneficiaryTeam -User control
ddlteam -DropDownList Name its in user control.
You can use FindControl to locate the correct Control by going up the control tree.
DropDownList drp = Repeater1.Items[i].FindControl("WebUserControl1").FindControl("DropDownList1") as DropDownList;
First you access the correct repeater item by an index and then by ID of the User Control and then the DropDownList inside the control.
first divide this to two part
1-first in your Usercontrol create public property that represents value of your DropDown
public class UserControl_TeamFilter : System.Web.UI.UserControl
{
....
public string TeamSelectedValue
{
get { return ddlteam.SelectedValue; }
}
...
2-now you just need to find and cast your Usercontrol in repeater item as below
(rptBeneficiaries.Items[i].FindControl("ucBeneficiaryTeam") as UserControl_TeamFilter).TeamSelectedValue
Related
i like to get initial value to my DDL and write ddl1.Text="6" - it works fine..
i try to do the same to a DDL which is part of a simple usercontrol(3 DDLs which create a date) - this doesn't work!!!
in default.aspx i tried-
DateUserControl2.SetD("17");
DateUserControl2.SetM("7");
((DropDownList)DateUserControl2.Controls[4]).Text = "2003";
in DateUserControl.ascx.cs
i put all the listitems created in Page_Init and it works fine
the other methods
public void SetD(object d)
{
this.DropDownListDuc.Text = d + "";
}
public void SetM(object m)
{
this.DropDownListMuc.SelectedValue = m + "";
}
when i try to trace, i see that the methods are ok, but, for example,if d parameter is 4 and
this.DropDownListDuc.Text = 4 + "";
is performed, still NOTHING changes!!!
(again, the same line in a "simple" DDL,like DropDownList1.Text = "20"; changes the DDL to 20!!
changing a Label in the eser control works too. it is just a DDL_in_a_usercontrol problem
thanks!
Well, keep in mind that you are NOT allowed to have two controls with the same "id" on one page.
But, what then occurs if you drop the user control two times on the same web page?
I have a hotel edit UC.
So, I can do this:
Say this markup:
<uc1:UHotelEdit runat="server" ID="UHotelEdit"
MyTable ="tblHotelsA" />
<div style="clear:both;height:30px">
</div>
<uc1:UHotelEdit runat="server" ID="UHotelEdit2"
MyTable ="tblHotelsA" />
So, note we have TWO UC's on the page. UhotelEdit, and UhotelEdit2.
My page load code is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UHotelEdit.MyPk = 6;
UHotelEdit.LoadData();
UHotelEdit2.MyPk = 7;
UHotelEdit2.LoadData();
}
}
And now we have this:
So, in above, we have Hotel Name text box, first name, last name etc.
But, we are NOT allowed to have multiple "id" of the same control on the page.
So, how can/could I set the hotel name of the first control to Zoo, and Zoo2 for the 2nd one?
Well, all of the controls are "nested" inside of the UC.
This is quite much like a grid view, or whatever. So, you have two choices:
if several of the controls had to be "often" changed, then make a public property in the UC.
or, you can get/grab/pluck out the control name of the UC.
eg this:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox txtHotel = UHotelEdit.FindControl("txtHotel") as TextBox;
TextBox txtHotel2 = UHotelEdit2.FindControl("txtHotel") as TextBox;
txtHotel.Text = "Zoo";
txtHotel2.Text = "Zoo2";
}
And thus we now have this:
But, if you had to "often" change the hotel name text box above?
Well, then add a public property to the UC, say like this:
public string HotelName
{
get { return txtHotel.Text; }
set { txtHotel.Text = value; }
}
And now our code becomes this:
protected void Button1_Click(object sender, EventArgs e)
{
UHotelEdit.HotelName = "Zoo";
UHotelEdit2.HotelName = "Zoo2";
}
So, for common or often controls to be changed? Setup a get/set for that control. For the rare and not common used?
Then you can use find control. Quite much you do the same to get a control out of repeater, listview, gridview etc. - you in general have to use find control for those nested controls.
Well, everything quite much works the same, but we have to prefix the control(s) with the name of the user control (id) we used when dropping on the page.
but, if you have JavaScript code, then how can I select the first one (hotel name) vs the 2nd one?
Turns out, all controls rendering inside of a UC will PREFIX the control with the name of the user control!!!
So, on the page you find two hotel text boxes,
UHotelEdit_txtHotel
UhotelEdit2_txtHotel
But, they are not exposed that way to code behind (just like controls nested in a listview or repeater also are not). but, they exist and jQuery selectors can be used against those controls.
In code behind, you have to use UC.findcontrol("txtHotel") to pluck out a reference to that control in the UC - since its nested inside.
So, if your UC has a dropdown list, and you want to with ease to change that value? Then add a PUBLIC method to the control that allows you do to this. So, while the controls inside are not exposed, you can with ease add a simple public method that allows you to change the drop down list value.
I have added a user control dynamically which contains dropdowns'. Loading User control is iterated 'I' times according to the condition. Now, I need to access the dropdowns' selected item. I have created an accessor function to get the dropdown selected values, but it is giving me the default value, which is "00" of the drop-down.
If someone needs additional Information to solve my problem,
kindly ask me.
Adding user control dynamically:
for (int i = 0; i < 3; i++)
{
//DailyControl is a userControl
DailyControl w1 = (DailyControl)LoadControl("~/DailyControl.ascx");
//InputPanel is a panel where user control is added.
InputPanel.Controls.Add(w1);
}
DailyControl.ascx
Select the Time:
<asp:DropDownList ID="clock" runat="server">
<asp:ListItem Text="am" />
<asp:ListItem Text="pm" />
</asp:DropDownList>
DailyControl.ascx.cs // accessor function to get the dropdown
public string Clock
{
get
{
return clock.SelectedItem.Text.ToString();
}
}
Instead of add controls dinamically, you can use a Repeater on aspx page.
In the Repeater you can bind a DropDownList (or a control containing the DropDownList) and get
myDropDownList.SelectedValue
(or SelectedItem)
I am relatively new to asp.net.
I have parent custom user control and child custom user control.
As you can see above child control has DropDownList control.
When selection changed in DropDownList control postback accrued and Page_Load(object sender, EventArgs e) method of the parent control is fired,
at this stage(in Page_Load method of the parent control) I need to get the selected value in DropDownList.
Any idea how can I get the selected value in DropDownList in Page_Load method of the parent control?
One option would be to expose either the control on your child control, or an accessor to the value of the same control. For example, in your code behind for the child control you could have a property like
public TextBox MyTextBoxControl
{
get { return MyLocalTextBoxControl; }
}
And then access it on the Page_Load of the main control like so:
protected void Page_Load(object sender, EventArgs e)
{
...
var textValue = MyChildControl.MyTextBoxControl.Text;
...
}
Of course, you will need to decide if it makes better sense from a reusability standpoint whether to expose just the text portion of the control (or whatever property you need at the parent level) or access to the whole control.
For reference, you would expose access only to the text portion of the sub-sub control as follows.
public string MyTextBoxControlText
{
get { return MyLocalTextBoxControl.Text; }
set { MyLocalTextBoxControl.Text = value; }
}
I have two views of WPF (Vista1.xaml and Vista2.xaml), and they are part of a MainWindow.xaml.
In the Vista1.xaml i have a listview, where the user selects some items by clicking and the Vista2.xaml has a button.
I want that when the user clicks on the button of Vista2.xaml, get the items that the user previously selected in the listview of Vista1.xaml.
I have class in the Vista1.xaml.cs ListViewItem_PreviewMouseLeftButtonDown method that captures the user selects the item in the listview.
the code is as follows
private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && item.IsSelected)
{
...
}
}
I appreciate your help
Add a public property to you MainPage that contains the data you want to pass between Vista1 and Vista2. Then, in your event handler of Vista1, set the property and on the button click of Vista2 read the property.
I can't figure out how to access the value of a control in a dynamically-created gridview. Thhe problem is that I don't know the name of the control. If I knew the name of the control I could do just do this :
string dropDownListText =((DropDownList).row.FindControl("DropDownList1")).SelectedItem.Value;`
I know the type of control and I know the cell it is in. Is there any way using the information about the control I have to be able to access the control's value?
If you know what cell it is, then you could do something like this;
TableCell tc = GridView1.Cells[indexOfCell];
// where GridView1 is the id of your GridView and indexOfCell is your index
foreach ( Control c in tc.Controls ){
if ( c is YourControlTypeThatYouKnow ){
YourControlTypeThatYouKnow myControl = (YourControlTypeThatYouKnow)c;
}
}
If you don't know what cell it is exactly, then you can always loop through each cell. I am sure there is a better way in Linq.
With Linq (I think this should work)
var controls = GridView1.Cells[indexOfCell].Cast<Control>();
YourControlTypeThatYouKnow myControl = (YourControlTypeThatYouKnow) controls.Where( x => x is YourControlTypeThatYouKnow).FirstOrDefault();
On ItemDataBound event handler of GridView, access the table-cell as follows. Within the cell, Controls collection provides you access to ASP control embedded in the cell
TableCell cell = e.Item.Cells[0];
if (cell.Controls.Count > 0)
{
cell.Controls[0].Visible = false;
}
I have had the same problem. Add a command name to your field like CommandName="ProjectClick". Then add a rowcommand and in the row command do this:
if (e.CommandName.Equals("ProjectClick")) {
}
or you can use the text:
if (e.CommandName.Equals("")) {
if (((System.Web.UI.WebControls.LinkButton)(e.CommandSource)).Text.Equals("Projects")) {
}
}