在Insert ITem Template中向DetailView DropDownlist添加项目

时间:2012-01-13 03:49:58

标签: vb.net detailsview

我有详细信息,其中我使用Dropdownlist来插入项目模板,我正在尝试使用vb.net代码使用for循环bu向Dropdown添加项目当页面运行时它添加值五次因为详细视图中的总行数是的,请任何一个人来解决这个问题 这是我的代码

If Me.IsPostBack Then



            If Page.User.Identity.Name = "gate" Then
                For Each DetVW As DetailsViewRow In DetailsView1.Rows
                    Dim ddltype As DropDownList = CType(DetVW.FindControl("DDLTYPE"), DropDownList)

                    ddltype.Items.Add(New ListItem("", ""))
                    ddltype.Items.Add(New ListItem("Sugar Loading", "SL"))
                    ddltype.Items.Add(New ListItem("Molasses Unloading", "MU"))
                    ddltype.Items.Add(New ListItem("General Vehicle", "GV"))
                    ddltype.Items.Add(New ListItem("Ethanol Loading", "EL"))
                    ddltype.Items.Add(New ListItem("Bagasses Unloading", "BU"))
                    Label4.Text = "01"

                Next
            ElseIf Page.User.Identity.Name = "bhone" Then

                For Each DetVW As DetailsViewRow In DetailsView1.Rows
                    Dim ddltype As DropDownList = CType(DetVW.FindControl("DDLTYPE"), DropDownList)

                    ddltype.Items.Add(New ListItem("", ""))
                    ddltype.Items.Add(New ListItem("Sugar Loading", "SL"))
                    ddltype.Items.Add(New ListItem("Molasses Unloading", "MU"))
                    ddltype.Items.Add(New ListItem("General Vehicle", "GV"))
                    ddltype.Items.Add(New ListItem("Ethanol Loading", "EL"))
                    ddltype.Items.Add(New ListItem("Bagasses Unloading", "BU"))
                    Label4.Text = "02"
                Next
            ElseIf Page.User.Identity.Name = "ujl" Then
                For Each DetVW As DetailsViewRow In DetailsView1.Rows
                    Dim ddltype As DropDownList = CType(DetVW.FindControl("DDLTYPE"), DropDownList)

                    ddltype.Items.Add(New ListItem("", ""))
                    ddltype.Items.Add(New ListItem("Cotton Unloading", "CU"))
                    ddltype.Items.Add(New ListItem("Yarn Loading", "YL"))
                    Label4.Text = "03"
                Next
            End If
        End If

2 个答案:

答案 0 :(得分:0)

我不相信DetailsViewRow是一个命名容器,因此对于每一行,它会找到相同的下拉列表,从而填充五次。
IMO,您不需要迭代每一行 - 只需处理详细信息视图。例如

If Page.User.Identity.Name = "gate" Then
    Dim ddltype As DropDownList = CType(DetailsView1.FindControl("DDLTYPE"), DropDownList)
    ddltype.Items.Add(New ListItem("", ""))
    ddltype.Items.Add(New ListItem("Sugar Loading", "SL"))
    ddltype.Items.Add(New ListItem("Molasses Unloading", "MU"))
    ddltype.Items.Add(New ListItem("General Vehicle", "GV"))
    ddltype.Items.Add(New ListItem("Ethanol Loading", "EL"))
    ddltype.Items.Add(New ListItem("Bagasses Unloading", "BU"))
    Label4.Text = "01"
ElseIf Page.User.Identity.Name = "bhone" Then
....

另一种方法是查看RowState属性以选择插入模式下的行。例如,

If Page.User.Identity.Name = "gate" Then
   For Each DetVW As DetailsViewRow In DetailsView1.Rows
      If DetVW.RowState = DataControlRowState.Insert Then
        Dim ddltype As DropDownList = CType(DetailsView1.FindControl("DDLTYPE"), DropDownList)
        ddltype.Items.Add(New ListItem("", ""))
        ddltype.Items.Add(New ListItem("Sugar Loading", "SL"))
        ddltype.Items.Add(New ListItem("Molasses Unloading", "MU"))
        ddltype.Items.Add(New ListItem("General Vehicle", "GV"))
        ddltype.Items.Add(New ListItem("Ethanol Loading", "EL"))
        ddltype.Items.Add(New ListItem("Bagasses Unloading", "BU"))
        Label4.Text = "01"
        Exit For
      End If
   Next
ElseIf Page.User.Identity.Name = "bhone" Then
....

答案 1 :(得分:0)

我们只能使用asp.net

If UserRole = "Admin" Then
    DropDownList5.Items.Add(New ListItem("text", "value"))
    DropDownList5.Items.Add(New ListItem("text1", "value1"))
else
    DropDownList5.Items.Add(New ListItem("text2", "value2"))
End If