zoukankan      html  css  js  c++  java
  • Asp.net开发之旅GridView中嵌入DropDownList的一点心得

    问题背景:  

          我们在实际的开发中,通常会使用到在GridView中嵌入DropDownList,在DropDownList中绑定自己想要的数据,使用户的选择在自己的设定的范围内

    然而,在这一过程中,我们通常会遇到页面刷新改变了DropDownList的选择,导致下拉框中的内容不是我们想要的。

    问题解决:

    (一)

    如图:在编辑之前,项目中的显示是在label上的,当我们点击编辑的时候,就准换为下拉框形式,而此时,我们想要下拉框中显示的内容是label中的内容:

    分析:我们可以在点击编辑的时候,先找到label控件,获取其中的值,然后再将label中值给下拉框就可以

     protected void gvInfo_RowEditing(object sender, GridViewEditEventArgs e)//触发编辑事件
            {
                Label item1 = (Label)gvInfo.Rows[e.NewEditIndex].FindControl("Label1");
                Label item2 = (Label)gvInfo.Rows[e.NewEditIndex].FindControl("Label2");
                Label item3 = (Label)gvInfo.Rows[e.NewEditIndex].FindControl("Label3");
                Label item4 = (Label)gvInfo.Rows[e.NewEditIndex].FindControl("Label7");
    //实例化Label控件
                    gvInfo.EditIndex = e.NewEditIndex;
                    ManInfoBind();
                    DropDownList Item1 = (DropDownList)gvInfo.Rows[e.NewEditIndex].FindControl("ddlItem1");
                    DropDownList Item2 = (DropDownList)gvInfo.Rows[e.NewEditIndex].FindControl("ddlItem2");
                    DropDownList Item3 = (DropDownList)gvInfo.Rows[e.NewEditIndex].FindControl("ddlItem3");
                    DropDownList Item4 = (DropDownList)gvInfo.Rows[e.NewEditIndex].FindControl("ddlItem4");
    //实例化DropDownList控件
                    Item1.SelectedValue = item1.Text;
                    Item2.SelectedValue = item2.Text;
                    Item3.SelectedValue = item3.Text;
                    Item4.SelectedValue = item4.Text;//将label中的值给下拉框
             }

    这样,当我们点击编辑的时候,下拉框中的值就是之前label中值,不会因为页面的刷新返回第一条数据。

    (二)

    当我们点击的更新的时候,同样希望下拉框中的值不会因为页面刷新而改变,因为在更新的时候,我们会将新数据插入到数据库中;

     protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                MonitorService ms = new MonitorService();
                playerInfo pI = new playerInfo();//自定义的实体类
                TextBox name = (TextBox)(gvInfo.Rows[e.RowIndex].FindControl("txtname"));
                DropDownList itme1 = (DropDownList)(gvInfo.Rows[e.RowIndex].FindControl("ddlItem1"));
                DropDownList itme2 = (DropDownList)(gvInfo.Rows[e.RowIndex].FindControl("ddlItem2"));
                DropDownList itme3 = (DropDownList)(gvInfo.Rows[e.RowIndex].FindControl("ddlItem3"));
                DropDownList itme4 = (DropDownList)(gvInfo.Rows[e.RowIndex].
    FindControl("ddlItem4"));//实例化下拉框
                TextBox remarks = (TextBox)(gvInfo.Rows[e.RowIndex].FindControl("txtRemarks"));
                string id = Convert.ToString(gvInfo.DataKeys[e.RowIndex].Value.ToString());
                pI.Stu_id = id;
                pI.Stu_name = name.Text;
                pI.Item1 = itme1.Text;
                pI.Item2 = itme2.Text;
                pI.Item3 = itme3.Text;
                pI.Item4 = itme4.Text;//将下拉框的值给实体类中的项
                pI.Remarks = remarks.Text;
                ms.updateInfo(pI);//更新表中数据
                gvInfo.EditIndex = -1;
                ManInfoBind();
            }

    这样,我们解决了开篇说的那些问题。

  • 相关阅读:
    用WebStorm运行Vue项目
    秋招圆满结束
    最新的秋招进度 10月21号
    来更新一下秋招的进度~
    华为三面完进池子啦~9月17日
    C++ 迭代器失效问题
    C++类相关问题(空类、多态、重载、继承)
    C++各种变量、类型的存储位置
    写一个面试中场景题的总结
    明天要面阿里HR面了
  • 原文地址:https://www.cnblogs.com/huochangjun/p/1748952.html
Copyright © 2011-2022 走看看