Question

I would like to know the difference between returning a webcontrol and passing one. I have a webcontrol which is in my aspx code. Like

<asp:TableCell>
<telerik:RadComboBox runat="server" ID="rcbName"></telerik:RadComboBox>
</asp:TableCell>

Then I want to programmatically fill the combobox (shouldn't matter what kind of control it is).

So I had this function that returned a combobox:

private RadComboBox rcb CreateRcbSearchResults(DataSet ds)
{
    RadComboBox rcb = new RadComboBox();

    foreach (DataRow row in ds.Tables[0].Rows)
        rcb.Items.Add(new RadComboBoxItem(row["description"].ToString(), row["id"].ToString()));

    return rcb;
}

And then I would set the combobox:

rcbName = CreateRcbSearchResults(ds);

For some reason this doesn't work and it would just give me back an empty combobox (browser side), even though I saw it was filled when debugging.

A colleague looked at it and changed my function to pass the combobox:

private void CreateRcbSearchResults(RadComboBox rcb, DataSet ds)
{
    foreach (DataRow row in ds.Tables[0].Rows)
        rcb.Items.Add(new RadComboBoxItem(row["description"].ToString(), row["id"].ToString()));
}

Now I do:

CreateRcbSearchResults(rcbName, ds);

And now it works. I would like to know why this is. Can someone tell me?

Was it helpful?

Solution

There are a lot more properties that get set on the combobox when it is part of the page than just the things that you were setting. What you were doing was creating a completely new instance of a combobox, which has no ID and whatnot, and then replacing the instance that got generated by the page loading.

What you changed your code to passes the reference to the control that the page created and then you just added Items to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top