ASP.NET GridView,从ItemTemplate更改Button中的属性

时间:2009-12-03 13:37:47

标签: asp.net gridview button

我有一个GridView,可以从数据库中呈现书籍。

在每一行中都会显示“删除/编辑”按钮。当用户单击“编辑”按钮时,我希望显示“取消”和“更新”按钮,并禁用“编辑”按钮。

我考虑过将“编辑”按钮的onClick事件与GridView行一起使用,以根据行获取相应的按钮,将“编辑”按钮的“启用”属性设置为false,并将“取消”和“更新”按钮的可见性设置为“真”。 / p>

但是,即使我从事件处理程序获取的“编辑”按钮,似乎也无法更改属性。

这是代码。

protected void EditButton_Click(object sender, EventArgs e)
{
    Button Sender = (Button)sender;
    Sender.Text = "??"; //THIS CHANGE IS NOT APPLIED!!

    //Button Sender = (Button)sender;
    //GridViewRow grdRow = (GridViewRow)Sender.Parent.Parent;
    //Button btn = (Button)grdBooks.Rows[grdRow.RowIndex].Cells[1].FindControl("CancelButton");
} 

<asp:GridView
    id="grdBooks"
    DataSourceID="srcBooks"
    DataKeyNames="Product_ID"

    AutoGenerateColumns="false"
    CssClass="products"
    GridLines="none"
    Runat="server" OnRowCreated="grdBooks_RowCreated">
   <Columns>
     <asp:TemplateField>
      <ItemTemplate>
        <asp:Button  CausesValidation="false" ID="DeleteButton" CommandName="Delete" runat="server" Text="Delete" />
        <asp:Button  CausesValidation="false" ID="EditButton" CommandName="Edit" runat="server" Text="Edit" OnClick="EditButton_Click" />    
        <asp:Button  CausesValidation="false" ID="CancelButton"  Enabled="false" Visible="true" CommandName="Cancel" runat="server" Text="Cancel" />  
        <asp:Button  CausesValidation="false" ID="UpdateButton"  Enabled="false" Visible="true" CommandName="Update" runat="server" Text="Update" />  
      </ItemTemplate>
   </asp:TemplateField>
    <%-- <asp:CommandField  ButtonType="Button"  ShowEditButton="true"/>--%>
    <asp:BoundField 
        DataField="ISBN" 
        ReadOnly="true"
        HeaderText="ISBN" />
    <asp:BoundField 
        DataField="Title"
        ReadOnly="true"
        HeaderText="Title" />
    <asp:BoundField 
        DataField="First_Name" 
        ReadOnly="true"
        HeaderText="First Name" />
    <asp:BoundField 
        DataField="Last_Name" 
        ReadOnly="true"
        HeaderText="Last Name" />   
     <asp:BoundField 
        DataField="Price" 
        HeaderText="Price" />
    <asp:BoundField 
        DataField="Quantity" 
        HeaderText="Quantity" />
    </Columns>
</asp:GridView>    

2 个答案:

答案 0 :(得分:0)

处理完事件后,页面可能会重新加载并重置原始文本。

也许你可以使用JavaScript来做你需要的事情。

答案 1 :(得分:0)

好吧,毕竟非常简单,GridView中有这个漂亮的标签,它完全符合我的要求。 感谢您的观看,希望这篇文章有助于更多的讨论..

<EditItemTemplate>
        <asp:Button  CausesValidation="false" ID="CancelButton"  CommandName="Cancel" runat="server" Text="Cancel" />  
        <asp:Button  CausesValidation="false" ID="UpdateButton"  CommandName="Update" runat="server" Text="Update" />  
</EditItemTemplate>
相关问题