vb.net “更新/取消”按钮不会出现在 TemplateField 编辑按钮中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18137137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:35:51  来源:igfitidea点击:

"Update/Cancel" buttons don't appear in TemplateField Edit button

asp.netvb.netgridviewtemplatefieldcommandfield

提问by phalanx

When you create an edit button in every row of a Gridview using CommandFieldit displays update/cancel buttons after clicking, so you can accept/cancel changes. However, I want an edit button that has tooltip text, and since CommandFielddoesn't have tooltip property, i used TemplateField. It worked with the delete button, but I'm having problems with the edit button:

当您在 Gridview 的每一行中创建一个编辑按钮时,CommandField它会在单击后显示更新/取消按钮,因此您可以接受/取消更改。但是,我想要一个带有工具提示文本的编辑按钮,并且由于CommandField没有工具提示属性,所以我使用了TemplateField. 它与删除按钮一起工作,但我在编辑按钮上遇到了问题:

<asp:GridView ID="GridView1" runat="server" 
                    AllowPaging="True" AllowSorting="True"
                    DataMember="DefaultView" 
                    DataSourceID="SqlDataSource1" AutoGenerateColumns="False" 
                    DataKeyNames=FIELD,FIELD,FIELD" CellPadding="4" ForeColor="#333333" Width="90%"
                    Height="90%" Font-Size="Small">
                    <RowStyle BackColor="#EFF3FB" />
                    <Columns>
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True" 
                            SortExpression="FIELD" />
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" 
                            SortExpression="FIELD" />
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" 
                            SortExpression="FIELD" />
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True" 
                            SortExpression="FIELD" />
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True" 
                            SortExpression="FIELD" />
                        <asp:BoundField DataField="FIELD" HeaderText="FIELD" 
                            SortExpression="FIELD" />                        
                            <asp:CommandField ButtonType="Image"  Visible="true" EditText="Edit" ShowEditButton="True" EditImageUrl="images/pencil1.png"></asp:CommandField>                            
                            <asp:TemplateField >
                            <ItemTemplate>
                             <asp:ImageButton ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" 
                                        OnClientClick="return confirm('?Are you sure?');" ToolTip="delete" ImageUrl="images/DeleteRed1.png" />
                            </ItemTemplate>                                     
                            </asp:TemplateField>
                    </Columns>                    
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:DBUserInterfaceConnectionString %>"                     
                    SelectCommand="SELECT ... FROM ...  INNER JOIN ... ON ..."
                    DeleteCommand="DELETE FROM ... WHERE ...=@param;"
                    UpdateCommand="UPDATE ... SET ... = @param, ... = @param2 WHERE ... = @param3 and ... = @param4 and ... = @param5;"
                    >
                    </asp:SqlDataSource>

As I said before, I replaced CommandFieldwith:

正如我之前所说,我替换CommandField为:

<asp:TemplateField >
<ItemTemplate> 
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit" Text="Edit" ToolTip="Edit" ImageUrl="images/pincel1.png" />  
</ItemTemplate>
</asp:TemplateField >

but "Update/Cancel" buttons don't appear, so I can't update/edit anything. Why does it happen?

但是“更新/取消”按钮没有出现,所以我不能更新/编辑任何东西。为什么会发生?

Any Ideas to implement a succesful edit button?

实现成功编辑按钮的任何想法?

NOTES:

笔记:

  1. Both buttons don't have vb code behind, for some reason delete button works just with DeleteCommandin the SqlDataSource, and if I try to delete the command, it prompts error because no DeleteCommand is specified.

  2. UpdateCommandhas no purpose, it can be deleted. I could use it for a update button instead of an edit button, but when i tried, it says @paramsare not known, that's why I decided to use edit button instead.

  1. 两个按钮都没有后面的 vb 代码,出于某种原因,删除按钮只能DeleteCommand在 中使用SqlDataSource,如果我尝试删除该命令,它会提示错误,因为没有指定 DeleteCommand。

  2. UpdateCommand没有目的,可以删除。我可以将它用作更新按钮而不是编辑按钮,但是当我尝试时,它说@params未知,这就是我决定使用编辑按钮的原因。

回答by R.C

The <asp:TemplateField>is used when you want to set your own-defined i.e. User-Defined content for each item in the GridView control.

<asp:TemplateField>当你想设置自己的定义,即用户定义的内容在GridView控件每个项目使用。

The <asp:CommandField>is used when you want to use pre-defined command buttons to perform select, edit, or delete operations. Check MSDN here.

<asp:CommandField>当你要使用预先定义的命令按钮进行选择,编辑或删除操作时使用。在此处查看MSDN

So, when your are using your own user-defined way for edit button, you also need to specify your custom content way for Update & Cancel button inside <EditItemTemplate>as :

因此,当您使用自己的用户定义方式编辑按钮时,您还需要为内部的更新和取消按钮指定自定义内容方式<EditItemTemplate>

<asp:TemplateField >            
<ItemTemplate> 
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit" Text="Edit" 
     ToolTip="Edit" ImageUrl="images/pincel1.png" />  
</ItemTemplate>            
<EditItemTemplate>
<asp:ImageButton ID="BtnUpdate" runat="server" CommandName="Update" Text="Update" 
     OnClick="BtnUpdate_Click" ImageUrl="images/Update.png"/>
<asp:ImageButton ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel" 
     OnClick="BtnCancel_Click" ImageUrl="images/Cancel.png"/>
</EditItemTemplate>        
</asp:TemplateField >

And just make sure, Only if you are again providing your Custom Implementation for Update & Cancel logic,you also define the onclick events for these two Update and Cancel buttons. Else remove the OnClickfrom markup of these buttons. [ BtnUpdate_Click& BtnCancel_Clickhere.]

并确保,仅当您再次为更新和取消逻辑提供自定义实现时,您还需要为这两个更新和取消按钮定义 onclick 事件。否则删除OnClick这些按钮的from 标记。[ BtnUpdate_Click&BtnCancel_Click这里。]

回答by Garrison Neely

I think since you've converted it to a TemplateField, all of the automatically-functioning stuff (like Update/Cancel buttons) has been disabled. I'm betting you'll need to add an <EditItemTemplate>with the Update and Cancel buttons, and hook them to the relevant commands using CommandName.

我认为自从您将其转换为 TemplateField 后,所有自动运行的东西(如更新/取消按钮)都已被禁用。我敢打赌,您需要添加<EditItemTemplate>带有更新和取消按钮的 ,并使用 将它们连接到相关命令CommandName