First, I am new to ASP.NET
In order to reuse my dropdown list across different forms on different pages, I was advised that to use User Control to accomplish this. So I did some reading about user control and attempted to play around with it, but couldn't get it work since I am new to ASP.NET. Get this error:
Cannot access a non-static member of outer type 'ASP.Vendor' via nested type 'ASP.Vendor._Default'
1) I create a Controls\Vendor.ascx file
<% @ Control Language="C#" ClassName="Vendor" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillVendor();
}
}
private void FillVendor()
{
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 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)
{
VendorList.DataSource = objDs.Tables[0];
VendorList.DataTextField = "VendorName";
VendorList.DataValueField = "VendorID";
VendorList.DataBind();
VendorList.Items.Insert(0,"-- Select --");
} else {
lblMsg.Text = "No Vendor Found";
}
}
}
</script>
<asp:DropDownList ID="VendorList" runat="server" AutoPostBack="True" >
</asp:DropDownList>
2) I create a Tes2.aspx page with this code to see if I can pull that Vendor dropdown list, but no luck.
<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc" TagName="Vendor"
Src="Controls\Vendor.ascx" %>
<html>
<body>
Testing
<form runat="server">
<uc:Vendor id="VendorList"
runat="server"
/>
</form>
</body>
Obviously, I am new and must doing thing wrong. Can someone please help me or give me an example of a dropdown list in user control and how to include it in a form? Thanks!