0

I have a drop down web server control which is binded with datasource from database ..when i select a value it always return value of first item in list on button click please resolve this issue..here is the code using

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 Answers 2

1

It's very common issue related to binding of dropdown list. Here your list is binding every time your page loads which makes selected value as first item of the dropdown, So you just need to bind it when it is not postback from server like this.

if(!IsPostBack)
{
  joblist = util.getJobList(orgid);
  this.joblistdropdown.DataSource = joblist;
  joblistdropdown.DataTextField = "Jobtitle";
  joblistdropdown.DataValueField = "Id";
  this.joblistdropdown.DataBind();
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it worked..Can you explain it aswell?? what was wrong is that?
yes @DanishAhmad I have also mentioned the details.Have a look. :)
THanks buddy i have accepted your answer sorry i was new to it ..i just did it & in future i shall work .Thanks for ur answer God bless you.
0

Whenever your page loads again(Post Back) the DropdownList gets binded with the DataSource Again and that Flushes the Selected Index of the DropDownList. Please put a Breakpoint on the Page_Load Event and run your Code. You will understand it

Cheers, Gagan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.