在 Gridview TemplateField 内查找 Web 控件时出现问题

发布于 2024-08-01 21:40:56 字数 1874 浏览 3 评论 0原文

好的,所以我在更新 GridView 时获取 TemplateField 内的 DropDownList 的值时遇到问题。 最初,我使用 RowCommand 事件来检查命令名称,然后执行适当的任务(更新/删除),但我在事件触发两次时遇到问题,因此我将其切换为单独的事件(RowUpdating、RowDeleting)。 执行此操作后,FindControl 每次都会返回 null。 仅供参考,gridview 位于 UpdatePanel 内部,该 UpdatePanel 具有用于 RowEditing、RowUpdating 和 RowDeleting 事件的 AsyncPostBackTriggers。

这是 GridView 内部的 TemplateField:

<asp:TemplateField HeaderText="Type">
    <ItemTemplate>
        <asp:Label 
            ID="lblMedTypeEdit" 
            Text='<%# Bind("medDesc") %>' 
            runat="server">
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList 
            ID="ddlMedTypeEdit" 
            DataSourceID="srcMedTypes" 
            SelectedValue='<%# Bind("medtype") %>' 
            runat="server" 
            DataTextField="medDesc" 
            DataValueField="medCode">
        </asp:DropDownList>                             
    </EditItemTemplate>
</asp:TemplateField>

这是我在内部使用的代码

Protected Sub gvCurrentMeds_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCurrentMeds.RowUpdating
    Dim intRowIndex As Integer = e.RowIndex
    Dim ddlMedType As DropDownList = CType(Me.gvCurrentMeds.Rows(intRowIndex).Cells(1).FindControl("ddlMedTypeEdit"),DropDownList)
End Sub

我还尝试使用递归函数来查找控件(如下),但它仍然返回 null。

Public Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
    If root.ID = id Then
        Return root
    End If

    For Each c As Control In root.Controls
        Dim t As Control = FindControlRecursive(c, id)
        If Not t Is Nothing Then
            Return t
        End If
    Next
    Return Nothing
End Function

Okay, so I'm having issues getting the value of a DropDownList that's inside of a TemplateField when updating my GridView. Originally I was using the RowCommand event to check the command name and then performing the appropriate task (update/delete), but I had problems with the event firing twice, so I switched it out for separate events (RowUpdating, RowDeleting). After doing this, FindControl is returning null every time. Just FYI, the gridview is inside of an UpdatePanel that has an AsyncPostBackTriggers for RowEditing, RowUpdating and RowDeleting events.

Here's my TemplateField inside of the GridView:

<asp:TemplateField HeaderText="Type">
    <ItemTemplate>
        <asp:Label 
            ID="lblMedTypeEdit" 
            Text='<%# Bind("medDesc") %>' 
            runat="server">
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList 
            ID="ddlMedTypeEdit" 
            DataSourceID="srcMedTypes" 
            SelectedValue='<%# Bind("medtype") %>' 
            runat="server" 
            DataTextField="medDesc" 
            DataValueField="medCode">
        </asp:DropDownList>                             
    </EditItemTemplate>
</asp:TemplateField>

Here is the code I'm using inside of

Protected Sub gvCurrentMeds_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCurrentMeds.RowUpdating
    Dim intRowIndex As Integer = e.RowIndex
    Dim ddlMedType As DropDownList = CType(Me.gvCurrentMeds.Rows(intRowIndex).Cells(1).FindControl("ddlMedTypeEdit"),DropDownList)
End Sub

I also tried using a recursive function to find the control (below), but it is still returning back null.

Public Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
    If root.ID = id Then
        Return root
    End If

    For Each c As Control In root.Controls
        Dim t As Control = FindControlRecursive(c, id)
        If Not t Is Nothing Then
            Return t
        End If
    Next
    Return Nothing
End Function

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

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

发布评论

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

评论(1

故乡的云 2024-08-08 21:40:56

如果您只想知道下拉列表的新值是什么,则已在传递给事件处理程序的 GridViewUpdateEventArgs 对象的 NewValues 属性中为您提供了该值。

在您的示例中,e.NewValues["medtype"] 应该是更新后的值。

您已经在下拉列表中指定了 <%# Bind(...) %>,因此 ASP.NET 将为您查找控件并获取新值 - 您不必自己探索控制层次结构。

If you just want to know what the new value of the dropdown is, this is already provided for you in the NewValues property of the GridViewUpdateEventArgs object passed to the event handler.

In your example, e.NewValues["medtype"] should be the updated value.

You've already specified <%# Bind(...) %> on the dropdown, so ASP.NET will do the work of finding the controls and getting the new values for you - you don't have to plumb the control hierarchy yourself.

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