如果在代码隐藏中只有更简单的遍历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>
Codebehind(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中的文本。我认为问题在于我无法FindControl
来自<EditItemTemplate>
部分中由控件启动的<ItemTemplate>
部分中的控件。这就是我想要它的样子。第一个图像是项目模式,第二个是编辑模式。
-------&gt;
它没有显示在我上面的代码隐藏块中,但我在我的“编辑”命令块中使用以下内容来尝试识别结构以及如何抓住我的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("")
等,还是什么?!
我的意思是 GIMME A FREAKING BREAK MICROSOFT !!!把你的东西放在一起!!你让开发者的生活到处都是可怕的!不仅使用IE,而且使用非常不一致的.NET框架,其中每个控件都有不同的成员结构。 FCOL!我决定制作一套广泛的教程和指南,用于探索.NET框架以及某些控件如何转换为html,等等,一旦我推出新网站。这是API imho的一个主要缺点。作为一名新开发人员,很难分辨幕后发生的事情。我的目标是让那些拥有更多HTML和传统编程背景的人更加明显。我学到了一件事,我与框架有着严重的爱/恨关系。
答案 0 :(得分:0)
如果我理解正确,我认为这就是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
Next
答案 1 :(得分:0)