如果只有更簡單的遍歷代碼隱藏ASP.NET控件的方法。這是我作爲一個實習.NET開發人員存在的禍根。我想找一些幫助來確定ListView
控件的正確成員。我已經刪除了標記中的所有表示代碼,以便於查看,因爲它無關。這裏的情況:與ListView交互(NamingContainer)項目控件
標記
<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中的文本。我認爲問題在於我無法通過<ItemTemplate>
部分中的控件發起的命令在<EditItemTemplate>
部分控制FindControl
。這是我想要的樣子。第一個圖像是項目模式,第二個是編輯模式。
------->
這不是在上面我隱藏塊顯示,但我用我的「編輯」命令塊內的下列嘗試識別的結構,以及如何搶我的組合框控件對其採取措施,但無濟於事:(
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和傳統編程背景的人更加明顯。我學到了一件事,我對框架有着嚴肅的愛/恨關係。
我添加了一些圖像。在我的ItemTemplate中顯示Combobox是沒有意義的。順便說一句,我讀過,在標記中使用內聯程序邏輯通常不是好習慣。這是不是真的在這種情況下,因爲意見是如此痛苦? – Chiramisu
如果可以避免使用內聯代碼塊,則不鼓勵使用內聯代碼塊,但在數據綁定控件的上下文中,它完全沒問題。 –
嗯...我真的不認爲這是我在這種情況下尋找的答案。或者至少我看不到連接。無論如何,我想保留我的模板,也就是說,我不希望我的Combobox控件位於「ItemTemplate」部分。沒有冒犯的意思。其他想法?或進一步解釋?如果你喜歡,我們可以再次聊天。讓我知道,並再次感謝;) – Chiramisu