下拉返回错误的值asp.net服务器控件

时间:2013-06-09 20:40:16

标签: c# asp.net .net

我有一个下拉式Web服务器控件,它与来自数据库的数据源绑定..当我选择一个值时,它总是返回按钮单击列表中第一项的值请解决此问题。使用的代码是

using JobPortel.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JobPortel
{
    public partial class receivedresume : System.Web.UI.Page
    {
        MemberDTO memberobj;
        int orgid;
        Utilities util;
        String jobidString;
        List<JobDto> joblist;
        protected void Page_Load(object sender, EventArgs e)
        {
            String UserName = (String)Session["UserName"];
            util = new Utilities();
            memberobj = util.getMember(UserName);
            orgid = util.getOrganizationIdFromMember(memberobj.ID);

                joblist = util.getJobList(orgid);
                this.joblistdropdown.DataSource = joblist;
                joblistdropdown.DataTextField = "Jobtitle";
                joblistdropdown.DataValueField = "Id";
                this.joblistdropdown.DataBind();
            }

            if (IsPostBack) {

                 jobidString = this.joblistdropdown.SelectedValue;
            }
        }
        protected void savebtn_Click(object sender, EventArgs e)
        {
            int jobid = 0;

             jobid = Convert.ToInt32(jobidString);


            List<MemberDTO> cvlist = new List<MemberDTO>();

            using (var db = new jobportaldatabaseEntities1()) {

                var query = from m in db.Members
                            join p in db.applyjobtables on m.Id equals p.applicantid
                            where p.jobid == jobid
                            select m;
                foreach (var item in query)
                {
                    MemberDTO member = new MemberDTO();
                    member.ID = item.Id;
                    member.FirstName = item.firstname;
                    member.Lastname = item.lastname;
                    member.Location = item.location;
                    member.Mobile = item.mobile;
                    member.Email = item.email;
                    cvlist.Add(member);
                }
            }
            this.cvgridview.DataSource = cvlist;
            this.cvgridview.DataBind();
        }


    }
}

2 个答案:

答案 0 :(得分:1)

这是与下拉列表绑定相关的非常常见的问题。每次页面加载时,你的列表都会绑定,这使得选择的值成为下拉列表的第一项,所以你只需要在没有像这样从服务器回发时绑定它。

if(!IsPostBack)
{
  joblist = util.getJobList(orgid);
  this.joblistdropdown.DataSource = joblist;
  joblistdropdown.DataTextField = "Jobtitle";
  joblistdropdown.DataValueField = "Id";
  this.joblistdropdown.DataBind();
}

答案 1 :(得分:0)

每当您的页面再次加载(Post Back)时,DropdownList会再次与DataSource绑定,并刷新DropDownList的Selected Index。 请在Page_Load事件上放置一个断点并运行您的代码。你会明白的

干杯, GAGAN