ASP.NET C# - Dropdown list by using User Control
Solution 1
You can put two properties in your VendorListControl
to get the ddlVendorList selectedItem text and the ddlVendorBUList selectedItem text.
In VendorListControl.ascx.cs :
public string GetDdlVendorListSelectedItemText
{
get { return this.ddlVendorList.text; }
}
public string GetDdlVendorBUListSelectedItemText
{
get { return this.ddlVendorBUList.text; }
}
Then from your page CreateNewRecord
, you can access those properties. You just need to add an id to your control :
<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %>
// Some where in the form
<tr>
<td class="right" width="20%">Vendor Name:</td>
<td>
<uc:VendorListControl id="vendorListControl" runat="server" />
</td>
</tr>
And you can access your properties like this in CreateNewRecord.aspx.cs :
ExecuteInsert(this.vendorListControl.GetDdlVendorListSelectedItemText,
this.vendorListControl.GetDdlVendorBUListSelectedItemText,
MDFAmount.Text,
StartDate.Text,
EndDate.Text,
VendorQuarter.Text,
MDFName.Text,
MDFSummary.Text,
Status.SelectedItem.Text,
CreatedBy.Value
);
Solution 2
You define public property
who return SelectedItem.Text
in your UserControl.
User Control (Ascx)
public string YourValue
{
get
{
return ddlVendorList.SelectedItem.Text;
}
}
Page (Aspx)
You can use your public property : YourValue.
ExecuteInsert(YourValue,
....... );
Nota : if you wish set value, you define setter on your property
After your update add theses properties
public string YourDdlVendorListSelectedItemText
{
get
{
return this.ddlVendorList.text;
}
}
public string YourDdlVendorBUListSelectedItemText
{
get
{
return this.ddlVendorBUList.text;
}
}
Milacay
Updated on June 04, 2022Comments
-
Milacay almost 2 years
I am new to ASP.NET
Someone in this forum helped me how to get the dropdown list work wth user countrol and it is working.
In my user control file, VendorListControl.ascx, I have this code below. Please assume that the VendorListControl.ascx.cs works correctly, which is when I select a VendorName, it will fired "ddlVendor_SelectedIndexChanged" to refreshed the "ddlVendorBUList" dropdown list.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="VendorListControl.ascx.cs" Inherits="MyNamespace.VendorListControl" %> <asp:DropDownList runat="server" ID="ddlVendorList" onselectedindexchanged="ddlVendor_SelectedIndexChanged" AutoPostBack="True" /> <asp:DropDownList runat="server" ID="ddlVendorBUList" AutoPostBack="True" /> <asp:Label runat="server" ID="lblMessage" />
My VendorListControl.ascx.cs code:
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace MyNamespace { public partial class VendorListControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillVendors(); } } protected void ddlVendor_SelectedIndexChanged(object sender, EventArgs e) { int VendorID = Convert.ToInt32(ddlVendorList.SelectedValue.ToString()); FillVendorBU(VendorID); } private void FillVendors() { string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT VendorID, VendorName FROM MDF_Vendor"; DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; ; conn.Open(); dAdapter.Fill(objDs); conn.Close(); if (objDs.Tables[0].Rows.Count > 0) { this.ddlVendorList.DataSource = objDs.Tables[0]; this.ddlVendorList.DataTextField = "VendorName"; this.ddlVendorList.DataValueField = "VendorID"; this.ddlVendorList.DataBind(); this.ddlVendorList.Items.Insert(0, "-- Select --"); } else { this.lblMessage.Text = "No Vendor Found"; } } private void FillVendorBU(int VendorID) { string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT VendorBUID, VendorBUName FROM dbo.MDF_VendorBU WHERE VendorID = @VendorID"; cmd.Parameters.AddWithValue("@VendorID", VendorID); DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { ddlVendorBUList.DataSource = objDs.Tables[0]; ddlVendorBUList.DataTextField = "VendorBUName"; ddlVendorBUList.DataValueField = "VendorBUID"; ddlVendorBUList.DataBind(); ddlVendorBUList.Items.Insert(0, "--Select--"); } else { lblMessage.Text = "No states found"; } } } }
Next, in my CreateNewRecord.aspx page, I have this code to include both dropdown list from user control. And I can see the dropdown lists works properly.
<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %> // Some where in the form <tr> <td class="right" width="20%">Vendor Name:</td> <td> <uc:VendorListControl runat="server" /> </td> </tr>
The problem is related to the ID of those two dropdown lists. When I attemp to do the insert record, it seems not to detect the ID for "ddlVendorList" and "ddlVendorBUList" come from user control ascx page. Error " The name 'ddlVendorList' does not exist in the current context"
ExecuteInsert(ddlVendorList.SelectedItem.Text, ddlVendorBUList.SelectedItem.Text, MDFAmount.Text, StartDate.Text, EndDate.Text, VendorQuarter.Text, MDFName.Text, MDFSummary.Text, Status.SelectedItem.Text, CreatedBy.Value ); Response.Write("<b>Record was successfully added!</b>");
I know I am new to ASP.NET, so please help.
-
Milacay over 11 yearsI updated the post with the code in my ascx.cs file. Since I am new, please help took a look of that file and let me know what I need to modify... thanks!
-
Aghilas Yakoub over 11 yearsI'am happy to help you Milacay Thank's
-
Milacay over 11 yearsI got it works now. Thanks for being specific which is really helpfull for newbie like me.
-
Milacay over 11 yearsOne quick question, if I have a <table>, how do I put these two dropdown list in 2 rows like this <tr><td>"ddlVendorList" dropbox</td></tr> <tr><td>"ddlVendorBUList" dropbox</td></tr>. They are in one line only "<uc:VendorListControl id="vendorListControl" runat="server" />"?
-
Milacay over 11 yearsAghilas, please show me how? I only have one line ""<uc:VendorListControl id="vendorListControl" runat="server" />" code for both dropdown lists. how can I make them in 2 rows in the table using <tr><td> tags? Thanks
-
Aghilas Yakoub over 11 years<table><tr><td>your ddl 1</td></tr> <tr><td>your ddl 2</td></tr></table>
-
Milacay over 11 yearsI am totally lost, so I have "<tr><td><uc:VendorListControl id="vendorListControl" runat="server" /></td></tr>", dipslaying BOTH dropdown lists, ddlVendorList and ddlVendorBUList. I want to split them in two rows. If I do this, I will have total of 4 dropdown list with 2 each "<tr><td><uc:VendorListControl id="vendorListControl" runat="server" /></td></tr><tr><td><uc:VendorListControl id="vendorListControl" runat="server" /></td></tr>". Please help
-
Aghilas Yakoub over 11 yearsyou can divide inside your user control and not in your aspx Milacay
-
Milacay over 11 yearsThanks again Aghilas, I see what you mean. If I divide them in two rows in user control, later some other pages (like GridView), then I won't be able to use this code to divide in 2 columns only. Maybe I will have to create another same user control, but just divide them in 2 columns for gridview usage.
-
Aghilas Yakoub over 11 yearsI'am happy to help you Milacay and that you solved your problem