ASP.NET WEB用户控件下拉框

上一篇 / 下一篇  2008-02-14 10:03:26 / 个人分类:IT技术

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>   

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   

<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>

WebUserControl1.ascx.cs

    

using System;
        using System.Data;
        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;
        using System.Data.SqlClient;

    

namespace WebApplication1
        {
        public partial class WebUserControl1 : System.Web.UI.UserControl
        {
        private string pStrSql;
        private int pSelectedIdx;
        private string pLabelText;
        private bool pIsSelAll;
        protected void Page_Load(object sender, EventArgs e)
        {
        if (!Page.IsPostBack)
        {
        MakDropDownList();
        Label1.Text = this.pLabelText;
        }
        }
        public WebUserControl1()
        {
        pStrSql = "";
        pSelectedIdx = -1;
        }
        /// <summary>
        /// 控件属性SQL语句
        /// </summary>
        public string StrSql
        {
        set { pStrSql = value; }
        }
        /// <summary>
        /// 控件属性选中项的id
        /// </summary>
        public int SelectedIdx
        {
        set { pSelectedIdx = value; }
        }

    

/// <summary>
        /// 下拉框标题
        /// </summary>
        public string LabelText
        {
        set { pLabelText = value; }
        }

    

/// <summary>
        /// 是否要全选项
        /// </summary>
        public bool IsSetAll
        {
        set { pIsSelAll = value; }
        }
        protected void MakDropDownList()
        {
        if (this.pStrSql == string.Empty || this.pStrSql == null)
        {
        DropDownList1.Items.Clear();
        DropDownList1.Items.Add("没有数据");
        DropDownList1.Items[0].Value = "-1";
        }
        else
        {
        DropDownList1.Items.Clear();
        if (pIsSelAll)
        {
        DropDownList1.Items.Add("所有值");
        DropDownList1.Items[0].Value = "-1";
        }
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnMain1"].ConnectionString;
        string sqlstr = this.pStrSql; //"SELECT ProductName,ProductID  FROM Products ORDER BY ProductID";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
        DropDownList1.Items.Add(dt.Rows[i][1].ToString());
        if (pIsSelAll)
        {
        DropDownList1.Items[i+1].Value = dt.Rows[i][0].ToString();
        }
        else
        {
        DropDownList1.Items[i].Value = dt.Rows[i][0].ToString();
        }
        }
        }
        }

    

}
        }

    

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
    <title>无标题页</title>
& nbsp;   </head>
    <body>
    <form. id="form1" runat="server">
    <div>
    <uc1:WebUserControl1 id="WebUserControl1_1" StrSql="SELECT CategoryID, CategoryName FROM Categories" LabelText="产品分类"  runat="server">
    </uc1:WebUserControl1>
    <uc1:WebUserControl1 ID="WebUserControl1_2" runat="server" />
    &nbsp;
    <asp:Button ID="Button1" runat="server" nClick="Button1_Click" Text="Button" /><br />
        选择结果:<asp:Label ID="Label1" runat="server" ForeColor="#0000C0">ddddd</asp:Label></div>
    </form>
    </body>
    </html>
    

Default.aspx.cs

        using System;
        using System.Data;
        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;

    

namespace WebApplication1
        {
        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

    

protected void Button1_Click(object sender, EventArgs e)
        {
        Label1.Text = ((DropDownList)WebUserControl1_1.FindControl("DropDownList1")).SelectedItem.Value.ToString();
        }
        }
        }
    

Web.config

<connectionStrings>
<add name="ConnMain1" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>

TAG:

引用 删除 Guest   /   2008-03-23 11:29:20
1
引用 删除 小叶   /   2008-03-23 10:47:32
关于这个 我很奇怪 我的就是获取不了他的
selectedindex值
一直为0
能不能交流下
我QQ:360678342
Msn:flyxiaoye@hotmail.com
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-23  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 1492
  • 日志数: 27
  • 图片数: 3
  • 书签数: 7
  • 建立时间: 2007-01-08
  • 更新时间: 2008-02-27

RSS订阅

Open Toolbar