0
我用中继器创建了一个动态表单。我提交表单后,我的动态控件在哪里?
<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" & 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>
我几乎在那里(金田)!我已经把它放到'fieldRepeater.item(i).findControl(“testField”&i)'谢谢你的帮助。 – Travis
不客气。但是你也可以直接将控件放在'itemtemplate'中。这样你就不需要动态地重新创建它们。 – VDWWD
理论上是的,但是因为元素在Repeater中,所以你不会真的需要。他们被分配一个唯一的名称和ID。你可以在'Button1_Click'中完全访问它们:'Dim temp As DropDownList = CType(item.FindControl((“DropDownList1”)),DropDownList)' – VDWWD