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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList within SelectedItem Template.... 1

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
0
0
US
Greetings,

Can someone please clue me on how to load data into a DropDownList that is located with a SelectedItem Template? The ItemTemplate contains a LinkButton. However, attempting to populate the DropDownList prior to the LinkButton's Click event ending will produce the "...not set to the instance of an object..." error.

I thought I could put something in the Page_Load() event, but I can't reference a control that, technically, doesn't exist yet.

Code:
<asp:DataList ID="dlst_Test" OnItemCommand= "dlstTest_ItemCommand" Runat="Server">

<HeaderTemplate>    
  <table>
    <tr>
       <td>Requirement</td>
    </tr>
</HeaderTemplate>

<ItemTemplate>
    <tr>
        <td><asp:LinkButton Text='<%# Container.DataItem( "J_Name" ) %>' Runat="Server" /></td>
    </tr>
</ItemTemplate>

<SelectedItemTemplate>
     <tr>
         <td><asp:DropDownList id="drpJ_Name" OnSelectedIndexChanged="drpJ_Name_SelectedIndexChanged" AutoPostBack="True" Runat="Server">
             </asp:DropDownList></td>
     </tr>
</SelectedItemTemplate>

<FooterTemplate>
    </table>
</FooterTemplate>
</asp:DataList>

I understand how to do the actual database stuff. I just need to know how to correctly reference the DropDownList in order to load data into it.

Thanks:)
 
In the OnItemDataBound event for the DataList. Simply declare an event handler for that event, and w/in that event, you can do something like this:
Code:
switch(e.Item.ItemType)
{
  case ListItemType.SelectedItem:
    //get a reference to the object
    DropDownList dl = (DropDownList)e.Item.FindControl("drpJ_Name");
    //you can now bind it like normal
    dl.DataSource = getData();
    dl.DataTextField = "someField";
    dl.DataValueField = "someOtherField";
    dl.DataBind();
    break;
}//switch
You're right... the control doesn't exist in the Page_Load, which is why you must grab it during DataBinding of the DataList control.

:)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top