.NET中DropDownList的应用

一. 初始化dropdownlist
1. VIEW层
//====================================================================================
/// <summary>
/// 设置控件内容
/// </summary>
/// <returns></returns>
/// <originated>Shawn Qiu</originated>
/// <mender></mender>
/// <date>2010-04-01</date>
private void InitControlsValue(MT_TW_UserInfo t_tw_userinfo)
{
    QiuF.SetStrArray2Drplst(drplst_UserRole, BT_TW_RoleInfo.RoleInfo_GetRoleInfo(connection));
    
}//end of Function:SetControlsValue(MT_TW_UserInfo t_tw_userinfo)


2. BUSINESS层
//====================================================================================
/// <summary>
/// 获取RoleInfo表中RoleInfo的信息
/// </summary>
/// <returns>DropDownList</returns>
/// <originated>Shawn Qiu</originated>
/// <mender></mender>
/// <date>2010-03-31</date>
public static DropDownList RoleInfo_GetRoleInfo(SqlConnection connection)
{
    DropDownList drplst_RoleInfo = new DropDownList();
    string sRoleID, sRoleName;
    char[] cSeperator = { ',' };
    string[] strRoleID,strRoleName;

    string sqlRoleInfo = SQL_RoleInfo_Select_DESC();
    SqlDataAdapter daRoleInfo = new SqlDataAdapter(sqlRoleInfo, connection);
    DataSet dsRoleInfo = new DataSet();
    daRoleInfo.Fill(dsRoleInfo, MT_TW_RoleInfo.TableName);

    sRoleID = "";
    sRoleName = "";
    for (int i = dsRoleInfo.Tables[MT_TW_RoleInfo.TableName].Rows.Count - 1; i >= 0; i--)
    {
        sRoleID = sRoleID + "," + dsRoleInfo.Tables[MT_TW_RoleInfo.TableName].Rows[i].ItemArray[0].ToString();
        sRoleName = sRoleName + "," + dsRoleInfo.Tables[MT_TW_RoleInfo.TableName].Rows[i].ItemArray[1].ToString();

    }

    strRoleID = sRoleID.Split(cSeperator);
    strRoleName = sRoleName.Split(cSeperator);

    drplst_RoleInfo.Items.Clear();
    for (int i = 0; i < strRoleID.Length; i++)
    {
        ListItem li = new ListItem(strRoleName[i],strRoleID[i]);
        drplst_RoleInfo.Items.Add(li);

    }

    return drplst_RoleInfo;

}//end of Function:RoleInfo_GetRoleInfo(SqlConnection connection)

//====================================================================================
/// <summary>
/// 构造Select的基本数据的SQL语句(降序)
/// </summary>
/// <returns>SQL语句</returns>
/// <originated>Shawn Qiu</originated>
/// <date>2010-03-31</date>
private static string SQL_RoleInfo_Select_DESC()
{
    return string.Format(@"SELECT roleid,rolename,rolememo FROM t_tw_roleinfo ORDER BY roleid DESC");

}//end of Function:SQL_RoleInfo_Select_DESC()


3. 函数库调用函数
//====================================================================================
/// <summary>
/// 填充ListItem数据至DropdownList
/// </summary>
/// <returns></returns>
/// <originated>Shawn Qiu</originated>
/// <date>2010-03-31</date>
public static void SetStrArray2Drplst(DropDownList drplst_FormPage, DropDownList drplst_DataBase)
{
    drplst_FormPage.Items.Clear();
    for (int i = 0; i < drplst_DataBase.Items.Count; i++)
    {
        drplst_FormPage.Items.Add(drplst_DataBase.Items[i]);
    }

}//end of Function:SetStrArray2Drplst(DropDownList drplst_FormPage, DropDownList drplst_DataBase)



二. DropDownList -> Database
t_tw_userinfoTemp.UserRole = Convert.ToInt32(drplst_UserRole.SelectedValue);



三. Database -> DropDownList
drplst_UserRole.SelectedValue = t_tw_userinfo.UserRole.ToString();




你可能感兴趣的:(sql,C++,c,.net,C#)