保持dropdownlist选中值

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://linuxstyle.blog.csdn.net/article/details/4194627

RowDataBound事件

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;

       
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFFCC'");
            
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

            }
        }
        e.Row.Attributes["style"] = "Cursor:hand";

 

        //问题分类下拉框绑定选中值
        if (((DropDownList)e.Row.FindControl("ddlProVar")) != null)
        {
            DropDownList ddlProVar = (DropDownList)e.Row.FindControl("ddlProVar");

            SetSelectedItem(ddlProVar, ((HiddenField)e.Row.FindControl("hdProVarState")).Value);
        }
     

}

 

   /// <summary>
    /// 保存选定的dropdownlist值
    /// </summary>
    /// <param name="list"></param>
    /// <param name="value"></param>
    private void SetSelectedItem(System.Web.UI.WebControls.ListControl list, String value)
    {
        foreach (ListItem e in list.Items)
        {
            if (e.Text == value)
            {
                e.Selected = true;
                return;
            }
            else e.Selected = false;
        }
    }

展开阅读全文

dropdownlist中值问题?

09-29

用各种方法写dropdownlist 绑定数据库值,都没问题。rn如:rn[code=C#]rn public void ddlBind()rn rn //绑定公司dropdownlistrn List xzhrmodel = new List();rn xzhrmodel = HrSQL.GetQia();rn DropDownList1.Items.Clear();rn DropDownList1.Items.Add(new ListItem("请选择", ""));rn foreach (QiaModel qia in xzhrmodel)rn rn ListItem li = new ListItem();rn li.Value = qia.qia_mail;rn li.Text = qia.qia_name;rn DropDownList1.Items.Add(li);rn rn [/code]rn或是rn[code=C#] List xzhrmodel = new List();rn xzhrmodel = HrSQL.GetQia();rn ddl_pia.DataSource = xzhrmodel;rn ddl_pia.DataTextField = "qia_name";rn ddl_pia.DataValueField = "qia_mail";rn ddl_pia.DataBind();rn[/code]rn以上都没问题,正常。rn但是,我还要,根据值绑定选定的值就有问题了。rnthis.ddl_pia.SelectedItem.Text = Bmodel.B_qia.ToString();rn这样写没问题,但他的显示出来的效果是,值始终放在最上面,如 B ABCD 或 C ABCD... 会重复(不知有看懂没)rn表面上是实现了效果,但 rnBmodel.B_qia = this.ddl_pia.SelectedItem.Text.ToString();rnBmodel.B_qiamail = this.ddl_pia.SelectedValue.Trim();rn//取值时,selectvalue值是空值,除非我另外做选择,到是都有值的。rnrnrn然后我又换另外几种写法rn第一种:rn int index = ddl_pia.SelectedIndex;rn for (int i = 0; i < ddl_pia.Items.Count; i++)rn rn if (ddl_pia.Items[i].Value == Bmodel.B_qia.ToString())rn rn ddl_pia.Items[index].Selected = false;rn ddl_pia.Items[i].Selected = true;rn break;rn rn rn结果没报错,但效果 没有选定我要的值。rn第二种:rnthis.ddl_pia.SelectedValue = Bmodel.B_qiamail.ToString();rn结果和第一种一样,没有选定我要的值。rn第三种:rnDropDownList1.Items.FindByValue(Bmodel.B_qia.ToString()).Selected = true; rn报错“未将对象引用设置到对象的实例。”rn以上AutoPostPack true/false 都有试过。rn rn这个问题困扰我好久了,谁能帮我解决?rnrn很奇怪,另外其他也有写类色的却是正常的rn //绑定酒店数据rn List hotel = new List();rn hotel = HotelSQL.GetHotleName();rn ddl_hotel.DataSource = hotel;rn ddl_hotel.DataTextField = "hotel_name";rn ddl_hotel.DataValueField = "hotel_name";rn ddl_hotel.DataBind();rn this.ddl_hotel.SelectedValue = Cmodel.C_hotel;rn 论坛

获取dropDownList中值的问题【求助】

05-27

[code=C#]rn protected void Drop_Bind_DiQu()rn rn XQ.BLL.TRecruitmentTypeBLL rectypebll = new XQ.BLL.TRecruitmentTypeBLL();rn ddl_DiQu.DataSource = rectypebll.GetList("FTypeId=3");rn ddl_DiQu.DataTextField = "FRecTypeName";rn ddl_DiQu.DataValueField = "FID";rn ddl_DiQu.DataBind();rn //ddl_DiQu.Items.Insert(0, new ListItem("-请选择地区类别-", "0"));rn rn protected void Drop_Bind_HangYe()rn rn XQ.BLL.TRecruitmentTypeBLL rectypebll = new XQ.BLL.TRecruitmentTypeBLL();rn ddl_HangYe.DataSource = rectypebll.GetList("FTypeId=2");rn ddl_HangYe.DataTextField = "FRecTypeName";rn ddl_HangYe.DataValueField = "FID";rn ddl_HangYe.DataBind();rn //ddl_HangYe.Items.Insert(0, new ListItem("-请选择行业类别-", "0"));rn rn protected void Drop_Bind_ZhiWei()rn rn XQ.BLL.TRecruitmentTypeBLL rectypebll = new XQ.BLL.TRecruitmentTypeBLL();rn ddl_ZhiWei.DataSource = rectypebll.GetList("FTypeId=1");rn ddl_ZhiWei.DataTextField = "FRecTypeName";rn ddl_ZhiWei.DataValueField = "FID";rn ddl_ZhiWei.DataBind();rn //ddl_ZhiWei.Items.Insert(0, new ListItem("-请选择职位类别-", "0"));rn rnrn protected void ImageButton1_Click(object sender, ImageClickEventArgs e)rn rn DiQu = ddl_DiQu.SelectedValue;rn HangYe = ddl_HangYe.SelectedValue;rn ZhiWei = ddl_ZhiWei.SelectedValue;rn string UserOne = this.ReplaceBadChar(this.txt_User.Text.Trim());rnrn Response.Redirect("Job/JobList.aspx?DiQu=" + DiQu + "&&HangYe=" + HangYe + "&&ZhiWei=" + ZhiWei + "&&UserTxt=" + UserOne + "");rnrn rn[/code]rnrn我要在点击ImageButton1_Click中获取上面三个dropdownlist选中的值,而我这样写传到另一个页面只能获取每个dropdownlist中的第一个值,别的却获取不到。是怎么回事啊 论坛

DropDownList控件 选中值没法正确显示--在线等

12-04

求教各位大侠,为什么我建的DropDownList控件没法正确显示选中值。代码如下rn 前台相关代码:rnrn rn rn [color=#FF0000]rn rn [/color] rnrn 后台相关代码:rn protected void Page_Load(object sender, EventArgs e)rn rn if (!IsPostBack)rn rn string sqlProMngLev = "select * from tbProMngPara where ParaName='级别'";rn SqlDataAdapter daProMngLev = new SqlDataAdapter(sqlProMngLev, con);rn DataSet dsProMngLev = new DataSet();rn con.Open();rn daProMngLev.Fill(dsProMngLev);rn [color=#FF0000] DdlPro01Level.DataSource = dsProMngLev;rn DdlPro01Level.DataTextField = "ParaDtl";rn DdlPro01Level.DataValueField = "ParaId";[/color] DdlPro01Level.DataBind();rn con.Close();rnrn int edtId = Convert.ToInt32(Request.QueryString["edt_id"]);rn string sqlPro01Up = "select Pro01ID,Pro01Name,Pro01Level,Pro01Sort,Pro01Director,Pro01State,Pro01AddTime,replace(CONVERT(Char(10),Pro01FromTime,111),'/','-') as Pro01FromTime,replace(CONVERT(Char(10),Pro01PreEndTime,111),'/','-') as Pro01PreEndTime,replace(CONVERT(Char(10),Pro01RealEndTime,111),'/','-') as Pro01RealEndTime,Pro01Remark from tbPro01 where Pro01ID='" + edtId + "'";rn SqlDataAdapter daPro01Up = new SqlDataAdapter(sqlPro01Up, con);rn DataSet dsPro01Up = new DataSet();rn con.Open();rn daPro01Up.Fill(dsPro01Up);rn if (dsPro01Up.Tables[0].Rows.Count != 0)rn rn TxtPro01ID.Text = dsPro01Up.Tables[0].Rows[0].ItemArray[0].ToString();rn TxtPro01Name.Text = dsPro01Up.Tables[0].Rows[0].ItemArray[1].ToString();rn [color=#FF0000]DdlPro01Level.SelectedIndex = DdlPro01Level.Items.IndexOf(DdlPro01Level.Items.FindByText(dsPro01Up.Tables[0].Rows[0].ItemArray[2].ToString()));[/color]rn(我看网上都是这么写的,为什么我写下来就不对呢)rn 。。。 rn con.Close();rn rn rn rn 比较急,在线等,谢谢大家啦。。。rn 论坛

DropDownList中值不能改变,求助!

08-16

1.问题描述:rn1)在Default.aspx页中用GridView + SqlDataSource取出表student中的数据,并且启用“选定内容”;rn 表student结构:rn sno(主键) nvarchar(10) --学生学号rn name nvarchar(50) --学生姓名rn sex nchar(10) --学生性别rn age smallint --学生年龄rn2)在Default.aspx页的GridView下,用一个HyperLink控件转到编辑页面EditStudentInfo.aspx,其Text格式为“编辑学生 xxx”,NavigateUrl格式为“EditStudentInfo.aspx?sno=xxxx”;rn3)在EditStudentInfo.aspx页面中,含有一些Label和TextBox,以及一个DropDownList,用来编辑(更新)学生信息,其中DropDownList用来显示和选择学生性别;rn3)在EditStudentInfo.aspx.cs里,创建数据库连接,用SQL命令取出sno为特定值的学生的信息;rn[code=C#]string sno = Request.Params["sno"].ToString();rnstring querySql = "SELECT [name], [sex], [age] FROM [student] WHERE [sno]=" + sno;rn[/code]rn之后将对应信息存到相应TextBox和DropDownList中;rn[code=C#]ddlSex.Text = reader.getString(1)[/code]rn[color=#FF0000]4)好,到这里问题来了:无论在GridView中选择的那一行学生信息,其性别是男还是女,在转到编辑页面时,性别(DropDownList的值)总显示为“男”,搞不懂原因啊。。。[/color]5)我甚至在编辑页面中添加了一个Label1用来测试从数据库中取出的sex字段的值rn[code=C#]Label1.Text = reader.GetString(1);[/code]rn结果Label1可以显示正确的性别 - -|||rn2.程序截图rn1)Default.aspx页面rn[img=http://hi.csdn.net/attachment/201108/16/0_1313457482kBUa.gif][/img]rn2)EditStudentInfo.aspx页面rn[img=http://hi.csdn.net/attachment/201108/16/0_131345755866ls.gif][/img]rn最下面那个“女”就是Label1的值rn3.程序代码rn1)EditStudentInfo.aspx页面rn[code=HTML]rn rn rn rn rn rn rn rn rn rn rn rn 男rn 女rn rn rn rn rn rn rn rn rn rn rn rn rn rn[/code]rn2)EditStudentInfo.aspx.cs代码文件rn[code=C#]public partial class EditStudentInfo : System.Web.UI.Pagernrn SqlConnection conn = null;rnrn protected void Page_Load(object sender, EventArgs e)rn rn if (!IsPostBack)rn rn string connString = ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString;rn conn = new SqlConnection(connString);rn string sno = Request.Params["sno"].ToString();rnrn tryrn rn conn.Open();rn string querySql = "SELECT [name], [sex], [age] FROM [student] WHERE [sno]=" + sno;rn SqlCommand queryCmd = new SqlCommand(querySql, conn);rn SqlDataReader reader = queryCmd.ExecuteReader();rn while (reader.Read())rn rn txtSno.Text = sno;rn txtName.Text = reader.GetString(0);rn Label1.Text = reader.GetString(1);rn ddlSex.Text = reader.GetString(1);rn txtAge.Text = reader.GetInt16(2).ToString();rn rn rn catch (SqlException sqle1)rn rn Console.WriteLine(sqle1.Message);rn rn rn rn[/code] 论坛

没有更多推荐了,返回首页