自定义控件名称:
DropDownList_group.ascx
内容:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DropDownList_group.ascx.cs" Inherits="ascx_DropDownList_group" %>
<asp:DropDownList ID="DropDownList_group" runat="server" Width="150px">
</asp:DropDownList>
CS文件内容using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class ascx_DropDownList_group : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        group_show();
    }
    public void group_show()
    {
        DBConn myDB = new DBConn();
        string mySql = "";
        mySql = "select * from abis_group where 1 = 1 order by group_power asc";
        this.DropDownList_group.DataSource = myDB.getDataReader(mySql);
        this.DropDownList_group.DataTextField = "group_name";
        this.DropDownList_group.DataValueField = "group_power";
        this.DropDownList_group.DataBind();
        myDB.Close();
    }
}
已经在其它页面中正确引用
请问俺在其它页面中,点击DropDownList_group.ascx中的下拉框时,如何得到下拉框中的值?
小弟新手,谢谢

解决方案 »

  1.   

    public partial class ascx_DropDownList_group : System.Web.UI.UserControl
    {
        public string SelectedValue{
          get{
            EnsureChildControls();
            this.DropDownList_group.SelectedValue;
          }
        }
        .......
      

  2.   

    呵呵,上面少写了一个“return ”!
      

  3.   

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class ascx_DropDownList_group : System.Web.UI.UserControl
    {
        public string SelectedValue
        {
             get
               {
                  return this.DropDownList_group.SelectedValue;
               }
         }
        protected void Page_Load(object sender, EventArgs e)
        {
            group_show();
        }
        public void group_show()
        {
            DBConn myDB = new DBConn();
            string mySql = "";
            mySql = "select * from abis_group where 1 = 1 order by group_power asc";
            this.DropDownList_group.DataSource = myDB.getDataReader(mySql);
            this.DropDownList_group.DataTextField = "group_name";
            this.DropDownList_group.DataValueField = "group_power";
            this.DropDownList_group.DataBind();
            myDB.Close();
        }
    }