ASP.NET 在 Repeater 控件中查找下拉值第二部分

发布于 2024-07-22 05:29:18 字数 2701 浏览 4 评论 0原文

我对这个主题有一些疑问,仍然有问题。

我想从中继器控件内的多个下拉列表和文本框控件中查找值。

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 技术交流群。

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

发布评论

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

评论(2

场罚期间 2024-07-29 05:29:18

首先,我认为您想要:

myDropDown.SelectedItem.Text

而不是

myDropDown.Text

另外,为什么您有两个 ItemTemplates? 我不知道您甚至可以这样做...

您是否在页面生命周期的任何其他时刻与任何文本框或下拉菜单进行交互?

尝试在转发器数据绑定周围进行回发检查。 我认为发生的事情是动态加载控件,因此它们没有视图状态,因此值将始终为空。

First off I think you want:

myDropDown.SelectedItem.Text

Rather Than

myDropDown.Text

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.

满天都是小星星 2024-07-29 05:29:18

确保您在页面生命周期的正确位置运行代码。 如果您做得太早(例如在 OnInit 中),那么它还没有来自客户端的值。 尝试将其移至 OnCommand()/OnSubmit() 事件,看看会发生什么。 以下是有关 ASP.NET 页面生命周期的一些参考:

  1. http://msdn. microsoft.com/en-us/library/ms178472.aspx
  2. http://www .15seconds.com/issue/020102.htm
  3. http://www.beansoftware.com/ASP.NET-Tutorials/Page-Life-Cycle.aspx
  4. 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:

  1. http://msdn.microsoft.com/en-us/library/ms178472.aspx
  2. http://www.15seconds.com/issue/020102.htm
  3. http://www.beansoftware.com/ASP.NET-Tutorials/Page-Life-Cycle.aspx
  4. Picture of asp.net page life cycle
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文