【问题标题】:ASP.NET GridView, change properties in Button from ItemTemplateASP.NET GridView,从 ItemTemplate 更改 Button 中的属性
【发布时间】:2009-12-03 13:37:46
【问题描述】:

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

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

我考虑过使用 Edit 按钮的 onClick 事件以及 GridView 行来根据行获取适当的按钮,将 Edit 按钮的 Enable 属性设置为 false 并将 Cancel 和 Update 按钮的可见性设置为 true。

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

这里是代码。

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>    

【问题讨论】:

    标签: asp.net gridview button


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢您的回复。事实上,页面正在重新加载..可能是按钮位于 Griedview 中的模板内的事实导致重置原始文本..如果是这种情况,有没有办法克服这种默认行为?..自从我更新 griedview 之外的按钮,它工作得很好。
    【解决方案2】:

    好的,毕竟这很简单,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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多