【发布时间】:2011-11-09 23:24:48
【问题描述】:
如果有更简单的方法在代码隐藏中遍历 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
我想要完成的是让我的 Combobox 具有已在 DDL 中选择的单击行的值以及输入到 TextBox 中的文本。我认为问题在于我无法通过<ItemTemplate> 部分中的控件启动的命令对<EditItemTemplate> 部分中的控件进行FindControl。这就是我想要的样子。第一张图是项目模式,第二张图是编辑模式。
-------->
上面的代码隐藏块中没有显示它,但我在“编辑”命令块中使用以下内容来尝试识别结构以及如何抓取我的 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 框架,其中每个控件都有不同的成员结构,因为实现方式不同。整容!!!一旦我推出我的新网站,我决定制作一套广泛的教程和指南来探索 .NET 框架以及某些控件如何转换为 html 等等。这是 API imho 的一个主要缺点。作为一名新开发人员,很难说出幕后发生的事情。我的目标是让那些拥有更多 html 和传统编程背景的人更清楚这一点。我学到了一件事,我对框架有着严重的爱/恨关系。
【问题讨论】:
标签: asp.net vb.net controls code-behind traversal