我有两个下载列表,它们被加载并且来自db的默认值 - 我在Page Init事件中执行它(在页面加载中也是这样做但是根据我对此主题所做的一些研究更改为页面init )。然后,当我从数据库中检索特定项目的数据并设置每个下拉列表的选定值时,我看到在其中一个下拉列表中设置了正确的值而不是另一个。具体而言,ddlEcsrowService从BindListingDetails获取正确的值集,而ddlListingType则恢复为BindListingTypesDdl中设置的值。有什么建议?提前致谢。代码如下:
======================
create.aspx
<asp:DropDownList ID="ddlEcsrowService" runat="server" Width="258px" EnableViewState="true" AutoPostBack="true" >
</asp:DropDownList>
<asp:DropDownList ID="ddlListingType" runat="server" Width="258px" EnableViewState="true" AutoPostBack="true" >
</asp:DropDownList>
====================
create.aspx.vb
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not IsPostBack Then
BindEscrowServicesDdl()
BindListingTypesDdl()
If (Not Request("lstId") Is Nothing) Then
BindListingDetails(Request("lstId"))
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub BindEscrowServicesDdl()
Dim tmpDt = CType(Application("EscrowServices"), DataTable)
ddlEcsrowService.DataTextField = "escrowservice"
ddlEcsrowService.DataValueField = "escrowserviceid"
ddlEcsrowService.DataSource = tmpDt
ddlEcsrowService.DataBind()
If tmpDt.Rows.Count Then
For index As Integer = 0 To tmpDt.Rows.Count - 1
If tmpDt(index)("escrowserviceid") = "2" Then
ddlEcsrowService.Items(index).Selected = True
Exit For
End If
Next
End If
End Sub
Public Sub BindListingTypesDdl()
Dim tmpDt = CType(Application("ListingTypes"), DataTable)
ddlListingType.DataTextField = "listingtype"
ddlListingType.DataValueField = "listingtypeid"
ddlListingType.DataSource = tmpDt
ddlListingType.DataBind()
If tmpDt.Rows.Count Then
For index As Integer = 0 To tmpDt.Rows.Count - 1
If tmpDt(index)("listingtypeid") = "1" Then
ddlListingType.Items(index).Selected = True
Exit For
End If
Next
End If
End Sub
Public Sub BindListingDetails(ByRef Id As String)
Try
Dim tmpdt As New DataTable()
Dim param As New List(Of SPParameter)
param.Add(New SPParameter("listingid", SqlDbType.BigInt, Request("lstId")))
tmpdt = SPs.ExecSPTableType("pgetownerlistingdetails", param)
If tmpdt.Rows.Count > 0 Then
ddlEcsrowService.SelectedValue = tmpdt(0)("escrowserviceid")
ddlListingType.SelectedValue = tmpdt(0)("listingtypeid")
End If
Catch ex As Exception
End Try
End Sub
答案 0 :(得分:0)
我在上面评论过 - 您可以像这样设置一个下拉列表的selectedIndex吗?
在C#中,您必须执行以下操作:
ListItem itemToSelect = ddl.Items.FindByValue(itemvalue);
if (!(itemtoSelect == null))
{
ddl.SelectedIndex = ddl.Items.IndexOf(itemToSelect);
}
答案 1 :(得分:0)
您可以从BindListingTypesDdl()
If tmpDt.Rows.Count Then
For index As Integer = 0 To tmpDt.Rows.Count - 1
If tmpDt(index)("listingtypeid") = "1" Then
ddlListingType.Items(index).Selected = True
Exit For
End If
Next
End If
以下一个。首先检查您要查找的值是否存在于下拉列表中,并保留默认情况下所选项目的相应索引。
If ddl.Items.FindByValue("1") IsNot Nothing Then
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("1"))
End If
答案 2 :(得分:0)
我找到了解决方案。如果我使用下面的代码设置值,它可以工作:
ddlListingType.ClearSelection()
For Each item1 In ddlListingType.Items
If item1.value = tmpdt(0)("listingtypeid") Then
item1.selected = True
End If
Next
感谢大家的建议
更新: 我找出了这个工作的原因,并且FindByValue无法正常工作:引用数据将此id列作为char(2),但对于另一个下拉列表,它是一个int;所以数据库正在返回&#34; 1&#34;这就是加载到下拉列表中的内容。当列出详细信息返回值时,它是一个int形式,并且永远不会与下拉列表中的那个匹配。