ASP.NET:从 DataList 中的 EditItemTemplate 访问 WebControl

发布于 2024-09-12 19:29:22 字数 4552 浏览 9 评论 0原文

我正在尝试从 DataList 的 EditItemTemplate 访问 Web 控件(文本框),以便我可以更改它。当我尝试执行 DataList.FindControl("TextboxID") 时,它返回 null,因为它无法识别文本框已呈现。我尝试查看 DataBinding、DataBound 事件,但它们也不起作用。

更具体地说,当用户使用日历控件时,我需要更改文本框的值,因此我需要在 Calendar_SelectionChanged 事件中从 EditItemTemplate 访问该控件。

有人有任何想法或解决方法吗?谢谢!

代码:

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>

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" /> <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-09-19 19:29:22

你的 calDateEdit 上有visible = false。您是否在任何地方将其设置为 true ?否则它不会被渲染。

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

⒈起吃苦の倖褔 2024-09-19 19:29:22

这不是世界上最干净的东西,但我设法通过在“编辑模式”下加载我的页面来获得它,以便我想要更改的文本框可见。然后右键单击 -->查看源代码,然后向下滚动到我的文本框并获取它的 id,如下所示:“ctl00$Content$DataList$ctl00$txtContractStart”

然后我在日历选择更改事件中执行了以下代码:

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

此代码找到了文本框成功地。我希望这对其他人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文