如何在Gridview中获取按钮模板字段的值?

时间:2016-12-03 09:35:14

标签: c# asp.net gridview

当用户单击模板字段的按钮时,我想显示Gridview的行详细信息。我得到输出以使用模板字段在Gridview中显示按钮。但是当用户单击按钮时,它会重新加载页面并显示带有行的空模板。

c#的完整编码

string selectedColumn;
    string[] splitSelectedColumn;

    string groupByColumn;
    string[] splitGroupByColumn;

    ArrayList listBtnOrLbl = new ArrayList();

    int compareFlag = 0;
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        selectedColumn = txtColumnNames.Text;
        splitSelectedColumn = selectedColumn.Split(',');

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

        string[] compareGroup = new string[splitGroupByColumn.Length];

        //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[splitGroupByColumn.Length];
        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();
        //dt.Columns.Clear();
        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;
            string[] stackss = new string[splitSelectedColumn.Length];
            while (rd.Read())
            {
                //SET the FIRST VALUES in `stackss[]` for comparing next values of rd[]
                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;

                    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);
                    Response.Write("Execute BUTTON");
                    listBtnOrLbl.Add("button");
                }
                //stackss = new string[] { "" };
                for (int i = 0; i < stackss.Length; i++)
                {
                    stackss[i] = "";
                }
                int tempValue = 0;
                for (int i = 0; i < compareGroup.Length; i++)
                {
                    if (compareGroup[i] == rd[i].ToString())
                    {
                        tempValue = tempValue + 1;
                        if (tempValue == compareGroup.Length)
                        {
                            for (int k = 0; k < splitSelectedColumn.Length; k++)
                            {
                                stackss[k] = rd[k].ToString();
                                Response.Write("second rowsssss ==== " + stackss[k]);

                                if (k + 1 == splitSelectedColumn.Length)
                                {
                                    var row = dt.NewRow();
                                    DataRowCollection drc = dt.Rows;
                                    DataRow rowss = drc.Add(stackss);
                                    Response.Write("compare flag checking");
                                    Response.Write("Execute LABEL");
                                    listBtnOrLbl.Add("label");
                                    compareFlag = 0;
                                }
                            }
                        }
                    }
                }
                //GridView1.DataSource = dt;
                //GridView1.DataBind();
            }
            rd.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

当用户单击“刷新”按钮时,将生成Gridview模板字段。上面的编码是一个生成列和模板字段

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Response.Write("<br/>compare flag checking OVER button count=======================" + listBtnOrLbl.Count);

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int size = splitGroupByColumn.Length;
            //Button[] lnkBtn = new Button[size];
            Label[] lblData = new Label[size];

            Response.Write("<br/>Inside button count=======================" + listBtnOrLbl.Count);

            if ("button".Equals(listBtnOrLbl[j]))
            {
                for (int i = 0; i < splitGroupByColumn.Length; i++)
                {
                    Button lnkView = new Button();
                    lnkView.ID = "lnkView";
                    lnkView.Text = (e.Row.DataItem as DataRowView).Row[splitGroupByColumn[i]].ToString(); 

                    //lnkView.Text = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
                    lnkView.Click += ViewDetails;
                    lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row[splitGroupByColumn[i]].ToString();
                    e.Row.Cells[i].Controls.Add(lnkView);
                }
                j++;
            }
            else
            {
                for (int i = 0; i < splitGroupByColumn.Length; i++)
                {
                    lblData[i] = new Label();
                    lblData[i].ID = "lblView";
                    lblData[i].Text = (e.Row.DataItem as DataRowView).Row[splitGroupByColumn[i]].ToString();
                    Response.Write("<br/>PRINT label==" + lblData[i].Text);
                    e.Row.Cells[i].Controls.Add(lblData[i]);
                    //e.Row.Visible = false;
                }
                j++;
            }
     }

 protected void ViewDetails(object sender, EventArgs e)
    {
        Response.Redirect("exit.aspx"); //for testing purpose I gave like this, But this method is not calling i think when user clicks button
        Response.Write("testing....");
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('TESTING:')", true);

    }

用户单击Button之前的输出 output1 点击按钮后(第一个按钮) output2

我之前已经这样做了,但这是一个小而不是更复杂的代码。这工作正常。那我在这里做错了什么? 感谢

我认为当用户点击按钮时,ViewDetails()方法不会调用

0 个答案:

没有答案