dropdown selected index

Discussion in 'C#' started by trimzme, May 8, 2008.

  1. #1
    Hi guys,

    I careted dynamic drpdown in asp page. when i change dropdown value i want to reload the page. when realod the page i don't relod dropdown. in drop down i just want chaged value. can any one explain how to this one and can any one give samplecode for this one ASAP?

    Example:

    when page load i have some x value. now selected another value some y. After that when i submit the page dropdown value comes x. But i want to display one y .

    Thanks
     
    trimzme, May 8, 2008 IP
  2. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #2
    You can set the dropdownlist's AutoPostBack property to true to cause the post back.

    Then simply only bind the drop down data if it is not a post back. ASP.NET has a built in variable for this called IsPostBack.

    this.DropDown1.AutoPostBack = true;
    
    if(!IsPostBack)
    {
        BindDropDownListData();
    }
    Code (markup):
     
    dgxshiny, May 8, 2008 IP
  3. trimzme

    trimzme Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply! I am new to asp. In asp can give some complete sample code.(with dropdown creation,binding, and selectedindex event)
    Thanks
     
    trimzme, May 8, 2008 IP
  4. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #4
    
    protected void Page_Load( object sender, EventArgs e )
    {
            If(!IsPostBack)
                 BindDropDown();
    
            BindData();
    }
    
    private void BindDropDown()
    {
            this.ddCats.DataSource = GetDataToBind();
            this.ddCats.DataBind();
    }
    
    private void BindData()
    {
            //bind the data on your page
    }
    
    
    Code (markup):
     
    dgxshiny, May 8, 2008 IP
  5. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #5
    Within the BindData() event you can bind other controls on the page using

    
    if(this.DropDownMenu.SelectedIndex == 0)
         dothis;
    
    Code (markup):
    or

    
    if(this.DropDownMenu.SelectedValue = "Red")
         dothis;
    
    Code (markup):
     
    dgxshiny, May 8, 2008 IP