如果在代码背后有一种更简单的遍历ASP.NET控件的方法就好了。作为一个实习的.NET开发人员,这一直是我生存的祸根。我想要一些帮助来识别ListView控件的适当成员。为了便于查看,我删除了标记中的所有表示代码,因为它无论如何都不相关。情况如下:
标记
<asp:ListView ID="NewProduct" runat="server" DataSourceID="NewProductSDS" DataKeyNames="ID">
<ItemTemplate>
<asp:Table ID="NewProductTable" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:LinkButton ID="editProductName" runat="server" CommandName="Edit" />
</asp:TableCell>
<!-- I want this value to be transferred to my edit combobox -->
<asp:TableCell ID="NewProductName" runat="server">
<%# Eval("Product").ToString.Trim()%>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
<EditItemTemplate>
<asp:Table ID="NewProductTable" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:LinkButton ID="updateProductName" runat="server" CommandName="Rename" />
<asp:LinkButton ID="cancelProductName" runat="server" CommandName="Cancel" />
<!-- Autocomplete Combobox, NOTE: The DDL is not displayed -->
<asp:DropDownList ID="NewProductName_ddl" runat="server" DataSourceID="productLineSDS" DataTextField="Product" DataValueField="ID"></asp:DropDownList>
<asp:TextBox ID="NewProductName_cb" runat="server"></asp:TextBox>
<button id="NewProductName_btn" type="button"></button>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</EditItemTemplate>
</asp:ListView>代码隐藏(VB)
Protected Sub ItemClick(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles NewProduct.ItemCommand
Dim lv As ListView = DirectCast(sender, ListView)
Dim i As Integer = e.Item.DisplayIndex
'Session State Attempt
Session.Add("NewProductKey", lv.DataKeys(i).Value)
'URL State Attempt
NewProductKey = lv.DataKeys(i).Value
If e.CommandName = "Edit" Then
Session.Add("NewProductKey", lv.DataKeys(i).Value)
Try
'This DDL is in the <EditItemTemplate> section.
' Need to set "selected" to value from "NewProductName" table cell
' For some reason I can't "FindControl" on this one.
Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList)
Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_cb"), TextBox)
tb.Text = "test" 'BROKEN, can't even set the text. How can I ensure this control exists at this time?
'This TableCell is in the <ItemTemplate> section. I can get this
' value back just fine.
Dim pn As TableCell = DirectCast(lv.Items(0).FindControl("NewProductName"), TableCell)
ddl.SelectedValue = CInt(Session.Item("NewProductKey"))
ddl.Text = ddl.SelectedValue
Catch ex As Exception
End Try
'Wireup the Combobox using some custom Javascript.
Page.ClientScript.RegisterStartupScript([GetType], "", "cbsetup(""#NewProductName_cb"", ""#NewProductName_ddl"");", True)
ElseIf e.CommandName = "Rename" Then
Session.Add("NewProductKey", lv.DataKeys(i).Value)
'Update the Product Name with the new value as entered in the TextBox control.
Try
Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList)
Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_cb"), TextBox)
Dim pKey As String = NewProductKey.ToString
Dim pName As String = tb.Text 'Should take the value from the "NewProductName" TableCell
Using connection As New SqlConnection(myConnectionString)
'Query using pName and pKey works perfectly when run from SQL Server.
' The issue I'm having is capturing the values from the controls.
Dim updateQuery As New SqlCommand(RenameProductQueryStr, connection)
connection.Open()
updateQuery.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
End Try
End If
End Sub我想要完成的是,我的组合框已经在DDL中选择了单击行的值,并将文本输入到TextBox中。我认为问题在于我无法从由FindControl节中的控件发起的命令中对<EditItemTemplate>节中的控件进行<ItemTemplate>。这就是我想要的样子。第一个图像是项目模式,第二个图像是编辑模式。

->

它没有显示在上面的代码隐藏块中,但是我在我的"Edit“命令块中使用下面的命令来尝试识别结构和如何抓取Combobox控件来对它们进行操作,但是没有结果:
For Each item As Control In lv.Items
debugLabel.Text += ", Items: " + item.ToString + "<br />"
Next我不知道是否使用lv.Items(0).FindControl("")、lv.Items(0).Parent.FindControl("")、lv.Parent.FindControl("")、lv.FindControl("")等,或者什么?!
我的意思是给我一个该死的突破微软!把你的东西收拾好!!你把开发商的生活弄得乱七八糟!不仅在IE中,而且在一个非常不一致的.NET框架中,每个控件都有不同的成员结构,实现方式不同。FCOL!一旦我推出了我的新网站,我决定编写一套广泛的教程和指南,用于探索.NET框架,以及某些控件如何转换为html等等。这是API imho中的一个主要缺点。作为一名新开发人员,很难知道幕后发生了什么。我的目标是让那些拥有更多html和传统编程背景的人更清楚地看到这一点。我学到了一件事,我与框架有着严重的爱/恨关系。
发布于 2012-01-25 17:56:43
几个月后,我重新提出了这个问题,希望能简化它,并越来越有可能得到足够的援助。我已经张贴了这两个问题的答案,感谢参考问题的一些指导。
:)
发布于 2011-11-09 23:33:54
如果我对你的理解是正确的,我认为这就是Bind的作用。
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("SomeValue") %>'>
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeValue") %>' ... />
</EditItemTemplate>编辑
我想这就是你想要的:
For Each item As ListViewItem In lv.Items
Dim ddl As DropDownList = DirectCast(item.FindControl("NewProductName_ddl"), DropDownList)
If ddl IsNot Nothing Then
'your code
End If
Nexthttps://stackoverflow.com/questions/8073159
复制相似问题