I have never used user controls in C# .NET, and am working on a project where I have 3 dropdownlists, and need to put them into a user control. I was wondering how I would go about doing this. I will post the code for my dropdownlists, and a screen grab of what they currently look like, and what they should look like. Thanks.
public partial class _default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataBind();
DropDownList1_SelectedIndexChanged(sender, e);
DropDownList2.DataBind();
DropDownList2_SelectedIndexChanged(sender, e);
DropDownList3.DataBind();
}
}
// Drop Down List 1
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDataSource2.XPath = String.Format("mmsdata/mill[#n='{0}']/mach", DropDownList1.SelectedValue);
}
// Drop Down List 2
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDataSource3.XPath = String.Format("mmsdata/mill[#n='{0}']/mach[#n='{1}']/srn", DropDownList1.SelectedValue, DropDownList2.SelectedValue);
BindSensorList();
}
protected void BindSensorList()
{
//sender.GetType();
XmlDocument xdoc = XmlDataSource3.GetXmlDocument();
XmlNodeList nodes = xdoc.SelectNodes(XmlDataSource3.XPath);
var sensors = new List<Sensor>();
foreach (XmlNode node in nodes)
{
sensors.Add(new Sensor { id = node.Attributes["n"].Value, name = node.InnerText });
}
DropDownList3.DataSource = sensors;
DropDownList3.DataValueField = "id";
DropDownList3.DataTextField = "name";
DropDownList3.DataBind();
}
What I want them to look like
What they look like
The easiest way to add your dropdown to the user control is to create a user control in the project solution, and then put the asp.net webform code in the ui and you can then write a code behind for it same way you create a web forms.
Sample User Control
<%# Control Language="C#" ClassName="SampleUserControl" %>
<h3> <u>User Control</u> </h3>
<script runat=server>
</script>
Drop down 1: <asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
<asp:label id="Label1" runat=server/>
You can use a user control on a page and dynamically load its content, just initialise it on the page you wish to use it on and pass in values the same way you load a class/page
Related
Im facing a pretty weird problem today.
I created a DropDownList which adds the selected Item to a List. The List will be binded to the ListView.
This is the Dropdown:
<asp:DropDownList ID="ddlChooseNewApplication" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Selected="True">Bitte wählen</asp:ListItem>
<asp:ListItem Value="windows">Windows</asp:ListItem>
<asp:ListItem Value="mail">E-Mail</asp:ListItem>
<asp:ListItem Value="app1">App1</asp:ListItem>
<asp:ListItem Value="app2">App2</asp:ListItem>
<asp:ListItem Value="app3">App3</asp:ListItem>
</asp:DropDownList>
Next, when a Item has been clicked it runs the following code:
//This is a global variable
List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = ddlChooseNewApplication.SelectedValue.ToString();
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
It will be added to a List<> which should add the selected application to the ListView which looks like this:
<asp:ListView ID="ListViewNewApplications" runat="server">
<ItemTemplate>
<br /><br />
<%# Eval("ApplicationName") %><br />
<asp:Label ID="Label3" runat="server" Text="Titel"></asp:Label><br />
<asp:TextBox ID="tbNewTitle" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label4" runat="server" Text="Beschreibung"></asp:Label><br />
<asp:TextBox ID="tbNewDescription" runat="server" TextMode="MultiLine"></asp:TextBox><br /><br />
</div>
</ItemTemplate>
</asp:ListView>
Adding a single Item to the ListView works. The problem is, if I select a new Item in the DropDown, the current object in the ListView will be overwritten. I want it to create a new Item under the current Item.
Where is the mistake?
Many thanks in advance
Editted: Just read here that static property is per application domain, not per user, So the solution should change to use Session State
//Add using on top of .cs
using System.Linq;
//In cs class
public partial class name_of_class : Page
{
private List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Do page_load stuff....
Session.Remove("name_here"); //reset the Session when first page load
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Get List from Session
applicationName = (List<NewApplicationModels>)Session["name_here"];
if (applicationName == null)
applicationName = new List<NewApplicationModels>();
string value = ddlChooseNewApplication.SelectedValue.ToString();
if (!applicationName.Any(item => item.ApplicationName == value))
{
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
Session["name_here"] = applicationName;
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
}
}
For static property is per application domain, 2 stranger people from 2 different place browse the same page in same server (app domain) will all see it.
That means when userA change dropdownlist 2 times and the listview have 2 items, then userB browse and change dropdownlist, he may get 3 items in listview, 2 of userA and 1 of his recently choose (he may get nothing too).
I am looking for this over the internet but I am not getting any suitable answers. Here is my problem:
I have a web user control which has the following three controls
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true"
CssClass="radioButton" RepeatDirection="Horizontal" RepeatLayout="Flow"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" >
<asp:ListItem Value="UET111">Use Existing</asp:ListItem>
<asp:ListItem Value="CNT111">Create New</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" C></asp:TextBox>
It's code behind
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Visible = false;
TextBox1.Visible = false;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem.Value == "UET")
{
DropDownList1.Visible = true;
TextBox1.Visible = false;
}
else if (RadioButtonList1.SelectedItem.Value == "CNT")
{
DropDownList1.Visible = false;
TextBox1.Visible = true;
}
}
Now my question-
Actually I am using this web control 5 times on the same aspx page. The problem is I want to fill the dropdown list from aspx page and each dropdown will have different data(as I have 5 user control).
Is it possible to do that? Please help me with these and let me know if anyone knows a better way of doing this.
Create a public function in your user control that lets you send in a DataSource for the dropdown list to populate from, and if you want bind as well.
public BindDropdownDataSource(DataSet dataSource)
{
DropDownList1.DataSource = dataSource;
DropDownList1.DataBind();
}
I am adding a user to a role in asp.net but i want to get the selected role from a dropdownlist. a single option is working for me but i need to implement a two choices dropD list.
public partial class Register : Page
{
protected void Selection_Change(object sender, EventArgs e)
{
var tr = TakeRole.SelectedValue; // store it in some variable
}
protected void CreateUser_Click(object sender, EventArgs e)
{
// code
if (result.Succeeded)
{
manager.AddToRole(user.Id, "SomeRoleName");
}
}
on aspx i have
<asp:DropDownList id="TakeRole" AutoPostBack="True" OnSelectedIndexChanged="Selection_Change" runat="server">
<asp:ListItem> Supplier </asp:ListItem>
<asp:ListItem Selected="True"> Customer </asp:ListItem>
</asp:DropDownList>
Please use a listbox on multi selection mode and restrict selection to 2 items. Else you can use dropdownlist with jQuery.
If you intend to use checkboxlist, then other than binding source, in check changed event count checked items to 2 and if more items are selected an alert is to be displayed.
I have a page with a dropdownlist and a button on it. The initial selection on the dropdown is an empty string. I do not want this submitted to the server so I disable the button. I then want to enable the button if any other selection is made on the dropdown. However my ddlBusinessUnit_SelectedIndexChanged method is never hit when I make changes in the dropdown list.
html:
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsDate.Date = DateTime.Today;
PopulateBusinessUnits();
StatusMessages.Visible = false;
}
bGetFiles.Enabled = false;
}
public void ddlBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBusinessUnit.SelectedItem.Text != "")
bGetFiles.Enabled = true;
}
Set AutoPostBack="true" for your dropdown.
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
you're missing AutoPostBack="true" on your asp dropdown control
I have the following code in my aspx page:
<asp:Button id="display_button" runat="server" Text="Display" OnClick="Button1_Click" />
<asp:Button id="edit_button" runat="server" Text="Edit" OnClick="Button2_Click" />
<asp:Button id="save_button" runat="server" Text="Save" OnClick="Button3_Click" Visible="false" />
<asp:MultiView id="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View id="View1" runat="server">
<asp:FormView id="view_program" runat="server">
<ItemTemplate>
<%# Eval("status").ToString().Trim() %>
</ItemTemplate>
</asp:FormView>
</asp:View>
<asp:View id="View2" runat="server">
<asp:FormView id="edit_program" runat="server">
<ItemTemplate>
<asp:DropDownList id="p_status" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
</asp:View>
</asp:MultiView>
and the following functions attached to the buttons in the code-behind page:
protected void Button1_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View1);
save_button.Visible = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
save_button.Visible = true;
}
protected void Button3_Click(object sender, EventArgs e)
{
DropDownList p_status = edit_program.FindControl("p_status") as DropDownList;
var status = p_status.SelectedValue;
Label1.Text = status;
//save_button.Visible = false;
//MultiView1.SetActiveView(View1);
}
The idea being, that there are two views, the first displays the information, if the user wants to edit the information, they click button 2 which changes the view to the edit mode, which has the controls (drop downs, text fields, etc). It also makes the 'save' button appear.
What I am trying to make happen is, when the save button is clicked, it will grab all of the values from the various fields, update the object and then update the database. Then it would flip back to view1 with the updated info.
Problem is, as you can see in void Button3_Click, I try grab the values from the control, p_status, but it only gets the original value. example, the menu has three values, 'Green', 'Yellow', and 'Red'. Green is the default value and is selected when view2 is displayed. However, if I select Yellow or Red, and click save, rather than the label being updated to display one of those two values, it always displays Green.
Any ideas?
edit: page load function per request below
protected void Page_Load(object sender, EventArgs e)
{
try
{
Person myPerson = new Person(userid);
TestProgram myProgram = new TestProgram(id);
List<TestProgram> program = new List<TestProgram> { myProgram };
view_program.DataSource = program;
view_program.DataBind();
edit_program.DataSource = program;
edit_program.DataBind();
DropDownList p_status = edit_program.FindControl("p_status") as DropDownList;
p_status.Items.Add(new ListItem("Green", "Green"));
p_status.Items.Add(new ListItem("Yellow", "Yellow"));
p_status.Items.Add(new ListItem("Red", "Red"));
//myProgram.Status = "Red";
p_status.SelectedValue = myProgram.Status;
}
catch (Exception ex)
{
Response.Write(ex);
Label1.Text = ex.ToString();
}
}
Whoops...missed a little someting.. my
bad
when asp.net is not behaving as expected this is your best friend: MSDN: ASP.NET PAGE LIFECYLE
Upon Further Review...
there are a couple of problems here. your drop down list control with an id of "p_status" is contained inside a multiview (I forgot about what that meant...) you need to move the code to populate p_status into pre-render after checking to see if Multiveiw1.ActiveView = View2. Since it will always be a post back you need to bind values late in the page cycle