Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# ASP.NET Dynamically Changing the Datasource of a ListBox

Status
Not open for further replies.

Khold

MIS
Jun 22, 2008
25
GB
Hi all,

I've been stuck on this error for a harrowing amount of time, so I thought I'd bring my lamentations here. I'll try here to provide as much information as possible.

I have two buttons with the following code in my ASP.NET page:

Code:
  protected void AddUsersBtn_Click(object sender, EventArgs e)
    {
        String opName = "add";
        String grpId = GroupList.SelectedItem.ToString();
        String url = "UserToGroupListDialog.aspx?Operation="+opName+"&SelectedGrp="+grpId;
        String execScript = "callPopup('" + url + "');";
        
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "addUsersToGrpsDialog", execScript, true);
 
    }
    protected void RemoveUsersBtn_Click(object sender, EventArgs e)
    {
        String opName = "remove";
        String grpId = GroupList.SelectedItem.ToString();
        String url = "UserToGroupListDialog.aspx?Operation="+opName+"&SelectedGrp="+grpId;
        String execScript = "callPopup('" + url + "');";

        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "removeUsersFromGrpsDialog", execScript, true);

    }

Now, the purpose of these buttons is to open up another .aspx page with a listbox that is databound to a datasource depending on the operation (the operations 'add' or 'remove' are passed as part of the query string).

When I click the add users button, everything works fine and the list gets populated as expected. However, if I click on the remove users button, the listbox does not populate.

I have two datasource controls on the page to which the buttons are redirecting the user (UserToGroupListDialog.aspx i.e.). Here is the code for what I'm doing on page load:

Code:
 protected void Page_Load(object sender, EventArgs e)
    {

        String opName = Request.QueryString["Operation"];
    
        if (!Page.IsPostBack)
        {
            Page.ClientScript.RegisterClientScriptInclude("functions", "js/showDialog.js");

            if (opName.Equals("add"))
            {
                AvailUsersList.DataSourceID = "TXDatabaseAvailableUsersDataSource";
                AvailUsersList.DataTextField = "UserName";
                AvailUsersList.DataValueField = "UserName";
                AvailUsersList.DataBind();
            }

            if (opName.Equals("remove"))
            {

                AvailUsersList.DataSourceID = "TXDatabaseRemoveAvailableUsersDataSource";
                AvailUsersList.DataTextField = "UserName";
                AvailUsersList.DataValueField = "UserName";
                AvailUsersList.DataBind();
            }
     

            SelectedGrpLbl.Text = Request.QueryString["SelectedGrp"].ToString(); // set selected group
            
            if (SelectedGrpLbl.Text == null || SelectedGrpLbl.Text == "")
                SelectedGrpLbl.Text = "Error!";

            CancelOperationBtn.Attributes.Add("onclick", "window.opener = self; window.close();");

        }

    }

As you can see, I'm doing exactly the same thing with the listbox, except that I'm changing the datasource to which it is going to be bound. I have checked the queries that are configured to the datasource controls and they work as expected (they both run stored procedures to get the dataset that needs to be populated onto the listbox).

There is no problem with the query string either and the name-value pairs get passed on to the page as expected. I also tested whether the "remove" if condition is checked and entered into when the redirection happens, and it does.

So why isn't the remove users datasource control populating the listbox when the remove users button is pressed on the parent page? What am I missing here?

Appreciate your guidance on this as I cannot fathom what the source of the problem is.

Thank you. Will be glad to provide more information as necessary.
 
sorry i dont have a direct answer, but i can toss a few questions your way to maybe help determine the error.

1. SqlDataSource is early binding, so the control might be getting populated before the page_load event. Try putting the code in the Page_Init event?

2. Does AvailUsersList have a default DataSourceID specified in your html? that might prove the above. Change it to the other ds ID and see if it alternates what is shown.
 
I figured the error after coming back to office the next day. It was an error in the parameter source. I was pointing it to the wrong label!

Thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top