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" />
<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: