我正在使用vs2010,.net 4.我的gridview中的第一列是一个CommandField,它在初始渲染时显示Edit Delete和Set Default as linkbuttons。当编辑单击时,我有一个弹出窗口,表单在接受时更新我的数据库。我的问题是CommandField显示我不想要的更新和取消。
有一种方法可以在单击编辑时阻止更新和取消。
提前感谢您的帮助。
这是我的部分网格视图:
<asp:GridView runat="server" ID="lstComponents" Width="100%" BorderWidth="1px" BorderStyle="None"
EnableViewState="True" AutoGenerateColumns="False" DataKeyNames="ComponentID,ComponentName,ComponentType,IPAddress"
CellPadding="0" CellSpacing="0" OnRowDataBound="lstComponents_RowDataBound" AllowSorting="false"
HeaderStyle-CssClass="ListHeader" HeaderStyle-ForeColor="White" OnSelectedIndexChanging="lstComponents_SelectedIndexChanging"
OnRowEditing="lstComponents_RowEditing" OnRowDeleting="lstComponents_RowDeleting" >
<Columns>
<asp:CommandField ShowDeleteButton="true" ShowSelectButton="true" ShowEditButton="True"
HeaderStyle-CssClass="ListHeader" SelectText="Set Default" ItemStyle-CssClass="ListData"
HeaderStyle-Width="150px">
<HeaderStyle CssClass="ListHeader" Width="150px" />
<ItemStyle CssClass="ListData" />
</asp:CommandField>
答案 0 :(得分:1)
从asp:CommandField中删除ShowEditButton =“True”并在ItemTemplate中另外添加一个linkButton,并在linkButton的OnClick事件/ OnClientClick事件中执行您的逻辑。
查看更新的代码,
<asp:GridView runat="server AutoGenerateColumns="False" DataKeyNames="ComponentID,ComponentName,ComponentType,IPAddress"
CellPadding="0" CellSpacing="0" OnRowDataBound="lstComponents_RowDataBound"
HeaderStyle-CssClass="ListHeader" HeaderStyle-ForeColor="White" OnSelectedIndexChanging="lstComponents_SelectedIndexChanging"
OnRowEditing="lstComponents_RowEditing" OnRowDeleting="lstComponents_RowDeleting" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" runat="server" Text="edit">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" ShowSelectButton="true"
HeaderStyle-CssClass="ListHeader" SelectText="Set Default" ItemStyle-CssClass="ListData"
HeaderStyle-Width="150px">
<HeaderStyle CssClass="ListHeader" Width="150px" />
<ItemStyle CssClass="ListData" />
</asp:CommandField>
希望这可以帮助你...