0

问题:如何确定哪个控件*触发了部分回发?

*control = 通过Repeater 控件动态生成的文本框。

高级:本质上我有一个封装了Repeater 控件的UpdatePanel。我使用转发器来动态添加文本框和占位符。

OnChange 其中一个文本框,我希望 UpdatePanel.Load Sub 能够确定哪个文本框发出请求,以便我可以将其他控件添加到其适当的占位符(也动态添加)。

目前我收到一个错误:“发现多个控件具有相同的 id 'X'。FindControl 要求控件具有唯一的 ID。

VB

Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Public RegisteredRows As New List(Of CustomRow)
    Public callbackResult As String = ""
    Public rptCount As Integer = 0

    Public Class CustomRow
        Public desc As String
    End Class

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles dynForm.Load
        Dim i As Integer
        For i = 0 To 3
            Dim cr As CustomRow = New CustomRow
            cr.desc = ""
            RegisteredRows.Add(cr)
        Next
        Repeater1.DataSource = RegisteredRows
        Repeater1.DataBind()

        Dim cm As ClientScriptManager = Page.ClientScript
        dynLit.Text = "<script type='text/javascript' language='javascript'> function HandleDynamic() { __doPostBack('" & btnDynamic.ClientID & "', null);}</script>"

    End Sub

    Protected Sub DataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If Not e.Item.DataItem Is Nothing Then

            'Dim hdnBtn As Button = e.Item.FindControl("btnDynamic")
            Dim tb As TextBox = e.Item.FindControl("asbText")

            tb.Attributes.Add("onchange", "HandleDynamic();")
            'tb.Attributes.Add("onchange", " __doPostBack('" & btnDynamic.ClientID & "', null);")
            Repeater1.Controls.Add(tb)

            If RegisteredRows Is Nothing Then
                RegisteredRows = New List(Of CustomRow)
            End If

        End If

    End Sub

    Protected Sub upDynamic_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim uPanel As UpdatePanel = CType(sender, UpdatePanel)
        If sender.ID = "upDynamic" Then

            For i As Integer = 0 To uPanel.Controls.Count - 1
                If TypeOf uPanel.Controls(i) Is TextBox AndAlso uPanel.Controls(i).ID = "asbText" Then
                    If CType(uPanel.Controls(i), TextBox).Text <> "" Then
                        Dim lbl As New Label
                        lbl.Text = "Comments"

                        Dim txt As New TextBox
                        txt.Width = 100
                        txt.Text = "Hello World"

                        Dim ph As New PlaceHolder
                        ph = CType(sender.FindControl("phDynamic"), PlaceHolder)

                        ph.Controls.Add(lbl)
                        ph.Controls.Add(txt)
                    End If
                End If
            Next
        End If
    End Sub
End Class

ASPX

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dyanmic Prototype</title>
</head>
<body>
    <form id="dynForm" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true" />
    <div>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td nowrap="nowrap">
                    <asp:Label ID="lblMain" runat="server" Text="Are we on?" ></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:UpdatePanel ID="upDynamic" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional"
                        OnLoad="upDynamic_Load">
                        <ContentTemplate>
                            <asp:Repeater ID="Repeater1" runat="server" EnableViewState="false" OnItemDataBound="DataBound">
                                <HeaderTemplate>
                                    <table>
                                        <tr>
                                            <td nowrap="nowrap">
                                                <asp:Label ID="lblText" runat="server"  Text="Offense" />
                                            </td>
                                        </tr>
                                    </table>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <table>
                                        <tr>
                                            <td>
                                                <tr>
                                                    <td>
                                                        <asp:TextBox ID="asbText" runat="server" AutoComplete="Off" AutoCompleteType="Disabled"
                                                            EnableViewState="false" Width="350px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <asp:PlaceHolder ID="phDynamic" runat="server" />
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:Button ID="btnDynamic" runat="server" Text="" Visible="false" />
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnDynamic" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

<asp:Literal runat="server" id="dynLit"></asp:Literal>
4

1 回答 1

0

您是否尝试为文本框设置 id?

Dim txt As New TextBox
txt.Width = 100
txt.Text = "Hello World"
txt.id="textbox"+i
于 2013-02-15T17:30:32.987 回答