2011-11-09 55 views
1

如果只有更简单的遍历代码隐藏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。这是我想要的样子。第一个图像是项目模式,第二个是编辑模式。

enter image description here ------->enter image description here

这不是在上面我隐藏块显示,但我用我的“编辑”命令块内的下列尝试识别的结构,以及如何抢我的组合框控件对其采取措施,但无济于事:(

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

我简化它,并日益成为的希望重新陷害我这个问题问的个月后获得适当援助的可能性。我已经发布了这两个问题的答案,这要归功于引用问题的一些指导。

Go here to see my answer :)

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 
+0

我添加了一些图像。在我的ItemTemplate中显示Combobox是没有意义的。顺便说一句,我读过,在标记中使用内联程序逻辑通常不是好习惯。这是不是真的在这种情况下,因为意见是如此痛苦? – Chiramisu

+0

如果可以避免使用内联代码块,则不鼓励使用内联代码块,但在数据绑定控件的上下文中,它完全没问题。 –

+0

嗯...我真的不认为这是我在这种情况下寻找的答案。或者至少我看不到连接。无论如何,我想保留我的模板,也就是说,我不希望我的Combobox控件位于“ItemTemplate”部分。没有冒犯的意思。其他想法?或进一步解释?如果你喜欢,我们可以再次聊天。让我知道,并再次感谢;) – Chiramisu