0

我有一个网格视图。gridview 有一个保存文本的文本框多行。之后,我有一个名为已批准的图像按钮的列,一个名为未批准的列。我想在单击批准或未批准之前强制用户阅读多行框中的文本。我怎样才能以编程方式实现这一目标?我知道我应该创建一个 Rowdatabound 事件,但我应该如何处理代码?我正在使用 c# ASP.NET Web 应用程序。

4

1 回答 1

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; }
}
于 2013-04-11T11:14:45.867 回答