C#之 DropdownList控件使用心得

第一部份:20180718 

基础应用 取值

1、前台代码:

<asp:DropDownList ID="DDLcustomID" runat="server" DataTextField="customName" DataValueField="customID" EnableViewState="True" AppendDataBoundItems="False" AutoPostBack="False"></asp:DropDownList>

请注意各项参数的设置

EnableViewState="True" AppendDataBoundItems="False" AutoPostBack="False">

2、后台代码:

public partial class SysCustomCNameNew : System.Web.UI.Page
{
    int reValue;//用于保存返回值。返回值为-1(用户名存在),0(失败),1(成功),2(用户名不存在)
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //DDLcustomID.Items.Clear();
            //DDLcustomID.DataTextField = "";
            //DDLcustomID.DataValueField = "";
            ////DDLcustomID.DataSource = dt;
            ////DDLcustomID.DataBind();
            DropDownListBind();
        }
        
    }
    public void DropDownListBind()
    {
        //DDLcustomID.Items.Clear();
        //实例化公共类的对象
        DB db = new DB();
        //定义SQL语句
        string sqlstr = "SELECT [customID], [customName] FROM [BaCustom] ";
        //实例化数据集DataTable用于存储查询结果
        DataTable dt = db.reDt(sqlstr);
        //绑定DropDownListt控件
        DDLcustomID.DataSource = dt;//设置数据源,用于填充控件中的项的值列表
        DDLcustomID.DataBind();//将控件及其所有子控件绑定到指定的数据源
        //DDLcustomID.SelectedValue ="请选择";
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        reValue = CheckCNameNew();
        if (reValue == -1)
        {
            Response.Write("<script>alert('此客户简称下的联系人已经存在,请检查后重新录入!');</script>");
        }
        else
        {
            DB db = new DB();           

            int customID = int.Parse(DDLcustomID.SelectedValue.ToString());

            //string customID = DDLcustomID.SelectedValue.ToString();
            //string customID = DDLcustomID.SelectedItem.Value;
            //string customID = DDLcustomID.SelectedValue;
            //string customID = DDLcustomID.SelectedItem.Value;
            //int customID = Convert.ToInt32(DDLcustomID.SelectedValue.ToString());
            //string customID = this.TextcustomID.Value;
            string cuConName = this.textcuConName.Value;
            string cuConDust = this.textcuConDust.Value;
            string cuConMob = this.textcuConMob.Value;
            string cuConTel = this.textcuConTel.Value;
            string cuConMail = this.textcuConMail.Value;
            string cuConQQ = this.textcuConQQ.Value;
            string cuConRemark = this.textcuConRemark.Value;
            string cmdStr = "insert into BaCuContact(customID,cuConName,cuConDust,cuConMob,cuConTel,cuConQQ,cuConMail,cuConRemark) values('" + customID + "','" + cuConName + "','" + cuConDust + "'," + cuConMob + ",'" + cuConTel + "','" + cuConQQ + "','" + cuConMail + "','" + cuConRemark + "')";
            reValue = db.sqlEx(cmdStr);
            if (reValue == 1)
            {
                Response.Write("<script>alert('添加新联系人成功!');</script>");
                ClearText();//添加成功后清空输入框

            }
            else if (reValue == 0)
            {
                Response.Write("<script>alert('添加新联系人失败!!');</script>");
                //Response.Redirect("~/sysCustomCNameNew.aspx");
            }
        }
    }
    public void ClearText()
    {
        Response.Redirect("~/sysCustomCNameNew.aspx");
        //this.textcuConName.Value = "";
        //this.textcuConDust.Value = "";
        //this.textcuConMob.Value = "";
        //this.textcuConTel.Value = "";
        //this.textcuConMail.Value = "";
        //this.textcuConQQ.Value = "";
        //this.textcuConRemark.Value = "";
    }
    public int CheckCNameNew()
    {
        //实例化公共类对象
        DB db = new DB();
        string str = "select count(*) from BaCuContact where cuConName='" + this.textcuConName.Value + "' and customID='" + DDLcustomID.SelectedValue.ToString() + "'";

扫描二维码关注公众号,回复: 2690885 查看本文章

        DataTable dt = db.reDt(str);
        if (dt.Rows[0][0].ToString() != "0")
        {
            return -1;//该联系人已经存在
        }
        else
        {
            return 2;//该联系人尚未注册
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        //Response.Redirect("~/sysCustomNew.aspx");

    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/sysCustomCName.aspx");

    }
}

猜你喜欢

转载自blog.csdn.net/li97203/article/details/81096721