当用户刷新页面时,为什么GridView列名重复?

时间:2016-12-02 13:44:02

标签: c# asp.net gridview

我有一个GridView,我得到了我期望的输出。但是,当我单击刷新按钮而没有任何更改时,它会重复相同的列名称。如何防止重复列?。

这是我的输出正确。单击刷新按钮时,我将获得输出 Output one What i Expect

当我再次单击刷新按钮时,它将在同一网格视图中重复相同的coumns名称。 Output two which is wrong

我想知道为什么会重复以及如何防止它。

这是我的代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>

C#代码:

string selectedColumn;
    string[] splitSelectedColumn;

    string groupByColumn;
    string[] splitGroupByColumn;
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        selectedColumn = txtColumnNames.Text;
        splitSelectedColumn = selectedColumn.Split(',');

        groupByColumn = txtGroupBy.Text;
        splitGroupByColumn = groupByColumn.Split(',');

        Response.Write("group length="+splitGroupByColumn.Length);

        string[] compareGroup = new string[splitGroupByColumn.Length];
        Response.Write("inside array=" + compareGroup[0]);

        //Grouping column names using selected column text
        int flag = 1;
        foreach (string columnName in splitSelectedColumn)
        {
            flag = 1;
            foreach (string groupByName in splitGroupByColumn)
            {
                if (columnName.Equals(groupByName))
                {
                    flag = 2;
                    break;
                }
            }
            if (flag == 1)
            {
                groupByColumn = groupByColumn + "," + columnName;
            }

        }

        // CREATE A TEMPLATE FIELD AND BOUND FIELD

        BoundField bfield = new BoundField();

        TemplateField[] ttfield = new TemplateField[10];
        for (int i = 0; i < splitSelectedColumn.Length; i++)
        {
            if (i < splitGroupByColumn.Length)
            {
                ttfield[i] = new TemplateField();
                ttfield[i].HeaderText = splitGroupByColumn[i];
                GridView1.Columns.Add(ttfield[i]);
            }
            else
            {
                try
                {
                    bfield.HeaderText = splitSelectedColumn[i];
                    bfield.DataField = splitSelectedColumn[i];
                    Response.Write("<br/>BOUND FIELD==" + splitGroupByColumn[i]);
                    GridView1.Columns.Add(bfield);
                }
                catch (Exception)
                {

                }

            }
        }

        // CREATE DATA TABLE and COLUMN NAMES
        DataTable dt = new DataTable();
        for(int i=0;i<splitSelectedColumn.Length;i++)
        {
            dt.Columns.Add(splitSelectedColumn[i]);
            //Console.WriteLine(splitSelectedColumn[i]);
            System.Diagnostics.Debug.WriteLine(splitSelectedColumn[i]);
            Response.Write("<br/>DT COLUMN NAMES==" + splitSelectedColumn[i]);
        }

        //ADD ROWS FROM DATABASE
        string cs = ConfigurationManager.ConnectionStrings["connectionStringDB"].ConnectionString;

        int compareFlag = 0;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            //cmd.CommandText = "select " + selectedColumn + " FROM [RMSDemo].[dbo].[ItemRelation] where ItemLookupCode='" + txtItemLookupCode.Text + "'and ChildItemLookupCode1='" + txtChildItemLookupCode.Text + "' group by " + groupByColumn + " ";
            cmd.CommandText = "select " + selectedColumn + " FROM [RMSDemo].[dbo].[ItemRelation] group by " + groupByColumn + " ";
            con.Open();
            SqlDataReader rd = cmd.ExecuteReader();

            string addData=string.Empty;
            while (rd.Read())
            {
                if (compareFlag == 0)
                {
                    for (int i = 0; i < splitGroupByColumn.Length; i++)
                    {
                        compareGroup[i] = rd[splitGroupByColumn[i]].ToString();
                        Response.Write("<br/>COMPARE GROUP [i]==" + compareGroup[i]);
                    }

                    compareFlag = 1;
                    //string[] stack = Enumerable.Range(0, splitSelectedColumn.Length)
                    //    .Select(i => string.Empty)
                    //    .ToArray();

                    string[] stackss = new string[splitSelectedColumn.Length];
                    Response.Write("<br/>splitSelectedColumn.LENGTH==" + splitSelectedColumn.Length);
                    Response.Write("<br/>STACK.LENGTH=="+stackss.Length);
                    for (int i = 0; i < stackss.Length; i++)
                    {
                        stackss[i] = "";
                    }

                    for (int i = 0; i < compareGroup.Length;i++ )
                    {
                        stackss[i] = compareGroup[i];
                    }

                    //tESTING PURPOSE ONLY
                    for (int i = 0; i < stackss.Length; i++)
                    {
                        //stack[i] = "";
                        Response.Write("<br/>STACK.VALUES==" + stackss[i]);
                    }

                    var row = dt.NewRow();

                    DataRowCollection drc = dt.Rows;
                    DataRow rowss = drc.Add(stackss);
                    //dt.Rows.Add(rowss);

                }

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            rd.Close();
        }            
    }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int size = splitGroupByColumn.Length;
            Button[] lnkBtn = new Button[size];
            for (int i = 0; i < splitGroupByColumn.Length; i++)
            {
                lnkBtn[i] = new Button();
                lnkBtn[i].ID = "lnkView";
                lnkBtn[i].Text = (e.Row.DataItem as DataRowView).Row[splitGroupByColumn[i]].ToString();
                //Response.Write("<br/>PRINT==" + lnkBtn[i].Text);
                //lnkView.Text = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
                //lnkView.Click += ViewDetails;
                lnkBtn[i].CommandArgument = (e.Row.DataItem as DataRowView).Row[splitGroupByColumn[i]].ToString();
                e.Row.Cells[i].Controls.Add(lnkBtn[i]);
            }
}
  

我没有在页面中添加任何Cookie。但是当我尝试从浏览器中删除cookie后,我得到了输出而没有重复列

2 个答案:

答案 0 :(得分:1)

考虑实施对char *subpath = malloc(strlen(dir)+strlen(dp->d_name)+1); strcpy(subpath,dir); strcat(subpath,dp->d_name); count(subpath,...); free(subpath); 的调用。根据我的经验,在更新UI时,通常最好清除它并重新绘制。

答案 1 :(得分:0)

这是因为回发而发生的。在代码的顶部写下if statement并将所有代码放在此处:

if (!ispostback)
{

....

}