Interacting with ListView (NamingContainer) Item Controls
如果有更简单的方法在代码隐藏中遍历 ASP.NET 控件就好了。这一直是我作为实习 .NET 开发人员存在的祸根。我需要一些帮助来识别
标记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <ItemTemplate> </asp:TableCell> <!-- I want this value to be transferred to my edit combobox --> <%# Eval("Product").ToString.Trim()%> </asp:TableCell> </asp:TableRow> </asp:Table> </ItemTemplate> <EditItemTemplate> <!-- Autocomplete Combobox, NOTE: The DDL is not displayed --> </asp:DropDownList> </asp:TextBox> <button id="NewProductName_btn" type="button"></button> </asp:TableCell> </asp:TableRow> </asp:Table> </EditItemTemplate> </asp:ListView> |
代码隐藏 (VB)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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 中的文本。我认为问题在于我无法通过
------->
它没有显示在我上面的代码隐藏块中,但我在"编辑"命令块中使用以下内容来尝试识别结构以及如何抓取我的 Combobox 控件以对其进行操作,但无济于事:(
1 2 3 | For Each item As Control In lv.Items debugLabel.Text +=", Items:" + item.ToString +"<br />" Next |
我不知道是用
我的意思是给我一个可怕的rest微软!把你的东西放在一起!!你让开发人员的生活到处都是可怕的悲惨!不仅使用 IE,而且使用非常不一致的 .NET 框架,其中每个控件都有不同的成员结构,因为实现方式不同。整容!!!一旦我推出我的新网站,我决定制作一套广泛的教程和指南来探索 .NET 框架以及某些控件如何转换为 html 等等。这是 API imho 的一个主要缺点。作为一名新开发人员,很难说出幕后发生的事情。我的目标是让那些拥有更多 html 和传统编程背景的人更清楚这一点。我学到了一件事,我对框架有着严重的爱/恨关系。
几个月后,我重新提出了这个问题,希望能够简化它并增加获得足够帮助的可能性。感谢参考问题的一些指导,我已经发布了这两个问题的答案。
去这里看看我的答案:)
如果我对您的理解正确,我认为这就是
1 2 3 4 5 6 7 8 | <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("SomeValue") %>'> </asp:DropDownList> </ItemTemplate> <EditItemTemplate> ' ... /> </EditItemTemplate> |
编辑
我想这就是你所追求的:
1 2 3 4 5 6 | 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 Next |