[更新/取消]按钮不会出现在模板列编辑按钮 [英] "Update/Cancel" buttons don't appear in TemplateField Edit button

查看:205
本文介绍了[更新/取消]按钮不会出现在模板列编辑按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你使用一个GridView的每一行中创建一个编辑按钮 CommandField中它显示更新/取消按钮点击后,这样你就可以接受/取消更改。
不过,我想有提示文字,因为 CommandField中没有tooltip属性,我用了一个编辑按钮模板列。它曾与删除按钮,但我有与编辑键问题:

 < ASP:GridView控件ID =GridView1=服务器
                    AllowPaging =真AllowSorting =真
                    数据成员=默认视图
                    的DataSourceID =SqlDataSource1的AutoGenerateColumns =FALSE
                    的DataKeyNames =字段,字段FIELD的cellpadding =4前景色=#333333WIDTH =90%
                    HEIGHT =90%FONT-SIZE =小>
                    < RowStyle的BackColor =#EFF3FB/>
                    <柱体和GT;
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD只读=真
                            SORTEX pression =FIELD/>
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD
                            SORTEX pression =FIELD/>
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD
                            SORTEX pression =FIELD/>
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD只读=真
                            SORTEX pression =FIELD/>
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD只读=真
                            SORTEX pression =FIELD/>
                        < ASP:BoundField的数据字段=FIELD的HeaderText =FIELD
                            SORTEX pression =FIELD/>
                            < ASP:CommandField中按钮类型=形象可见=真的EditText =编辑ShowEditButton =真EditImageUrl =图像/ pencil1.png>< / ASP:CommandField中>
                            < ASP:的TemplateField>
                            <&ItemTemplate中GT;
                             < ASP:ImageButton的ID =deleteButton=服务器的CommandName =删除文本=删除
                                        的OnClientClick =返回确认('¿你确定吗?');工具提示=删除的ImageUrl =图片/ DeleteRed1.png/>
                            < / ItemTemplate中>
                            < / ASP:的TemplateField>
                    < /专栏>
                    < FooterStyle背景色=#507CD1FONT-粗体=真前景色=白/>
                    < PagerStyle背景色=#2461BF前景色=白Horizo​​ntalAlign =中心/>
                    < SelectedRowStyle背景色=#D1DDF1FONT-粗体=真前景色=#333333/>
                    < HeaderStyle背景色=#507CD1FONT-粗体=真前景色=白/>
                    < EditRowStyle背景色=#2461BF/>
                    < AlternatingRowStyle背景色=白/>
                < / ASP:GridView的>< ASP:SqlDataSource的ID =SqlDataSource1=服务器
                    的ConnectionString =下;%$的ConnectionStrings:DBUserInterfaceConnectionString%>中
                    的SelectCommand =SELECT ... FROM ... INNER JOIN ... ON ...
                    DeleteCommand会=DELETE FROM ... WHERE ... = @参数;
                    更新命令=UPDATE ... SET ... = @参数... = @参数2 WHERE ... = @参数3和... = @ p​​aram4和... = @ p​​aram5;
                    >
                    < / ASP:SqlDataSource的>

正如我以前说过,我换成 CommandField中

 < ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:ImageButton的ID =editButton=服务器的CommandName =编辑文本=编辑工具提示=编辑的ImageUrl =图片/ pincel1.png/>
< / ItemTemplate中>
< / ASP:的TemplateField>

但更新/取消按钮不会出现,所以我不能更新/编辑操作。为什么会发生?

任何想法,以实现一个成功的编辑按钮?

注:


  1. 这两个按钮没有动code的背后,由于某种原因,删除按钮的工作原理只是的DeleteCommand 的SqlDataSource ,如果我尝试删除命令,因为没有指定DeleteCommand会被它会提示错误。


  2. 更新命令没有目的,它可以被删除。我可以用它的更新按钮,而不是一个编辑按钮,但是当我尝试,它说 @params 不知道,这就是为什么我决定使用编辑按钮来代替。



解决方案

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

< ASP:CommandField中>当你要使用pre定义的命令按钮来执行选择,编辑或删除操作被使用。检查 MSDN这里

所以,当您使用的是自己的用户定义的方式进行编辑按钮,还需要指定更新和放您的自定义内容的方式; EditItemTemplate中>在&LT取消按钮为:

 < ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:ImageButton的ID =editButton=服务器的CommandName =编辑文本=编辑
     工具提示=编辑的ImageUrl =图片/ pincel1.png/>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:ImageButton的ID =BtnUpdate=服务器的CommandName =更新文本=更新
     的OnClick =BtnUpdate_Click的ImageUrl =图片/ Update.png/>
< ASP:ImageButton的ID =BtnCancel=服务器的CommandName =取消文本=取消
     的OnClick =BtnCancel_Click的ImageUrl =图片/ Cancel.png/>
< / EditItemTemplate中>
< / ASP:的TemplateField>

和刚刚确定,只有当您再次提供您的自定义执行情况更新和放大器;取消逻辑,您也可以定义为这两个更新的onclick事件和取消按钮。否则这些按钮标记删除的OnClick
[ BtnUpdate_Click &安培; BtnCancel_Click 点击这里。]

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

<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 CommandField with:

<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 DeleteCommand in the SqlDataSource, and if I try to delete the command, it prompts error because no DeleteCommand is specified.

  2. UpdateCommand has 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 @params are not known, that's why I decided to use edit button instead.

解决方案

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.

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

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 :

<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 OnClick from markup of these buttons. [ BtnUpdate_Click & BtnCancel_Click here.]

这篇关于[更新/取消]按钮不会出现在模板列编辑按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆