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() + "'";

        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");

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值