Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
in this code i am trying get value from dropdownlist and on button click show result on gridview. but problem is always select id=1 and show result against 1 value. please help me out.





C#
protected void Page_Load(object sender, EventArgs e)
        {


            DropDownList1.DataSource = rm.Rooms;
            DropDownList1.DataValueField = "roomid";
            DropDownList1.DataTextField = "roomname";
            DropDownList1.DataBind();

           

        }




        protected void Button1_Click(object sender, EventArgs e)
        {
            string selectvalue = DropDownList1.SelectedValue;
            int roomindex = Convert.ToInt32(selectvalue);

            Room selectroom = rm.Rooms.SingleOrDefault<Room>(room => room.roomid == roomindex);

            GridView1.DataSource = selectroom.Assets;
            GridView1.DataBind();
        }
Posted
Comments
An@nd Rajan10 18-Dec-13 4:04am    
what about solution 1 ?

Try like this

check for postback in page load..

C#
protected void Page_Load(object sender, EventArgs e)
       {

           if (!Page.IsPostBack)
           {
               DropDownList1.DataSource = rm.Rooms;
               DropDownList1.DataValueField = "roomid";
               DropDownList1.DataTextField = "roomname";
               DropDownList1.DataBind();
           }
       }
 
Share this answer
 
i think the all selectedvalue as 1.

anyway try this code

C#
string selectvalue = DropDownList1.SelectedItem;

or
C#
string selectvalue = DropDownList1.Text;


please comment after your result
 
Share this answer
 
v2
Try this:-
string selectvalue = DropDownList1.SelectedItem.value;
 
Share this answer
 
Add your code in ISPostBack.your page is getting postback on button click event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900