0

我有一个下拉 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();
        }


    }
}
4

2 回答 2

1

这是与下拉列表绑定相关的非常常见的问题。每次加载页面时,您的列表都会绑定,这会将所选值作为下拉列表的第一项,因此您只需要在它不是像这样从服务器回发时绑定它。

if(!IsPostBack)
{
  joblist = util.getJobList(orgid);
  this.joblistdropdown.DataSource = joblist;
  joblistdropdown.DataTextField = "Jobtitle";
  joblistdropdown.DataValueField = "Id";
  this.joblistdropdown.DataBind();
}
于 2013-06-09T20:42:31.743 回答
0

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

干杯,加根

于 2013-06-09T23:13:56.700 回答