【问题标题】:Where is my dynamic control after I submit the form?提交表单后我的动态控件在哪里?
【发布时间】:2016-12-12 18:35:22
【问题描述】:

我用转发器创建了一个动态表单。

<asp:repeater ID="fieldRepeater" OnItemDataBound="dataBound" runat="server">
    <itemtemplate>
        <div id="controlRow" class="row" runat="server">
            <div id="testContainer" class="col-md-2" runat="server">

            </div>
        </div>
    </itemtemplate>
</asp:repeater>

在代码隐藏中,我创建了我想出现在其中的字段。

Protected Sub dataBound(ByVal sender As Object, e As RepeaterItemEventArgs)
    dim temp as new DropDownList
    dim tempLabel as new label
    Dim testContainer As HtmlGenericControl = e.Item.FindControl("testContainer")

    'createField
    temp.ID = "testField" & e.Item.ItemIndex 'this is testField0
    temp.Items.Add(New ListItem("Not Used", 0))
    temp.Items.Add(New ListItem("Used", 1))
    temp.CssClass = "form-control"


    'createLabel
    tempLabel.ID = "testFieldLabel" & e.Item.ItemIndex
    tempLabel.AssociatedControlID = "testField" & e.Item.ItemIndex
    tempLabel.Text = dr("controlLabel")
    testContainer.Controls.Add(temp)
    testContainer.Controls.Add(tempLabel)
end sub

表单运行良好,我什至可以使用数据库中的数据预先填充它,但在我的提交处理程序中,我的控件不存在:

Protected Sub submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submit.Click
    Dim i As Int32 = 0
    For Each r As DataRow ...
        'db stuff
        Dim temp As New DropDownList
        temp = Me.FindControl("testField" & i.ToString)
        'db stuff            
        i += 1
    next
end sub

temp = Me.FindControl("testField" &amp; i.ToString) 总是nothing 有人可以帮我找出原因吗?

我的 HTML 输出如下所示:

<div id="cntMain_fieldRepeater_testContainer_0" class="col-md-2">
    <label for="cntMain_fieldRepeater_testField0_0" id="cntMain_fieldRepeater_testFieldLabel0_0" style="color:#007AFF;">Activity</label>
    <select name="ctl00$cntMain$fieldRepeater$ctl00$testField0" id="cntMain_fieldRepeater_testField0_0" class="form-control">
        <option value="0">Not Used</option>
        <option value="1">Used</option>
    </select>
</div>

【问题讨论】:

    标签: asp.net vb.net webforms code-behind


    【解决方案1】:

    需要在每次页面加载时(重新)创建动态创建的控件,其中包括回发。所以你必须将Repeater的DataBinding移到!IsPostBack检查之外。

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        fieldRepeater.DataSource = loadDataHere
        fieldRepeater.DataBind
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As Integer = 0
        For Each item As RepeaterItem In fieldRepeater.Items
            Dim temp As DropDownList = CType(item.FindControl(("testField" + i.ToString)),DropDownList)
            temp.BackColor = Color.Red
            i = (i + 1)
        Next
    End Sub
    

    【讨论】:

    • 我快到了(有点)!我已经把它归结为fieldRepeater.item(i).findControl("testField"&amp;i) 谢谢你的帮助。
    • 不客气。但是您也可以将控件直接放在itemtemplate 中。这样您就不需要动态地重新创建它们。
    • 理论上是的,但是因为元素在中继器中,所以您实际上不必这样做。它们被分配了一个唯一的名称和 ID。你可以在Button1_ClickDim temp As DropDownList = CType(item.FindControl(("DropDownList1")),DropDownList) 中完全一样地访问它们
    • PS 如果我的 VB 看起来有点奇怪,我使用 C# > VB 的代码翻译器
    • 我一直忘记我不必像那样对表单字段进行微观管理。 10 年的冷聚变必须从头开始构建该领域,这与 asp classic 和滥用请求范围没有太大区别。我会按照你的建议重写它。再次感谢
    猜你喜欢
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2019-03-14
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多