using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
namespace Common
{
public abstract class DropDownListHelper
{
public readonly string STRING_VALUE = "STRING_VALUE";
public readonly string STRING_TEXT = "STRING_TEXT";
public static void SetSource(out string sError,DropDownList ddlCtrl, string sSQL,DataTable dtSource = null, bool bSelectFirstItem = false, string sFirstRowText = "")
{
sError = string.Empty;
DataTable dt = null;
try
{
if (!string.IsNullOrEmpty(sSQL))
{
dt = SqlServerHelper.GetDataTable(out sError,sSQL);
}
else if (dtSource != null)
{
dt = dtSource.Copy();
}
}
catch (Exception ex)
{
sError = ex.Message.ToString();
return;
}
ddlCtrl.Items.Clear();
ddlCtrl.DataSource = null;
if (dt != null && dt.Rows.Count > 0 && dt.Columns.Count > 1) // 2 : id value
{
if (!string.IsNullOrEmpty(sFirstRowText))
{
DataRow dr = dt.NewRow();
dr[dt.Columns[0]] = -1; // ID -1
dr[dt.Columns[1]] = sFirstRowText;
dt.Rows.InsertAt(dr, 0);// 0
}
//ddlCtrl.AutoPostBack = true;
ddlCtrl.DataSource = dt;
ddlCtrl.DataValueField = dt.Columns[0].ColumnName; // ID
ddlCtrl.DataTextField = dt.Columns[1].ColumnName; //
ddlCtrl.DataBind();
if (bSelectFirstItem) //
{
int iID = 0;
if (Int32.TryParse(dt.Rows[0][0].ToString(), out iID))
{
ddlCtrl.SelectedValue = iID.ToString();
}
}
else
{
ddlCtrl.Items.Insert(0, "");// 0
}
}
}
public static void SelectItem(out string sError, DropDownList ddlCtrl, string sSQL,string sSelectedValue="")
{
sError = string.Empty;
try
{
if (!string.IsNullOrEmpty(sSelectedValue)) ddlCtrl.SelectedValue = sSelectedValue;
else if (!string.IsNullOrEmpty(sSQL))
{
object oResult = SqlServerHelper.GetSingle(sSQL);
if (!Common.Tool.IsNullOrEmptyObject(oResult))
ddlCtrl.SelectedValue = oResult.ToString();
}
}
catch (Exception ex)
{
sError = ex.Message.ToString();
return;
}
}
}
}
// :1
string sError=string.Empty;
Common.DropDownListHelper.SetSource(out sError, ddlAppName, "", dataTable,true," ");
if (!string.IsNullOrEmpty(sError)) { Common.MessageBox.Show(this, sError); return; }
// :2
string sError=string.Empty;
Common.DropDownListHelper.SetSource(out sError, ddlAppName, " sql (2 :id,value)", null,true," ");
if (!string.IsNullOrEmpty(sError)) { Common.MessageBox.Show(this, sError); return; }
// dropDownlist
string sSQL = "select GroupID FROM sysUserGroup WHERE AppID= " + hidAppID.Value + " and UserID=" + hidUserID.Value;
Common.DropDownListHelper.SelectItem(out sError, ddlGroup, sSQL);
if (!string.IsNullOrEmpty(sError)) { Common.MessageBox.Show(this, sError); return; }
//Common.MessageBox.Show :
/// <summary>
///
/// </summary>
/// <param name="page"> , this</param>
/// <param name="msg"> </param>
public static void Show(System.Web.UI.Page page,string msg)
{
msg = msg.Replace("'", "\""); //
page.ClientScript.RegisterStartupScript(page.GetType(),"message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
}
// SqlServerHelper :http://blog.csdn.net/keenweiwei/article/details/6845709