C#柜台需要2个按钮,点击更新 [英] c# Counter requires 2 button clicks to update

查看:163
本文介绍了C#柜台需要2个按钮,点击更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,已经一整天缠着我。

I have a problem that has been bugging me all day.

在我的code我有以下几点:

In my code I have the following:

private int rowCount
    {
        get { return (int)ViewState["rowCount"]; }
        set { ViewState["rowCount"] = value; }
    }

和按钮事件

protected void addRow_Click(object sender, EventArgs e)
    {
        rowCount = rowCount + 1;
    }

然后在Page_Load中,我读了价值,并相应地创建控件。

Then on Page_Load I read that value and create controls accordingly.

据我所知在Page_Load火灾后的按钮事件触发因此该值不更新,直到下一次回发。真正的噩梦。

I understand the button event fires AFTER the Page_Load fires so the value isn't updated until the next postback. Real nightmare.

下面是整个code:

protected void Page_Load(object sender, EventArgs e)
    {
        string xmlValue = ""; //To read a value from a database
        if (xmlValue.Length > 0)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = XMLToDataSet(xmlValue);
                Table dimensionsTable = DataSetToTable(ds);
                tablePanel.Controls.Add(dimensionsTable);

                DataTable dt = ds.Tables["Dimensions"];
                rowCount = dt.Rows.Count;
                colCount = dt.Columns.Count;
            }
            else
            {
                tablePanel.Controls.Add(DataSetToTable(DefaultDataSet(rowCount, colCount)));
            }
        }
        else
        {
            if (!Page.IsPostBack)
            {
                rowCount = 2;
                colCount = 4;
            }
            tablePanel.Controls.Add(DataSetToTable(DefaultDataSet(rowCount, colCount)));
        }
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        resultsLabel.Text = Server.HtmlEncode(DataSetToStringXML(TableToDataSet((Table)tablePanel.Controls[0])));
    }
    protected void addColumn_Click(object sender, EventArgs e)
    {
        colCount = colCount + 1;
    }

    protected void addRow_Click(object sender, EventArgs e)
    {
        rowCount = rowCount + 1;
    }

    public DataSet TableToDataSet(Table table)
    {
        DataSet ds = new DataSet();

        DataTable dt = new DataTable("Dimensions");
        ds.Tables.Add(dt);

        //Add headers
        for (int i = 0; i < table.Rows[0].Cells.Count; i++)
        {
            DataColumn col = new DataColumn();
            TextBox headerTxtBox = (TextBox)table.Rows[0].Cells[i].Controls[0];

            col.ColumnName = headerTxtBox.Text;
            col.Caption = headerTxtBox.Text;
            dt.Columns.Add(col);
        }


        for (int i = 0; i < table.Rows.Count; i++)
        {
            DataRow valueRow = dt.NewRow();
            for (int x = 0; x < table.Rows[i].Cells.Count; x++)
            {
                TextBox valueTextBox = (TextBox)table.Rows[i].Cells[x].Controls[0];
                valueRow[x] = valueTextBox.Text;
            }
            dt.Rows.Add(valueRow);
        }

        return ds;
    }

    public Table DataSetToTable(DataSet ds)
    {
        DataTable dt = ds.Tables["Dimensions"];
        Table newTable = new Table();

        //Add headers
        TableRow headerRow = new TableRow();
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            TableCell headerCell = new TableCell();
            TextBox headerTxtBox = new TextBox();
            headerTxtBox.ID = "HeadersTxtBox" + i.ToString();
            headerTxtBox.Font.Bold = true;
            headerTxtBox.Text = dt.Columns[i].ColumnName;

            headerCell.Controls.Add(headerTxtBox);
            headerRow.Cells.Add(headerCell);
        }
        newTable.Rows.Add(headerRow);

        //Add value rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TableRow valueRow = new TableRow();
            for (int x = 0; x < dt.Columns.Count; x++)
            {
                TableCell valueCell = new TableCell();
                TextBox valueTxtBox = new TextBox();
                valueTxtBox.ID = "ValueTxtBox" + i.ToString() + i + x + x.ToString();
                valueTxtBox.Text = dt.Rows[i][x].ToString();

                valueCell.Controls.Add(valueTxtBox);
                valueRow.Cells.Add(valueCell);
            }
            newTable.Rows.Add(valueRow);
        }

        return newTable;
    }

    public DataSet DefaultDataSet(int rows, int cols)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("Dimensions");
        ds.Tables.Add(dt);


        DataColumn nameCol = new DataColumn();
        nameCol.Caption = "Name";
        nameCol.ColumnName = "Name";
        nameCol.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(nameCol);

        DataColumn widthCol = new DataColumn();
        widthCol.Caption = "Width";
        widthCol.ColumnName = "Width";
        widthCol.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(widthCol);

        if (cols > 2)
        {
            DataColumn heightCol = new DataColumn();
            heightCol.Caption = "Height";
            heightCol.ColumnName = "Height";
            heightCol.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(heightCol);
        }
        if (cols > 3)
        {
            DataColumn depthCol = new DataColumn();
            depthCol.Caption = "Depth";
            depthCol.ColumnName = "Depth";
            depthCol.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(depthCol);
        }
        if (cols > 4)
        {
            int newColCount = cols - 4;
            for (int i = 0; i < newColCount; i++)
            {
                DataColumn newCol = new DataColumn();
                newCol.Caption = "New " + i.ToString();
                newCol.ColumnName = "New " + i.ToString();
                newCol.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(newCol);
            }
        }

        for (int i = 0; i < rows; i++)
        {
            DataRow newRow = dt.NewRow();
            newRow["Name"] = "Name " + i.ToString();
            newRow["Width"] = "Width " + i.ToString();
            if (cols > 2)
            {
                newRow["Height"] = "Height " + i.ToString();
            }
            if (cols > 3)
            {
                newRow["Depth"] = "Depth " + i.ToString();
            }
            dt.Rows.Add(newRow);
        }
        return ds;
    }

    public DataSet XMLToDataSet(string xml)
    {
        StringReader sr = new StringReader(xml);
        DataSet ds = new DataSet();
        ds.ReadXml(sr);

        return ds;
    }

    public string DataSetToStringXML(DataSet ds)
    {
        XmlDocument _XMLDoc = new XmlDocument();
        _XMLDoc.LoadXml(ds.GetXml());

        StringWriter sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);

        XmlDocument xml = _XMLDoc;
        xml.WriteTo(xw);
        return sw.ToString();
    }

    private int rowCount
    {
        get { return (int)ViewState["rowCount"]; }
        set { ViewState["rowCount"] = value; }
    }
    private int colCount
    {
        get { return (int)ViewState["colCount"]; }
        set { ViewState["colCount"] = value; }
    }

编辑:这是我的.aspx,以及如果你想尝试一下,在VS

Here's my .aspx as well in case you want to try it out in VS.

    <asp:Panel ID="tablePanel" runat="server" CssClass="table-panel" />
    <asp:Label ID="resultsLabel" runat="server" />
    <asp:LinkButton ID="submit" Text="submit" runat="server" onclick="submit_Click" />
    <asp:LinkButton ID="addColumn" Text="Add Column" runat="server" 
        onclick="addColumn_Click" />
    <asp:LinkButton ID="addRow" Text="Add Row" runat="server" onclick="addRow_Click" />

在此先感谢,

Thanks in advance,

马尔科

推荐答案

OK,我在的这个职务。

下面是我的最后code - 随意使用它,如果你需要基于一个计数器来创建动态控件

Here's my final code - feel free to use it if you ever need to create dynamic controls based on a counter.

此外,我不会介意的整体编码风格的反馈,我总是AP preciate他人的投入。

Also I wouldn't mind feedback on the overall coding style, I always appreciate others' input.

protected void Page_Load(object sender, EventArgs e)
    {
        string xmlValue = ""; //To read a value from a database
        if (xmlValue.Length > 0)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = XMLToDataSet(xmlValue);
                Table dimensionsTable = DataSetToTable(ds);
                tablePanel.Controls.Add(dimensionsTable);

                DataTable dt = ds.Tables["Dimensions"];
                rowCount = dt.Rows.Count;
                colCount = dt.Columns.Count;
            }
            else
            {
                tablePanel.Controls.Add(DataSetToTable(DefaultDataSet(rowCount, colCount)));
            }
        }
        else
        {
            if (!Page.IsPostBack)
            {
                rowCount = 2;
                colCount = 4;
            }
            else
            {
                if (GetPostBackControl(this.Page).ID == "addRow")
                {
                    rowCount = rowCount + 1;
                }
                else if (GetPostBackControl(this.Page).ID == "addColumn")
                {
                    colCount = colCount + 1;
                }
            }
            tablePanel.Controls.Add(DataSetToTable(DefaultDataSet(rowCount, colCount)));
        }
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        resultsLabel.Text = Server.HtmlEncode(DataSetToStringXML(TableToDataSet((Table)tablePanel.Controls[0])));
    }
    protected void addColumn_Click(object sender, EventArgs e)
    {

    }

    protected void addRow_Click(object sender, EventArgs e)
    {

    }

    public DataSet TableToDataSet(Table table)
    {
        DataSet ds = new DataSet();

        DataTable dt = new DataTable("Dimensions");
        ds.Tables.Add(dt);

        //Add headers
        for (int i = 0; i < table.Rows[0].Cells.Count; i++)
        {
            DataColumn col = new DataColumn();
            TextBox headerTxtBox = (TextBox)table.Rows[0].Cells[i].Controls[0];

            col.ColumnName = headerTxtBox.Text;
            col.Caption = headerTxtBox.Text;
            dt.Columns.Add(col);
        }


        for (int i = 0; i < table.Rows.Count; i++)
        {
            DataRow valueRow = dt.NewRow();
            for (int x = 0; x < table.Rows[i].Cells.Count; x++)
            {
                TextBox valueTextBox = (TextBox)table.Rows[i].Cells[x].Controls[0];
                valueRow[x] = valueTextBox.Text;
            }
            dt.Rows.Add(valueRow);
        }

        return ds;
    }

    public Table DataSetToTable(DataSet ds)
    {
        DataTable dt = ds.Tables["Dimensions"];
        Table newTable = new Table();

        //Add headers
        TableRow headerRow = new TableRow();
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            TableCell headerCell = new TableCell();
            TextBox headerTxtBox = new TextBox();
            headerTxtBox.ID = "HeadersTxtBox" + i.ToString();
            headerTxtBox.Font.Bold = true;
            headerTxtBox.Text = dt.Columns[i].ColumnName;

            headerCell.Controls.Add(headerTxtBox);
            headerRow.Cells.Add(headerCell);
        }
        newTable.Rows.Add(headerRow);

        //Add value rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TableRow valueRow = new TableRow();
            for (int x = 0; x < dt.Columns.Count; x++)
            {
                TableCell valueCell = new TableCell();
                TextBox valueTxtBox = new TextBox();
                valueTxtBox.ID = "ValueTxtBox" + i.ToString() + i + x + x.ToString();
                valueTxtBox.Text = dt.Rows[i][x].ToString();

                valueCell.Controls.Add(valueTxtBox);
                valueRow.Cells.Add(valueCell);
            }
            newTable.Rows.Add(valueRow);
        }

        return newTable;
    }

    public DataSet DefaultDataSet(int rows, int cols)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("Dimensions");
        ds.Tables.Add(dt);


        DataColumn nameCol = new DataColumn();
        nameCol.Caption = "Name";
        nameCol.ColumnName = "Name";
        nameCol.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(nameCol);

        DataColumn widthCol = new DataColumn();
        widthCol.Caption = "Width";
        widthCol.ColumnName = "Width";
        widthCol.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(widthCol);

        if (cols > 2)
        {
            DataColumn heightCol = new DataColumn();
            heightCol.Caption = "Height";
            heightCol.ColumnName = "Height";
            heightCol.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(heightCol);
        }
        if (cols > 3)
        {
            DataColumn depthCol = new DataColumn();
            depthCol.Caption = "Depth";
            depthCol.ColumnName = "Depth";
            depthCol.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(depthCol);
        }
        if (cols > 4)
        {
            int newColCount = cols - 4;
            for (int i = 0; i < newColCount; i++)
            {
                DataColumn newCol = new DataColumn();
                newCol.Caption = "New " + i.ToString();
                newCol.ColumnName = "New " + i.ToString();
                newCol.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(newCol);
            }
        }

        for (int i = 0; i < rows; i++)
        {
            DataRow newRow = dt.NewRow();
            newRow["Name"] = "Name " + i.ToString();
            newRow["Width"] = "Width " + i.ToString();
            if (cols > 2)
            {
                newRow["Height"] = "Height " + i.ToString();
            }
            if (cols > 3)
            {
                newRow["Depth"] = "Depth " + i.ToString();
            }
            dt.Rows.Add(newRow);
        }
        return ds;
    }

    public DataSet XMLToDataSet(string xml)
    {
        StringReader sr = new StringReader(xml);
        DataSet ds = new DataSet();
        ds.ReadXml(sr);

        return ds;
    }

    public string DataSetToStringXML(DataSet ds)
    {
        XmlDocument _XMLDoc = new XmlDocument();
        _XMLDoc.LoadXml(ds.GetXml());

        StringWriter sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);

        XmlDocument xml = _XMLDoc;
        xml.WriteTo(xw);
        return sw.ToString();
    }

    private int rowCount
    {
        get { return (int)ViewState["rowCount"]; }
        set { ViewState["rowCount"] = value; }
    }
    private int colCount
    {
        get { return (int)ViewState["colCount"]; }
        set { ViewState["colCount"] = value; }
    }
    public static Control GetPostBackControl(Page page)
    {
        Control control = null;

        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }

这篇关于C#柜台需要2个按钮,点击更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆