Dynamic columns and NamingContainer

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Dynamic columns and NamingContainer

    I dynamically add columns to DataGrid as described in MSDN
    articles "Top Questions about the DataGrid Web Server Control"
    and "Creating Web Server Control Templates Programmatically".
    The columns are template based and all they use a same template
    producer. In the edit mode (ListItemType.EditItem) the template
    adds textbox and validator controls to a column as:

    public void InstantiateIn(System.Web.UI.Control container)
    {
    TextBox tb = new TextBox();
    tb.Text = "";
    tb.ID = "MyTextBox";
    container.Controls.Add(tb);
    CompareValidator val = new CompareValidator();
    val.ControlToValidate(tb.ID);
    container.Controls.Add(val);
    }

    During run-time this method throws exception like "... multiple controls
    have
    same ID: MyTextBox...". Of course, if there was no need to use the
    validator,
    I probably will not touch ID of the textbox and leave it with a default
    value (empty?).
    But the validator requires this ID for its ControlToValidate property.
    Any workarounds?

    P.S. In above method, the "container" parameter is type of TableCell.
    But its Parent and NamingContainer properties are undefined (I checked
    it during debugging with QuickWatch). Why?
    I expected that TableCell's Parent will be DataGridItem and NamingContainer
    will be DataGridItem or DataGrid itself. I ask it because I want to
    reference
    a parent column from above InstantiateIn() method to create IDs based on
    a column information (column index or something else).







    Tumurbaatar S. Guest

  2. Similar Questions and Discussions

    1. create dynamic columns
      Any one knows how to display sentence in 3 colums in coldfusion . Thank you!
    2. Dynamic Columns
      I am working on a query builder, where I am trying to let a user specify any combination of several checkboxes, which are then passed to a variable...
    3. Datagrid Dynamic Columns
      This is my problem I have a data grid with about twenty columns, I have a dropdown list that allows the user to select the view they want for the...
    4. Following Examples, Still Can't Add Dynamic Columns
      Greetings, I am unable to dynamically add columns to a DataGrid in a web user control (.ascx) I am creating. This applies to VS.NET 2003 /...
    5. Dynamic columns creation
      Hi. I have a stored procedure that among other things returns a column with values that will be used to compose a temporary table. The number of...
  3. #2

    Default RE: Dynamic columns and NamingContainer

    The error you got means that you instantiated more than one column using the
    same Template that contained more than one Textbox in the datagriditem with
    the same ID. You can solve that by assigning the ID upon databinding:
    1- Modify the InstaniateIn method to look like this:

    TextBox tb = new TextBox();
    container.Controls.Add(tb);
    RequiredFieldValidator val = new RequiredFieldValidator ();
    container.Controls.Add(val);
    container.DataBinding += new EventHandler(ItemContainer_DataBinding);

    2- Add another method like this:
    void ItemContainer_DataBinding(object sender, EventArgs e)
    {
    TableCell td= (TableCell)sender; //gets the container
    //gets the naming container that you wanted
    DataGridItem dgItem = ((DataGridItem )td.NamingContainer );
    //get the cell # within the tablerow
    int cellNo=-0;
    foreach (TableCell tc in dgItem.Cells)
    {
    if (tc == td) break;
    cellNo++;
    }
    //the first control within the cell is the textbox
    TextBox tb = td.Controls [0] as TextBox ;
    //assign a unique ID for each cell within the same TableRow
    tb.ID = "MyTextBox" + cellNo;
    //the validator is the second control within the container
    RequiredFieldValidator val = td.Controls[1] as RequiredFieldValidator;
    //set the validator properties
    val.ControlToValidate = tb.ID;
    val.ErrorMessage = "Cannot leave blank";
    //add a value to databind to the TextBox
    tb.Text = ((DataRowView)dgItem.DataItem)[0].ToString ();
    }
    --
    HTH,
    Phillip Williams
    [url]http://www.societopia.net[/url]
    [url]http://www.webswapp.com[/url]


    "Tumurbaatar S." wrote:
    > I dynamically add columns to DataGrid as described in MSDN
    > articles "Top Questions about the DataGrid Web Server Control"
    > and "Creating Web Server Control Templates Programmatically".
    > The columns are template based and all they use a same template
    > producer. In the edit mode (ListItemType.EditItem) the template
    > adds textbox and validator controls to a column as:
    >
    > public void InstantiateIn(System.Web.UI.Control container)
    > {
    > TextBox tb = new TextBox();
    > tb.Text = "";
    > tb.ID = "MyTextBox";
    > container.Controls.Add(tb);
    > CompareValidator val = new CompareValidator();
    > val.ControlToValidate(tb.ID);
    > container.Controls.Add(val);
    > }
    >
    > During run-time this method throws exception like "... multiple controls
    > have
    > same ID: MyTextBox...". Of course, if there was no need to use the
    > validator,
    > I probably will not touch ID of the textbox and leave it with a default
    > value (empty?).
    > But the validator requires this ID for its ControlToValidate property.
    > Any workarounds?
    >
    > P.S. In above method, the "container" parameter is type of TableCell.
    > But its Parent and NamingContainer properties are undefined (I checked
    > it during debugging with QuickWatch). Why?
    > I expected that TableCell's Parent will be DataGridItem and NamingContainer
    > will be DataGridItem or DataGrid itself. I ask it because I want to
    > reference
    > a parent column from above InstantiateIn() method to create IDs based on
    > a column information (column index or something else).
    >
    >
    >
    >
    >
    >
    >
    >
    Phillip Williams Guest

  4. #3

    Default Re: Dynamic columns and NamingContainer

    Thank you!
    I also found something like your recommendation.
    My DataGrid already uses ItemCreated event so
    I put there a code that sets these IDs.
    Thank you again!

    "Phillip Williams" <Phillip.Williams@webswapp.com> wrote in message
    news:5D50E093-95EE-4395-B095-8A525624850B@microsoft.com...
    > The error you got means that you instantiated more than one column using
    > the
    > same Template that contained more than one Textbox in the datagriditem
    > with
    > the same ID. You can solve that by assigning the ID upon databinding:
    > 1- Modify the InstaniateIn method to look like this:
    >
    > TextBox tb = new TextBox();
    > container.Controls.Add(tb);
    > RequiredFieldValidator val = new RequiredFieldValidator ();
    > container.Controls.Add(val);
    > container.DataBinding += new EventHandler(ItemContainer_DataBinding);
    >
    > 2- Add another method like this:
    > void ItemContainer_DataBinding(object sender, EventArgs e)
    > {
    > TableCell td= (TableCell)sender; //gets the container
    > //gets the naming container that you wanted
    > DataGridItem dgItem = ((DataGridItem )td.NamingContainer );
    > //get the cell # within the tablerow
    > int cellNo=-0;
    > foreach (TableCell tc in dgItem.Cells)
    > {
    > if (tc == td) break;
    > cellNo++;
    > }
    > //the first control within the cell is the textbox
    > TextBox tb = td.Controls [0] as TextBox ;
    > //assign a unique ID for each cell within the same TableRow
    > tb.ID = "MyTextBox" + cellNo;
    > //the validator is the second control within the container
    > RequiredFieldValidator val = td.Controls[1] as RequiredFieldValidator;
    > //set the validator properties
    > val.ControlToValidate = tb.ID;
    > val.ErrorMessage = "Cannot leave blank";
    > //add a value to databind to the TextBox
    > tb.Text = ((DataRowView)dgItem.DataItem)[0].ToString ();
    > }
    > --
    > HTH,
    > Phillip Williams
    > [url]http://www.societopia.net[/url]
    > [url]http://www.webswapp.com[/url]
    >
    >
    > "Tumurbaatar S." wrote:
    >
    >> I dynamically add columns to DataGrid as described in MSDN
    >> articles "Top Questions about the DataGrid Web Server Control"
    >> and "Creating Web Server Control Templates Programmatically".
    >> The columns are template based and all they use a same template
    >> producer. In the edit mode (ListItemType.EditItem) the template
    >> adds textbox and validator controls to a column as:
    >>
    >> public void InstantiateIn(System.Web.UI.Control container)
    >> {
    >> TextBox tb = new TextBox();
    >> tb.Text = "";
    >> tb.ID = "MyTextBox";
    >> container.Controls.Add(tb);
    >> CompareValidator val = new CompareValidator();
    >> val.ControlToValidate(tb.ID);
    >> container.Controls.Add(val);
    >> }
    >>
    >> During run-time this method throws exception like "... multiple controls
    >> have
    >> same ID: MyTextBox...". Of course, if there was no need to use the
    >> validator,
    >> I probably will not touch ID of the textbox and leave it with a default
    >> value (empty?).
    >> But the validator requires this ID for its ControlToValidate property.
    >> Any workarounds?
    >>
    >> P.S. In above method, the "container" parameter is type of TableCell.
    >> But its Parent and NamingContainer properties are undefined (I checked
    >> it during debugging with QuickWatch). Why?
    >> I expected that TableCell's Parent will be DataGridItem and
    >> NamingContainer
    >> will be DataGridItem or DataGrid itself. I ask it because I want to
    >> reference
    >> a parent column from above InstantiateIn() method to create IDs based on
    >> a column information (column index or something else).
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>

    Tumurbaatar S. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139