Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am created a dynamic gridview based on dynamic temparory table by calling SP. In ItemTemplate a used Lable child control to display values,in EditItemTmplate i used Lable and DropDowlList to display values.

Now my requirement is when ever user clicks Update link Button i need to capture the values present in all controls in all columns.

My .aspx.cs code is as follows:
gvVendorSupportDocuments.Columns.Clear();
           SupportDocumentsForVerdorPersistence sdvpSearch = new SupportDocumentsForVerdorPersistence();
           sdvpSearch.FromDate = Convert.ToDateTime(txtFromDate.Text);
           sdvpSearch.ToDate = Convert.ToDateTime(txtToDate.Text);
           sdvpSearch.InVendorID = Convert.ToInt32(txtVendorID.Text);
           DataSet ds = sdvpSearch.GetSupportDocumentsForVendor();
           dtcols = ds.Tables[0].Columns.Count;
           dtrows = ds.Tables[0].Rows.Count;
           //GridViewTemplate gvt = new GridViewTemplate(dtrows ,dtcols);
           //gvt.colsize = dtcols;
           //gvt.rowsize = dtrows;
           for (int i = 0; i < dtcols; i++)
           {
               TemplateField TF = new TemplateField();
               TF.HeaderText = Convert.ToString(ds.Tables[0].Columns[i].ColumnName);
               for (int j = 0; j <dtrows; j++)
               {
                   string colvalue = Convert.ToString(ds.Tables[0].Rows[j][i]);
                   TF.ItemTemplate = new GridViewTemplate(ListItemType.Item, colvalue, i, j, dtrows, dtcols);
                   TF.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem, colvalue, i, j, dtrows, dtcols);
                   //if (colvalue == "")
                   //    TF.Visible =Convert .ToBoolean ("false");
               }
               gvVendorSupportDocuments.Columns.Add(TF);
               //gvVendorSupportDocuments.DataSource = ds;
               //gvVendorSupportDocuments.DataBind();
           }
           gvVendorSupportDocuments.DataSource = ds;
           gvVendorSupportDocuments.DataBind();


and GridViewTemplate calss code is as follows:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Vendor
{
    
    public class GridViewTemplate:System.Web.UI.Page, ITemplate
    {
        public int iRowCount;
        public int iColumnCount;
        ListItemType templateType;
        int i;
        int j;
        static object[,] values = new object[200, 200];
        static int colNo = 0;
        static int rowNo = 0;
        public GridViewTemplate(ListItemType type, string colvalue, int colsno, int rowsno, int dtrows, int dtcols)
        {
            templateType = type;
            i= colsno;
            j = rowsno;
            //object[][] values;          
            if (colvalue == "")
            {
                values[j, i] = "N/A";
            }
            else
            {
                values[j, i] = colvalue;
            }  
            //value = colvalue;             
            iRowCount = dtrows;
            iColumnCount = dtcols;
        }
        public  void InstantiateIn(System.Web.UI.Control container)
        {
            //rowsize =Convert .ToInt32 (values )
            Literal lc = new Literal();
            Label lbl = new Label();
            LinkButton lb = new LinkButton();
            // CheckBox ckh = new CheckBox();
            //TextBox tb1 = new TextBox();
            DropDownList ddl = new DropDownList();
            ddl.Items.Add("YES");
            ddl.Items.Add("NO");
           
            switch (templateType)
            {
                case ListItemType.Header:
                    //lc.Text = "<B>" + value + "</B>";
                    lb.Text = "Edit";
                    lb.CommandName = "EditButton";
                    container.Controls.Add(lb);
                    container.Controls.Add(lc);
                    break;
                case ListItemType.Item:
                    //int noofrows=Convert .ToInt32 ( values.GetLength[0]);
                    //int noofcols =Convert .ToInt32 ( values.GetLength[1]);
                    while (rowNo < iRowCount)
                    {
                        while (colNo < iColumnCount)
                        {
                            lbl.Text = Convert.ToString(values[rowNo, colNo]);
                            colNo++;
                            break;
                        }
                        if (colNo >= iColumnCount)
                        {
                            rowNo++;
                            colNo = 0;
                        }
                        if (rowNo >= iRowCount)
                        {
                            rowNo = 0;
                        }
                        break;
                    }
                    container.Controls.Add(lbl);
                    //lbl.Text = value;
                    //ddl.Enabled = Convert.ToBoolean("False");
                    //container.Controls.Add(ckh);
                    break;
                case ListItemType.EditItem:
                    //TextBox tb = new TextBox();
                    //tb.Text = "";
                    while (rowNo < iRowCount)
                    {
                        while (colNo < iColumnCount)
                        {
                            string fielddata = Convert.ToString(values[rowNo, colNo]);
                            if (fielddata == "NO" | fielddata == "YES")
                            {
                                ddl.SelectedValue = Convert.ToString(fielddata);
                                container.Controls.Add(ddl);
                            }
                            else
                            {
                                lbl.Text = Convert.ToString(fielddata);
                                container.Controls.Add(lbl);
                            }
                            colNo++;
                            break;
                        }
                        if (colNo >= iColumnCount)
                        {
                            rowNo++;
                            colNo = 0;
                        }
                        if (rowNo >= iRowCount)
                        {
                            rowNo = 0;
                        }
                        break;
                    }
                    //container.Controls.Add(tb);
                    break;
                case ListItemType.Footer:
                    //lc.Text = "<I>" + value + "</I>";
                    container.Controls.Add(lc);
                    break;
            }
        }
    }
}

Please anybody help me to get my requirment.

Thanks in advance
Uday
Posted
Updated 28-Sep-11 0:06am
v2

1 solution

Hi,

Upto my knowledge you can able to get values for child controls from inner data control.

On only If you create itemcommand event for that dynamic data control.

Here I write some code for get child control of inner data control values

Just check as it is

ASP.NET
<form id="form1" runat="server">
<div>
    <asp:datalist id="DataList1" runat="server" width="60%" repeatcolumns="3" repeatdirection="Horizontal" xmlns:asp="#unknown">
        OnItemDataBound="DataList1_ItemDataBound">
        <HeaderTemplate>
        </HeaderTemplate>
        <itemtemplate>
            <table>
                <tr>
                    <td>
                        Head Item
                        <asp:label id="Label1" runat="server" text="<%#Container.DataItem %>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:datalist id="DataList2" runat="server" onitemcommand="DataList1_ItemCommand">
                            <HeaderTemplate>
                                <table>
                            </HeaderTemplate>
                            <itemtemplate>
                                <tr>
                                    <td>
                                        <asp:textbox id="TextBox1" runat="server">
                                    </td>
                                    <td>
                                        <asp:linkbutton id="LinkButton1" runat="server" commandname="sd" commandargument="4">
                                            <asp:label id="Label2" runat="server" text="<%#Container.DataItem %>">

                                    </td>
                                </tr>
                            </itemtemplate>
                            <footertemplate>
                                </footertemplate></table>


                    </td>
                </tr>
            </table>
        </itemtemplate>
        <footertemplate>
        </footertemplate>

</div>
</form>


And the code behind file contains

C#
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {


            List<int> str = new List<int>();
            str.Add(1);
            str.Add(2);
            str.Add(3);
            str.Add(4);
            str.Add(5);
            str.Add(6);
            
            DataList1.DataSource = str;
            DataList1.DataBind();


        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList dtlst = (DataList)e.Item.FindControl("DataList2");
            Label  lblid = (Label )e.Item.FindControl("Label1");
            List<int> str = new List<int>();
            str.Add(1);
            str.Add(2);
            str.Add(3);
            str.Add(4);
            str.Add(5);
            str.Add(6);

            dtlst.DataSource = str;
            dtlst.DataBind();

        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int cnt=e.Item.Controls.Count;
        TextBox cltr = (TextBox)e.Item.FindControl("TextBox1");
  string uname = cltr.Text;
        
    }
</int></int></int></int>


I hope you'll Understand What I did.

finally what I'm saying is If you able to create itemcommand event then your problem is solved.

Here I used datalist control you can use gridview also

All the Best.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900