How to set gridview to edit mode on button click instead of using commandname

时光毁灭记忆、已成空白 提交于 2019-12-25 12:09:55

问题


Hi all I am having a template field as follows with an itemtemplate

<asp:TemplateField HeaderText="Edit/Delete">
 <ItemTemplate>
     <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick=lnkEdit_Click">     </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Generally instead of Click event we use to write CommandName="Edit" and on OnRowEditing event we will set gridview row to edit mode with the following code

protected void grdDemo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdDemo.EditIndex = e.NewEditIndex;
        bindGrid();
    }

Instead of this I would to set gridview row to edit mode on link button click, how can we do that any ideas please


回答1:


There are couple of other option available since you wish to ignore the commandname :)

  1. Click anywhere to activate edit mode in gridview

  2. Activate Edit mode based on ID - Datakey

  3. Set the EditIndex property to the appropriate row and then ReBind the GridVIew again to it's DataSource.

    protected void btnEdit_Click(object sender, EventArgs e)
    
     {
      GridView1.EditIndex = 1;
     }
    
  4. Google/Bing for more..




回答2:


You can use edit item template as follows

the following is the sample aspx code

    <ItemTemplate>
    <asp:LinkButton ID="lblSubject"  Width="100%" Height="100%"  CommandName="Edit"   ForeColor="Black" runat="server" Text='<%#Bind("Subject") %>'>
  </asp:LinkButton>
   </ItemTemplate>
   <EditItemTemplate>
  <asp:TextBox ID="lblSubject" runat="server" Text='<%#Bind("Subject") %>'>
       </asp:TextBox>
    </EditItemTemplate>


来源:https://stackoverflow.com/questions/15037103/how-to-set-gridview-to-edit-mode-on-button-click-instead-of-using-commandname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!