如何在ListView中创建一个新按钮?

时间:2011-01-25 07:40:04

标签: asp.net vb.net listview

我需要在ListView的布局模板中创建一个图像按钮, 当我点击按钮时,它应该打开插入模板,以便我可以插入新的记录 这是我的ListView:

<asp:ListView ID="LVCategories" runat="server" DataKeyNames="CatID" DataSourceID="CategoriesDS" EnableModelValidation="True">
<AlternatingItemTemplate>
    <tr style="">
        <td>
            <asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionLabel" runat="server" Text='<%# Eval("CatDescription") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleArLabel" runat="server" Text='<%# Eval("CatTitleAr") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionArLabel" runat="server" Text='<%# Eval("CatDescriptionAr") %>' />
        </td>
        <td>
            <asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
        </td>
        <td>
            <asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Eval("Published") %>' Enabled="false" />
        </td>
        <td>
            <asp:Label ID="DisplayOrderLabel" runat="server" Text='<%# Eval("DisplayOrder") %>' />
        </td>
        <td>
            <asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
        </td>
    </tr>
</AlternatingItemTemplate>
<EditItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
        </td>
        <td>
            <asp:Label ID="CatIDLabel1" runat="server" Text='<%# Eval("CatID") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatTitleTextBox" runat="server" Text='<%# Bind("CatTitle") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatDescriptionTextBox" runat="server" Text='<%# Bind("CatDescription") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatTitleArTextBox" runat="server" ext='<%# Bind("CatTitleAr") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatDescriptionArTextBox" runat="server" Text='<%# Bind("CatDescriptionAr") %>' />
        </td>
        <td>
            <asp:TextBox ID="PictureIDTextBox" runat="server" Text='<%# Bind("PictureID") %>' />
        </td>
        <td>
            <asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Bind("Published") %>' />
        </td>
        <td>
            <asp:TextBox ID="DisplayOrderTextBox" runat="server" Text='<%# Bind("DisplayOrder") %>' />
        </td>
        <td>
            <asp:TextBox ID="CreatedOnTextBox" runat="server" Text='<%# Bind("CreatedOn") %>' />
        </td>
    </tr>
</EditItemTemplate>
<EmptyDataTemplate>
    <table runat="server" style="">
        <tr>
            <td>
                No data was returned.
            </td>
        </tr>
    </table>
</EmptyDataTemplate>
<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert"  Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
        </td>
        <td>
            <asp:TextBox ID="CatIDTextBox" runat="server" Text='<%# Bind("CatID") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatTitleTextBox" runat="server" Text='<%# Bind("CatTitle") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatDescriptionTextBox" runat="server" Text='<%# Bind("CatDescription") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatTitleArTextBox" runat="server" Text='<%# Bind("CatTitleAr") %>' />
        </td>
        <td>
            <asp:TextBox ID="CatDescriptionArTextBox" runat="server" Text='<%# Bind("CatDescriptionAr") %>' />
        </td>
        <td>
            <asp:TextBox ID="PictureIDTextBox" runat="server" Text='<%# Bind("PictureID") %>' />
        </td>
        <td>
            <asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Bind("Published") %>' />
        </td>
        <td>
            <asp:TextBox ID="DisplayOrderTextBox" runat="server" Text='<%# Bind("DisplayOrder") %>' />
        </td>
        <td>
            <asp:TextBox ID="CreatedOnTextBox" runat="server" Text='<%# Bind("CreatedOn") %>' />
        </td>
    </tr>
</InsertItemTemplate>
<ItemTemplate>
    <tr style="">
        <td>
            <asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionLabel" runat="server" Text='<%# Eval("CatDescription") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleArLabel" runat="server" Text='<%# Eval("CatTitleAr") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionArLabel" runat="server" Text='<%# Eval("CatDescriptionAr") %>' />
        </td>
        <td>
            <asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
        </td>
        <td>
            <asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Eval("Published") %>' Enabled="false" />
        </td>
        <td>
            <asp:Label ID="DisplayOrderLabel" runat="server" Text='<%# Eval("DisplayOrder") %>' />
        </td>
        <td>
            <asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
        </td>
    </tr>
</ItemTemplate>
<LayoutTemplate>
    <table runat="server">
        <tr runat="server">
            <td runat="server">
                <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                    <tr runat="server" style="">
                        <th runat="server">
                            <asp:ImageButton ID="btnNew" runat="server" CommandName="New" />
                             <%--<asp:ImageButton ID="NewCatbtn" runat="server" CommandName="Insert" ImageUrl="~/ADMIN/themes/Img/New.png" AlternateText="Click Here To Add New" OnClick="OpenTheInsertTemplates"/>--%>
                             </th>
                        <th runat="server">CatTitle</th>
                        <th runat="server">CatDescription</th>
                        <th runat="server">CatTitleAr</th>
                        <th runat="server">atDescriptionAr</th>
                        <th runat="server">PictureID</th>
                        <th runat="server">Published</th>
                        <th runat="server">DisplayOrder</th>
                        <th runat="server">CreatedOn</th>
                    </tr>
                    <tr ID="itemPlaceholder" runat="server"></tr>
                </table>
            </td>
        </tr>
        <tr runat="server">
            <td runat="server" style=""></td>
        </tr>
    </table>
</LayoutTemplate>
<SelectedItemTemplate>
    <tr style="">
        <td>
            <asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionLabel" runat="server" 
                Text='<%# Eval("CatDescription") %>' />
        </td>
        <td>
            <asp:Label ID="CatTitleArLabel" runat="server" 
                Text='<%# Eval("CatTitleAr") %>' />
        </td>
        <td>
            <asp:Label ID="CatDescriptionArLabel" runat="server" 
                Text='<%# Eval("CatDescriptionAr") %>' />
        </td>
        <td>
            <asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
        </td>
        <td>
            <asp:CheckBox ID="PublishedCheckBox" runat="server" 
                Checked='<%# Eval("Published") %>' Enabled="false" />
        </td>
        <td>
            <asp:Label ID="DisplayOrderLabel" runat="server" 
                Text='<%# Eval("DisplayOrder") %>' />
        </td>
        <td>
            <asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
        </td>
    </tr>
</SelectedItemTemplate>

                                    

2 个答案:

答案 0 :(得分:2)

<asp:Button runat="server" ID="cmdInsert" CommandName="Insert" Text="Insert" />

Protected Sub ListView1_ItemInserting (ByVal sender As Object, ByVal e as ListViewEditEventArgs)
    ' ... '
End Sub

答案 1 :(得分:0)

此示例已解决如下: ASPX文件:

 <th runat="server">
                                    <asp:ImageButton ID="ImageButton1" runat="server"  Text="new" CommandName="new" ImageUrl="~/ADMIN/themes/Img/DeleteBtn.png" />
                                </th>

VB.net代码:

Sub deletedd(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
    Try
        If e.CommandName.Equals("Delete") Then
            'this to take a value from any control
            Dim Idlabel As Label = e.Item.FindControl("ImageIDLabel")
            Session("ID") = Idlabel.Text()
        End If
        If e.CommandName.Equals("new") Then
            'Other code
            ListView1.InsertItemPosition = InsertItemPosition.FirstItem
        End If
        If e.CommandName.Equals("Cancel") Then
            'Other code
            ListView1.InsertItemPosition = InsertItemPosition.None
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try


End Sub