如何在回发后保留按钮上的条件属性?

时间:2018-03-05 14:27:00

标签: c# asp.net webforms

我的问题是:

我正在创建一个包含按钮的gridview。在这些按钮中,显示某些默认文本。

在网格的最底部,有一个独特的按钮,可以创建一个额外的行onclick。

现在,在我在第一页加载时创建初始行,并且所有默认按钮属性都存在之后,我可以从下拉列表中选择一个值来更改下一个单元格中按钮的文本结束了。

但是当我点击添加新行时,该按钮文本将恢复为默认值。

然而,下拉列表的选定值不会改变。

我正在引用本教程:

http://snipplr.com/view/72801/adding-dynamic-rows-in-gridview-with-dropdownlists-in-aspnet/

但已调整代码以包含动态按钮

    private void AddNewRowToGrid()
    {

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;

                for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
                {
                    //extract the DropDownList Selected Items
                    DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList1");
                    DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2");

                    // Update the DataRow with the DDL Selected Items
                    dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
                    dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text;

                }

                //Rebind the Grid with the current data
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }


    protected void Change_Button(object sender, EventArgs e)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        GridViewRow row = (GridViewRow)((DropDownList)sender).NamingContainer;
        int index = row.RowIndex;
        Button btn = (Button)Gridview1.Rows[index].Cells[3].FindControl("btn");
        btn.Text = "HELLO";
        ViewState["CurrentTable"] = dtCurrentTable;
    }
        <asp:TemplateField HeaderText="Header 2">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="Change_Button">
            <asp:ListItem Value="-1">Select</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Header 3">

        <ItemTemplate>
            <asp:Button ID="btn" runat="server" Text="Default">
            </asp:Button>                   
        </ItemTemplate>

任何人都可以解释我是如何在回发中维护所需的文本或任何属性的吗?

这完全是为了我自己的教育。我正在尝试了解更多关于在webforms之间移动时保留页面状态的信息。

0 个答案:

没有答案