0

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?

Rubenisme
  • 787
  • 1
  • 8
  • 15

1 Answers1

1

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.

Stefan H
  • 6,635
  • 4
  • 24
  • 35
  • Seems weird that calling a constructor would not do the same things as it would when called from the aspx page. Initially I had set an ID as well but since rcbName was already set as ID, I don't think it is necessary. – Rubenisme Jul 12 '12 at 15:18
  • There are page specific values that are set on the control. Try debugging and looking at the control that you are passing in, and then compare that to a new RadComboBox() and see the difference. – Stefan H Jul 12 '12 at 15:20