Gridview单击行启用控件

时间:2013-04-11 08:15:35

标签: c# asp.net gridview rowdatabound

我有一个gridview。 gridview有一个文本框多行,用于保存文本。之后我有一个名为approved的图像按钮列,一个名为not approved的列。我想在单击已批准或未批准之前强制用户读取多行框内的文本。我如何以编程方式实现此目的?我知道我应该创建一个Rowdatabound事件,但我应该用代码做什么?我正在使用c#ASP.NET Web应用程序。

1 个答案:

答案 0 :(得分:0)

我知道您可以使用javascript跟踪浏览器的滚动条,但我个人从未遇到类似的文本框功能。我建议尝试稍微不同的方法,为什么不在网格视图中添加额外的列,其中有一个复选框控件 - 我已阅读并接受协议。只有在选中复选框时才会启用“批准”按钮,这是一个示例:

<强> ASPX:

<div>
    <asp:ScriptManager ID="sm" runat="server" />
    <asp:UpdatePanel ID="updatePanel" runat="server">
        <ContentTemplate>
            <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" OnRowDataBound="grid_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox TextMode="MultiLine" runat="server" ID="txtAgreement" Text='<%# Eval("Agreement") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            I have read and accepted the agreement
                            <asp:CheckBox ID="chkAgreement" AutoPostBack="true" runat="server" OnCheckedChanged="CheckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnAccept" runat="server" Text="Accept" OnClick="Accept" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

代码背后:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var items = new List<License> { new License { Agreement = "Agreement 1" }, new License { Agreement = "Agreement 2" } };
            grid.DataSource = items;
            grid.DataBind();
        }
    }

    protected void Accept(object sender, EventArgs e)
    {
    }

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            (e.Row.FindControl("btnAccept") as Button).Enabled = false;
    }

    protected void CheckedChanged(object sender, EventArgs e)
    {
        var chkAgreement = sender as CheckBox;
        Button btnAccept = null;

        if (chkAgreement != null)
        {
            var row = chkAgreement.Parent.Parent as GridViewRow;
            btnAccept = row.FindControl("btnAccept") as Button;

            if (btnAccept != null)
                if (chkAgreement.Checked)
                    btnAccept.Enabled = true;
                else
                    btnAccept.Enabled = false;
        }
    }
}

public class License
{
    public string Agreement { get; set; }
}