views:

25

answers:

2

Hi all. I am trying to access a webcontrol (a Textbox) from the EditItemTemplate of a DataList, so I can change it. When I try to do DataList.FindControl("TextboxID") it comes back with null because it doesn't recognize the textbox has rendered. I've tried looking in the DataBinding, DataBound events and those don't work either.

To be more specific, I need to change the value of a textbox when the user uses a Calendar control, so I need to access the control from EditItemTemplate in the Calendar_SelectionChanged event.

Anyone have any ideas or workarounds? Thanks!

Code:

protected void calendar1_SelectionChanged(object sender, EventArgs e)
{
    // Access EditItemTemplate Control
}


<asp:DataList ID="DataListMaintenance" runat="server" 
                                        oncancelcommand="DataListMaintenance_CancelCommand" 
                                        oneditcommand="DataListMaintenance_EditCommand" 
                                        onupdatecommand="DataListMaintenance_UpdateCommand" 
                                        DataSourceID = "LMMaintDataSource" 
                                        ondeletecommand="DataListMaintenance_DeleteCommand">
                                    <EditItemTemplate>
                                        <table width = "100%" cellpadding = "2" cellspacing = "1">
                                            <tr>
                                                <td valign = "top">
                                                    <b>Contract Start Date:</b>
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="txtContractStart" runat="server" Text = '<%# Bind("ContractStartDate") %>'></asp:TextBox>
                                                    <% if (!calDateEdit.Visible)
                                                       { %>
                                                    <asp:LinkButton ID="linkChoose" runat="server" onclick="linkChoose2_Click">Choose</asp:LinkButton>
                                                    <%} %>
                                                    <% if (calDateEdit.Visible)
                                                       { %>
                                                    <asp:LinkButton ID="linkCancel" runat="server" onclick="linkCancel2_Click">Cancel</asp:LinkButton>
                                                    <%} %>
                                                </td>
                                                <td>
                                                    <asp:Calendar ID="calDateEdit" runat="server" Visible ="false" 
                                    onselectionchanged="calendar1_SelectionChanged">
                                     <SelectedDayStyle BorderColor="Blue" BorderStyle="Solid" />
                                 </asp:Calendar>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:Button ID="cmdUpdate" runat="server" Text="Update" CommandName = "Update" />&nbsp;<asp:Button ID="cmdCancel" runat="server" Text="Cancel" CommandName = "Cancel" />
                                                </td>
                                            </tr>
                                        </table>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <table width = "100%" cellpadding = "2" cellspacing = "1">
                                            <tr>
                                                <td valign = "top">
                                                    <b>Contract Start Date:</b>
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblStart" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ContractStartDate")%>'></asp:Label>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                    </asp:DataList>
A: 

You have visible = false on your calDateEdit. Are you setting it to true anywhere? It won't be rendered otherwise.

Daniel Dyson
Yes I'm setting it to true when I click the "Choose" button right next to it. protected void linkChoose2_Click(object sender, EventArgs e) { // Hide other calendar if it's visible if (calDateEdit.Visible) { calDatEdit.Visible = false; } calDateEdit.Visible = true; }
Confused
A: 

It's not the cleanest thing in the world, but I managed to get it by loading my page in the "Edit Mode" so that the Textbox I wanted to change was visible. Then doing a Right Click --> View Source, then scrolling down to my textbox and getting the id of it that looks something like this: "ctl00$Content$DataList$ctl00$txtContractStart"

I then did the following code in my Calendar Selection Changed Event:

TextBox txtContract = (TextBox)Page.FindControl("ctl00$Content$DataList$ctl00$txtContractStart");

This code found the textbox successfully. I hope this helps someone else.

Confused