Control.Enabled如何工作?

时间:2018-07-05 14:45:17

标签: c# asp.net webforms

我花了几天时间研究各种资源,并且变得更加困惑。我在.aspx文件中有几个控件:编辑按钮,年份下拉列表和四个带有文本框和下拉列表的gridview。网格视图中的文本框和下拉列表开始禁用。当用户单击编辑按钮时,他们应该启用。第一次可以使用,但是不会再次禁用。以下是相关代码:

private void toggleEditMode()
{
    editBtn.CssClass = editBtn.Attributes["mode"].ToString() == "edit" ? "btn btn-success" : "btn btn-primary";
    editBtn.Text = editBtn.Attributes["mode"].ToString() == "edit" ? "<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;Save" : "<span class='glyphicon glyphicon-edit'></span>&nbsp;Edit";
    editBtn.Attributes["mode"] = editBtn.Attributes["mode"].ToString() == "edit" ? "save" : "edit";

    selectYear.Enabled = !selectYear.Enabled;
    foreach (GridView gv in panels)
    {
        foreach (GridViewRow gvr in gv.Rows)
        {
            TextBox name = (TextBox)gvr.FindControl("nameTB");
            DropDownList rating = (DropDownList)gvr.FindControl("ratingDDL");

            name.Enabled = !name.Enabled;
            rating.Enabled = !rating.
        }
    }
}

“编辑”按钮将正确变为“保存”按钮,并且年份下拉列表可正确切换,但不会禁用gridview中的文本框和下拉列表。在调试过程中,我发现在此方法的开头,每个文本框和DDL的Enabled属性为false。

所有文本框和DDL均开始被禁用,单击按钮后启用,即使按钮和年份DDL正确切换,也不会禁用。

我的问题是:Enabled属性到底是如何工作的?任何帮助将不胜感激。

编辑:这是标记:

<asp:LinkButton ID="editBtn" runat="server" ClientIDMode="Static" OnClick="ToggleEditMode" CssClass="btn btn-primary" mode="edit">
    <span class="glyphicon glyphicon-edit"></span>&nbsp;Edit
</asp:LinkButton>

<div class="form-inline" role="form">
    <fieldset>
        <div class="form-group">
            <label for="selectYear">Year:&nbsp;</label>
            <asp:DropDownList ID="selectYear" runat="server" CssClass="form-control" AutoPostBack="true" ClientIDMode="Static"></asp:DropDownList>
        </div>
    </fieldset>
</div>

这是gridview:

<asp:GridView ID="jrSchools1a2aAdmin" runat="server" AutoGenerateColumns="false" GridLines="None" ClientIDMode="Static" OnRowCreated="BindRatings" CssClass="table table-striped table-bordered">
    <Columns>        
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:TextBox ID="nameTB" runat="server" Text='<%# Eval("name") %>' ClientIDMode="Static" schoolID='<%# Eval("schoolID") %>' Enabled="false" CssClass="form-control"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rating">
            <ItemTemplate>
                <asp:DropDownList ID="ratingDDL" runat="server"  SelectedValue='<%# Eval("rating") %>' ClientIDMode="Static" Enabled="false" CssClass="form-control"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="students" HeaderText="Students" />
        <asp:BoundField DataField="7_1" HeaderText="7-I" />
        <asp:BoundField DataField="7_2" HeaderText="7-II" />
        <asp:BoundField DataField="8_1" HeaderText="8-I" />
        <asp:BoundField DataField="8_2" HeaderText="8-II" />
        <asp:BoundField DataField="open" HeaderText="Open" />
        <asp:BoundField DataField="score" HeaderText="Score" />
    </Columns>
</asp:GridView>

ToggleEditMode检查是否保存,运行SQL查询是否保存并调用toggleEditMode()

编辑2:在此处调用toggleEditMode()。对困惑感到抱歉。在其他任何地方都没有。

protected void ToggleEditMode(object sender, EventArgs e)
{
    if (editBtn.Attributes["mode"].ToString() == "save")
    {
        StringBuilder query = new StringBuilder();
        List<SQLParameter> parameters = new List<SQLParameter>();

        //Determine the year
        int year;
        int.TryParse(selectYear.SelectedItem.Value, out year);
        parameters.Add(new SQLParameter("@year", year));

        // Use a counter so we can enumerate parameter names
        int i = 0;
        foreach (GridView gv in panels)
        {
            foreach (GridViewRow gvr in gv.Rows)
            {
                TextBox name = (TextBox)gvr.FindControl("nameTB");
                DropDownList rating = (DropDownList)gvr.FindControl("ratingDDL");

                name.CssClass = "form-control green";

                //SQL statements here

                parameters.Add(new SQLParameter(String.Format("@name{0}", i), name.Text));
                parameters.Add(new SQLParameter(String.Format("@schoolID{0}", i), name.Attributes["schoolID"].ToString()));
                parameters.Add(new SQLParameter(String.Format("@rating{0}", i), rating.SelectedValue));

                i++;
            }
        }

        SqlConn.doQuery(query.ToString(), parameters);

        //populateTables();
    }
    toggleEditMode();
}

1 个答案:

答案 0 :(得分:0)

就像评论中提到的@mjw一样,我在标记中设置了Enabled='false'。由于page life cycle,控件始终呈现为Enabled='false'。如果可以根据条件启用/禁用控件,则最好在Page_Load函数中进行处理。但是,ASP.NET具有内置在GridView控件中的编辑功能,并且这些功能应该优于AJAX提交,因为ASP.NET具有固有的安全优势。以下是一些可帮助您入门的链接:

Making a column editable in an ASP.net GridView

https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms972948(v=msdn.10)