Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Location
    California
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem setting which item is selected in a DropDownList when the page is loaded

    Hi,

    I'm using ReportViewer with asp.net 4.0 to display reports on the web. I have a DropDownList on a page that I'm using to redirect to other pages, so I need to have AutoPostBack for the DropDownList set to true so that the SelectedIndexChanged function is called.

    I also want to set the initial selected item in the DropDownList when the page is first loaded (or returned to from other pages). I've tried putting this into Page_Load and only doing it when Page.IsPostBack == false, but it's still writing over the user's choice when the user selects something.

    Thanks!

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,823
    Thanks
    42
    Thanked 199 Times in 198 Posts
    what does your
    Page.IsPostBack == false
    statement look like? I am guessing by the code you are doing VB and not C#
    if VB should look something like this
    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
    dropdownlist1.SelectedIndex = 0
    end If
    end Sub
    if C# should look like
    Code:
    protected void Page_Load(object sender, EventArgs e){
      if(!IsPostBack){
      dropdownlist1.SelectedIndex = 0;
      }
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Location
    California
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    I'm coding in C#. The code is

    protected void Page_Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack == false)
    {
    ddlGroupBy.Items[0].Selected = true;
    ddlGroupBy.Items[1].Selected = false;
    ddlGroupBy.Items[2].Selected = false;
    }
    }


    I have since found another way around this problem: instead of having 3 aspx pages to go to 3 reportviewers that all take the same data inputs, I set the reportviewer's LocalReport.ReportPath to whichever reportviewer I want.

    The odd thing about this is: when I went back to using 3 aspx pages to match my orignial question, I no longer had the problem where Load was getting called with IsPostBack = false when it was overwriting the user's choice! Thanks for your help!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •