使用用户控件的ASP.NET C#下拉列表

Mil*_*cay 10 c# asp.net

首先,我是ASP.NET的新手

为了在不同页面上的不同表单中重用我的下拉列表,我被告知要使用用户控件来完成此操作.所以我做了一些关于用户控件的阅读并尝试使用它,但由于我是ASP.NET新手,所以无法使用它.得到此错误:

无法通过嵌套类型"ASP.Vendor._Default"访问外部类型"ASP.Vendor"的非静态成员

1)我创建一个Controls\Vendor.ascx文件

<% @ 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>
Run Code Online (Sandbox Code Playgroud)

2)我使用此代码创建一个Tes2.aspx页面,看看我是否可以提取该供应商下拉列表,但没有运气.

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

显然,我是新人,必须做错事.有人可以帮助我或者给我一个用户控制下拉列表的示例以及如何将其包含在表单中吗?谢谢!

Tim*_*ora 2

我看到的第一个问题是你是从Page你的内部继承的UserControl

public partial class _Default : System.Web.UI.Page
Run Code Online (Sandbox Code Playgroud)

继承UserControl自代替。

// notice that I also renamed the class to match the control name
public partial class Vendor : System.Web.UI.UserControl
Run Code Online (Sandbox Code Playgroud)

使用代码隐藏文件

正如 @x0n 指出的,您的用户控件代码可以放置在代码隐藏文件中(当您在 Visual Studio 中创建用户控件时自动创建)。用户控件通常由标记部分 (.ascx)、隐藏代码 (.ascx.cs) 和设计器文件 (.ascx.designer.cs) 组成。HTML 标记进入 ASCX 文件,绑定代码进入代码隐藏。

我建议保存您的代码,删除当前的用户控件,然后通过 Visual Studio 重新添加它。

项目结构示例
在此输入图像描述

标记 (ASCX) 文件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VendorListControl.ascx.cs" Inherits="MyNamespace.VendorListControl" %>
<asp:DropDownList runat="server" ID="ddlVendorList" />
<asp:Label runat="server" ID="lblMessage" />
Run Code Online (Sandbox Code Playgroud)

代码隐藏

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();
            }
        }

        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 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";
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

替代方法 - 删除类声明

如果您出于某种原因不想添加代码隐藏文件,请完全删除类声明并仅将代码包含在其中。

<script runat="server">
    protected void Page_Load(object sender, EventArgs e){
        if (!IsPostBack){
            FillVendor();
        }
    }

    // etc
</script>
Run Code Online (Sandbox Code Playgroud)

作为旁注,我会将数据访问逻辑放在一个单独的类中,以便正确分离/重用,但是一旦纠正了上述问题,您概述的结构就应该可以工作。