If only there were an easier way of traversing ASP.NET controls in the codebehind. This has been the bane of my existence as an interning .NET developer. I would like some help identifying the proper member of the ListView
controls. I've deleted all the presentation code in the markup to make it easier to look, since it isn't relevant anyway. Here's the situation:
Markup
<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
What I want to accomplish is for my Combobox to have the value of the clicked row already selected in the DDL AND the text entered into the TextBox. I think the issue lies with my inability to FindControl
on a control in the <EditItemTemplate>
section from a Command initiated by a control in the <ItemTemplate>
section. Here's what I want it to look like. The first image is item mode, the second is edit mode.
------->
It's not shown in my codebehind block above, but I'm using the following inside my "Edit" command block to try to identify the structure and how to grab my Combobox controls to act on them, but to no avail :(
For Each item As Control In lv.Items
debugLabel.Text += ", Items: " + item.ToString + "<br />"
Next
I don't know whether to use lv.Items(0).FindControl("")
, lv.Items(0).Parent.FindControl("")
, lv.Parent.FindControl("")
, lv.FindControl("")
, etc, or WHAT?!
I mean GIMME A FREAKING BREAK MICROSOFT!!! Get your stuff together!! You're making developers' lives everywhere freaking MISERABLE!! Not only with IE, but with a very inconsistent .NET framework where every control has a different member structure as is implemented differently. FCOL!!! I've decided to make an extensive set of tutorials and guides for exploring the .NET framework and how certain controls translate to html, and so on, once I roll out my new website. This is a major shortcoming in the API imho. As a new developer, it is extremely difficult to tell what is going on behind the scenes. I aim to make that a bit more evident to those with more of an html and traditional programming background. I've learned one thing, I have a serious love/hate relationship with frameworks.
I re-framed my asking of this question months later in hopes of simplifying it and increasingly the likelihood of receiving adequate assistance. I have posted the answer to both of these questions, thanks to some guidance from the referenced question.