Crete Table动态地控制行和控件并在回发时保留值 [英] Crete Table rows and controls dynamically and retain values on postback

查看:63
本文介绍了Crete Table动态地控制行和控件并在回发时保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我动态创建行和控件并添加到表中但是当我回发点击提交时按钮当时我无法获得表格行。





这是我的代码



来源:

Hi all,


I am creating rows and controls dynamically and adding to a table but when I do postback clicking on submit button at that time I am not able to get table rows.


Here is my code

Source:

<asp:Table id="tblList" viewstatemode="Enabled" runat="server" width="100%" cellpadding="0" cellspacing="0"> </asp:Table>







Generating rows and columns and controls dynamically and adding them.
for(int i=0; i<3;i++)
{
TableRow tblRow = new TableRow();
TableCell cell = new TableCell();
CheckBox chk = new CheckBox();

cell.Controls.Add(chk);
tblRow.Cell.Add(cell);
tblList.Rows.Add(tblRow);
}







When I click on my button 
protected void btn_Click(object sender, EventArgs e)
{
 int rows = tblList.Rows.Count; //0

}





我需要检查这个Checkbox已经检查过的方法。

我应该选择另一个不同的控件而不是表。我需要动态添加控件。

请指导我。



I need to check in this method which Checkbox has been checked.
Should I choose another different control instead of table. I need to add the controls dynamically.
Please guide me.

推荐答案

此代码用于动态创建控件和gridview ..

查看此示例代码...

This code for dynamically creating controls and gridview also..
see this example code...
public void Tally()
    {       
            DataTable dtdate = new DataTable();
            objRetailPL.status = 4;
            dtdate = objRetailBAL.GetTypeNew(objRetailPL);

            if (dtdate.Rows.Count > 0)
            {
                for (int i = 0; i < dtdate.Rows.Count; i++)
                {
                    string branchdate;
                    int totbirds; float totwt, meatyield, Avgweight;
                    DateTime datewr;

                    DataTable dtdatedetailsTable = new DataTable();
                    objRetailPL.branchdate = dtdate.Rows[i]["BranchDate"].ToString();
                    objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());

                    dtdatedetailsTable = objRetailBAL.getbradisdatewisedet(objRetailPL);

                    branchdate = dtdatedetailsTable.Rows[0]["BranchDate"].ToString();
                    totbirds = Convert.ToInt32(dtdatedetailsTable.Rows[0]["numofbirds"].ToString());
                    totwt = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalweght"].ToString());
                    meatyield = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalmeatyeild"].ToString());
                    Avgweight = Convert.ToSingle(dtdatedetailsTable.Rows[0]["AvgWeight"].ToString());

                    Table tbldynamic = new Table();
                    tbldynamic.BorderStyle = BorderStyle.Ridge;
                    tbldynamic.Width = 600;

                    TableCell tc = new TableCell();
                    TableRow tr = new TableRow();

                    Label lblbranchdate = new Label();
                    lblbranchdate.ID = "lblbrandate" + i.ToString();
                    lblbranchdate.Text = "  " + "Branch Date:" + " ";
                    tc.Controls.Add(lblbranchdate);

                    Label lblbranchdatedata = new Label();
                    lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
                    lblbranchdatedata.Text = branchdate + "   ";

                    tc.Controls.Add(lblbranchdatedata);
                    tr.Cells.Add(tc);
                    tbldynamic.Rows.Add(tr);

                    Label lbltotwt1 = new Label();
                    lbltotwt1.ID = "lbltotwt1" + i.ToString();
                    lbltotwt1.Text = "Total Weight:" + " ";
                    tc.Controls.Add(lbltotwt1);

                    Label lbltotwt1data = new Label();
                    lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
                    lbltotwt1data.Text = totwt + "   ";

                    tc.Controls.Add(lbltotwt1data);
                    tr.Cells.Add(tc);
                    tbldynamic.Rows.Add(tr);

                    tc = new TableCell();
                    tr = new TableRow();

                    Label lbltotbirds = new Label();
                    lbltotbirds.ID = "lbltotbird" + i.ToString();
                    lbltotbirds.Text = "Total Birds:" + " ";
                    tc.Controls.Add(lbltotbirds);

                    Label lbltotbirddata = new Label();
                    lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
                    lbltotbirddata.Text = totbirds + "   ";

                    tc.Controls.Add(lbltotbirddata);
                    tr.Cells.Add(tc);
                    tbldynamic.Rows.Add(tr);

                    Label lblmeatyield = new Label();
                    lblmeatyield.ID = "lblmeatyield" + i.ToString();
                    lblmeatyield.Text = "Meat Yield in Kgs:" + " ";
                    tc.Controls.Add(lblmeatyield);

                    Label lblmeatyielddata = new Label();
                    lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
                    lblmeatyielddata.Text = meatyield + "   ";

                    tc.Controls.Add(lblmeatyielddata);
                    tr.Cells.Add(tc);
                    tbldynamic.Rows.Add(tr);

                    Label lblavgweight = new Label();
                    lblavgweight.ID = "lblavgweight" + i.ToString();
                    lblavgweight.Text = "Avg Weight:" + " ";
                    tc.Controls.Add(lblavgweight);

                    Label lblavgweightdata = new Label();
                    lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
                    lblavgweightdata.Text = Avgweight + "   ";

                    tc.Controls.Add(lblavgweightdata);
                    tr.Cells.Add(tc);
                    tbldynamic.Rows.Add(tr);

                    DataTable dtdcDetails = new DataTable();
                    objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
                    dtdcDetails = objRetailBAL.dtdateTallyTable(objRetailPL);

                    GridView gv = new GridView();
                    //gv.ID = "gv" + dtdate.Rows[i]["BranchDate"].ToString();
                    gv.Width = 600;
                    Label lblTotal = new Label();
                    Label lbltotaleggs = new Label();

                    BoundField partyname = new BoundField();
                    partyname.HeaderText = "PartyName";
                    partyname.DataField = "partyname";
                    gv.Columns.Add(partyname);

                    BoundField birdtype = new BoundField();
                    birdtype.HeaderText = "Bird Type";
                    birdtype.DataField = "birdname";
                    gv.Columns.Add(birdtype);

                    BoundField totweight = new BoundField();
                    totweight.HeaderText = "Total Weight";
                    totweight.DataField = "totalweight";
                    gv.Columns.Add(totweight);

                    BoundField rateperkg = new BoundField();
                    rateperkg.HeaderText = "Rate/Kg";
                    rateperkg.DataField = "rateperkg";
                    gv.Columns.Add(rateperkg);


                    BoundField Bdcno = new BoundField();
                    Bdcno.HeaderText = "DCNo";
                    Bdcno.DataField = "dcno";
                    gv.Columns.Add(Bdcno);

                    gv.AutoGenerateColumns = false;
                    gv.ShowFooter = true;
                    gv.DataSource = dtdcDetails;
                    gv.DataBind();

                    GridViewRow footerRow = gv.FooterRow;
                    if (footerRow != null)
                    {
                        double totalSum = 0; double totalsum1 = 0;
                        foreach (DataColumn c in dtdcDetails.Columns)
                        {
                            if (c.ColumnName.ToUpper() == "TOTALWEIGHT")
                            {
                                double sum = 0; double total = 0;
                                int k = 0;
                                foreach (DataRow rw in dtdcDetails.Rows)
                                {

                                    if (dtdcDetails.Rows[k]["Birdname"].ToString() != "Eggs")
                                    {

                                        sum = sum + Convert.ToDouble(rw[c].ToString());
                                        footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName) + 1].Text = sum.ToString();
                                    }
                                    else {
                                        total = total + Convert.ToDouble(rw[c].ToString());
                                        //footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName) + 1].Text = total.ToString();
                                    }
                                    k++;
                                }

                                totalSum = Convert.ToDouble(Math.Round((totalSum + sum),2));
                                

                                totalsum1 = Convert.ToDouble(Math.Round((totalsum1 + total),2));
                               
                            }

                            else if (c.ColumnName.ToUpper() == "birdname")
                            {
                                footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName)].Text = "Sub Total";
                            }
                        }
                        lblTotal.Text = "Total Meat: " + totalSum+"Kg"+"  ";
                        lbltotaleggs.Text = "Total Eggs: " + totalsum1;
                        Style s = new Style();
                        s.BorderStyle = BorderStyle.Groove;
                        s.Font.Bold = true;
                        s.ForeColor = System.Drawing.Color.DimGray;
                        footerRow.ApplyStyle(s);

                        double totsum;
                        totsum = Convert.ToSingle(meatyield.ToString());

                        if (totsum == totalSum)
                        {
                            lblmeatyield.ForeColor = System.Drawing.Color.Green;
                            lblmeatyielddata.ForeColor = System.Drawing.Color.Green;
                        }

                        else
                        {
                            lblmeatyield.ForeColor = System.Drawing.Color.Red;
                            lblmeatyielddata.ForeColor = System.Drawing.Color.Red;
                        }

                    }
                    divmeat.Controls.Add(tbldynamic);
                    divmeat.Controls.Add(gv);
                    divmeat.Controls.Add(lblTotal);
                    divmeat.Controls.Add(lbltotaleggs);

                    LiteralControl @break = default(LiteralControl);
                    @break = new LiteralControl("<br />");
                    divmeat.Controls.Add(@break);

                    LiteralControl @break1 = default(LiteralControl);
                    @break1 = new LiteralControl("<br />");
                    divmeat.Controls.Add(@break1);
                }
            }
        }      

    }


这篇关于Crete Table动态地控制行和控件并在回发时保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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