在gridview中禁用按钮

时间:2013-06-05 09:17:16

标签: asp.net button gridview

我有一个Gridview:

<asp:GridView ID="gvtransaction" runat="server" AutoGenerateColumns="False" Width="60%" OnRowDataBound="gvtransaction_RowDataBound" >
            <Columns>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%# Bind("id") %>' Visible="false"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Consumer">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstname" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lbllastname" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Amount">
                    <ItemTemplate>
                        <asp:Label ID="lblamount" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Label ID="lblcurrencyID" runat="server" Text='<%# Bind("CurrencyID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Account Name">
                    <ItemTemplate>
                        <asp:Label ID="lblcurrencyname" runat="server" Text='<%# Bind("CurrencyName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:Label ID="lblstatus" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DateCreated">
                    <ItemTemplate>
                        <asp:Label ID="lbldatecreated" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Button ID="btnReject" runat="server" Text="Reject" OnClick="btnReject_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

这是onclick =“btnApprove_click()的代码:

 GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;

    string id = ((Label)row.FindControl("lblid")).Text;
    Response.Write(row.RowIndex);
    string ApprovedStatus = "Approved";

    Button btnApprove = (Button)sender;
    btnApprove.Enabled = false;

    string status = ClassBiller.ConsumerAcceptedStatus(int.Parse(id), ApprovedStatus, DateTime.Now);
    ViewPendingConsumer(); //rebind gridview para magEffect yun update

我担心的是,当我单击“批准”按钮或“拒绝”按钮时,如何禁用gridview中的按钮。

示例场景: 单击“批准”时,应禁用这些按钮,以防止用户再次单击该按钮。

我读过一些文章,建议使用gridview的onrowdatabound ..但我对如何做到这一点感到困惑......

我尝试使用

row.Enabled = false;

仍然无法运作......

请帮助... 谢谢

3 个答案:

答案 0 :(得分:0)

尝试使用rowdatabound Grid View事件,首先使用FindControl方法查找控件,如

protected void gvtransaction_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
        // For `Approve` button
        Button btnapprove = (Button)e.Row.FindControl("btnApprove"); // give property id of button form template field
        btnapprove.Enabled = true; //true means enable else you may set false to disable button 

        // For `Reject` button
        // Same condition but in `FindControl` method use `btnReject` id.
        }
    }

适用于Approve按钮对于Reject按钮,您可以使用相同的逻辑。

Link For Help

希望它清楚并适合你。

答案 1 :(得分:0)

您可以尝试在其click事件中禁用该按钮。但是当你的gridview再次数据绑定时,该按钮将被启用...

protected void btnApprove_Click(object sender, EventArgs e)
{
    Button btnApprove = (Button)sender;
    btnApprove.Enabled = false;
}

答案 2 :(得分:0)

这就是你需要做的,其中Cells [5]是你有Button的单元格,遇到了同样的问题而且对我有用。

protected void gvtransaction_RowDataBound(object sender,GridViewRowEventArgs e)     {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

       e.Row.Cells[5].Enabled = false;
    }

}