ASP.NET 在 Repeater 控件中查找下拉值第二部分
我对这个主题有一些疑问,仍然有问题。
我想从中继器控件内的多个下拉列表和文本框控件中查找值。
db.ConnectionString = SystemConnString
db.Open()
Dim selectedAdTitle As String = ""
Dim enteredAdFullName As String = ""
cmd.Parameters.Add(New SqlParameter("@TransactionID", TransactionID))
cmd.Parameters.Add(New SqlParameter("@AdTitle", selectedAdTitle))
cmd.Parameters.Add(New SqlParameter("@AdFullName", enteredAdFullName))
For i As Integer = 0 To myRepeater.Items.Count - 1
Dim AdTitle As DropDownList = DirectCast(myRepeater.Items(i).FindControl("AdTitle"), DropDownList)
Dim AdFullName As TextBox = DirectCast(myRepeater.Items(i).FindControl("AdFullName"), TextBox)
selectedAdTitle = AdTitle.Text
enteredAdFullName = AdFullName.Text
cmd.Parameters("@AdTitle").Value = selectedAdTitle
cmd.Parameters("@AdFullName").Value = enteredAdFullName
SQL = ""
SQL = SQL & "INSERT INTO InsuredPersons (TransactionID,Title,FullName) VALUES ("
SQL = SQL & "@TransactionID,"
SQL = SQL & "@AdTitle,"
SQL = SQL & "@AdFullName"
SQL = SQL & ")"
cmd.CommandText = SQL
cmd.Connection = db
cmd.ExecuteNonQuery()
Next
AdTitle 和 AdFullName 似乎没有带来这些值。 没有错误,所以他们发现控制正常。 下面是 ASPX 文件代码。
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="AdTitle" runat="server">
<asp:ListItem Selected="True" Value="" Text=""/>
<asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
<asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
<asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
<asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
<asp:ListItem Selected="False" Value="Other" Text="Other"/>
</asp:DropDownList>
<asp:TextBox ID="AdFullName" runat="server"></asp:TextBox>
</ItemTemplate>
编辑:
转发器是在页面加载时构建的
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
DirectCast 是在按钮单击上完成的
Protected Sub continueButtonDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles continueButtonDetails.Click
答案:必须将 IsPostback 放在转发器构建周围。
If Not IsPostBack() Then
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
End If
ive had a few questions on this subject, still having problems.
I want to find the values from a number of dropdown and textbox controls inside a repeater control.
db.ConnectionString = SystemConnString
db.Open()
Dim selectedAdTitle As String = ""
Dim enteredAdFullName As String = ""
cmd.Parameters.Add(New SqlParameter("@TransactionID", TransactionID))
cmd.Parameters.Add(New SqlParameter("@AdTitle", selectedAdTitle))
cmd.Parameters.Add(New SqlParameter("@AdFullName", enteredAdFullName))
For i As Integer = 0 To myRepeater.Items.Count - 1
Dim AdTitle As DropDownList = DirectCast(myRepeater.Items(i).FindControl("AdTitle"), DropDownList)
Dim AdFullName As TextBox = DirectCast(myRepeater.Items(i).FindControl("AdFullName"), TextBox)
selectedAdTitle = AdTitle.Text
enteredAdFullName = AdFullName.Text
cmd.Parameters("@AdTitle").Value = selectedAdTitle
cmd.Parameters("@AdFullName").Value = enteredAdFullName
SQL = ""
SQL = SQL & "INSERT INTO InsuredPersons (TransactionID,Title,FullName) VALUES ("
SQL = SQL & "@TransactionID,"
SQL = SQL & "@AdTitle,"
SQL = SQL & "@AdFullName"
SQL = SQL & ")"
cmd.CommandText = SQL
cmd.Connection = db
cmd.ExecuteNonQuery()
Next
AdTitle and AdFullName dont seem to be bringing across the values. There is no error so they have found the control ok. Below is the ASPX file code.
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="AdTitle" runat="server">
<asp:ListItem Selected="True" Value="" Text=""/>
<asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
<asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
<asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
<asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
<asp:ListItem Selected="False" Value="Other" Text="Other"/>
</asp:DropDownList>
<asp:TextBox ID="AdFullName" runat="server"></asp:TextBox>
</ItemTemplate>
Edit:
Repeater is constructed on page load
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
DirectCast is done on button click
Protected Sub continueButtonDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles continueButtonDetails.Click
Answer: Had to put IsPostback around Repeater Construction.
If Not IsPostBack() Then
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您想要:
而不是
另外,为什么您有两个 ItemTemplates? 我不知道您甚至可以这样做...
您是否在页面生命周期的任何其他时刻与任何文本框或下拉菜单进行交互?
尝试在转发器数据绑定周围进行回发检查。 我认为发生的事情是动态加载控件,因此它们没有视图状态,因此值将始终为空。
First off I think you want:
Rather Than
Also why do you have two ItemTemplates? I didn't know you could even do that...
Are you interacting with any of the TextBoxes or DropDowns at any other point during the page lifecycle?
Try putting a PostBack check around the repeater databinding. I think whats happening is your loading the controls dynamically, therefore they have no viewstate, therefore the values will always be empty.
确保您在页面生命周期的正确位置运行代码。 如果您做得太早(例如在 OnInit 中),那么它还没有来自客户端的值。 尝试将其移至 OnCommand()/OnSubmit() 事件,看看会发生什么。 以下是有关 ASP.NET 页面生命周期的一些参考:
Make sure your are running your code at the correct point in the page life cycle. If you are doing it too early (for example in the OnInit) then it won't have the values from the client yet. Try moving it to your OnCommand()/OnSubmit() event and see what happens. Here are some references on ASP.NET Page Life Cycle: