Repeater and text boxes inside

Discussion in 'ASP .Net Web Controls' started by SushiSean, Jul 10, 2006.

  1. SushiSean

    SushiSean Guest

    Hi. I have a repeater which generates tables with few columns.
    Something likes that.

    ====Table====
    id ---Name ---- Age ---- Button
    1 Alex 32 "Update"
    2 Max 23 "Update"
    3 John 37 "Update"

    "Age" field it is a HtmlInputText cantrol, "Update" - it is imagebutton.
    Customer when open that page can change all "Age" fields and then click
    on "Update" button I need get all that new values from all fields. How I can
    do that?

    I can get new value only from current RepeaterItem, but I need from
    all other rows.

    protected void UpdateCartEvent(object sender, EventArgs e)
    {
    RepeaterItem RItem = (RepeaterItem)((Control)sender).Parent;
    foreach (Control CurItem in RItem.Controls)
    {
    if (CurItem.ID == "age")
    {
    }
    }
    }

    The problem is I can't get Parent from RepeaterItem. It should be Repeater
    but it get my null.
     
    SushiSean, Jul 10, 2006
    #1
    1. Advertising

  2. SushiSean

    Teemu Keiski Guest

    You can get Repeater for sure from repeaterItem if you use NamingContainer
    property (e.g RepeaterItem's NamingContainer property returns Repeater)
    What if you try code something like following (assuming Repeater1 is
    repeater's ID)


    //Loop through every repeater's item
    foreach(RepeaterItem rpitem in Repeater1.Items)
    {
    HtmlInputText age=(HtmlInputText)rpitem.FindControl("age");
    //Do something with the age control
    }

    In case UpdateCartEvent is executed when Update button is clicked, you could
    add to the previous code

    protected void UpdateCartEvent(object sender, EventArgs e)
    {
    RepeaterItem RItem = (RepeaterItem)((Control)sender).NamingContainer;
    Repeater rpt=(Repeater)RItem.NamingContainer;

    //Previous looping goes here
    //Loop through every repeater's item
    foreach(RepeaterItem rpitem in rpt.Items)
    {
    HtmlInputText age=(HtmlInputText)rpitem.FindControl("age");
    //Do something with the age control
    }

    }

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "SushiSean" <> wrote in message
    news:...
    > Hi. I have a repeater which generates tables with few columns.
    > Something likes that.
    >
    > ====Table====
    > id ---Name ---- Age ---- Button
    > 1 Alex 32 "Update"
    > 2 Max 23 "Update"
    > 3 John 37 "Update"
    >
    > "Age" field it is a HtmlInputText cantrol, "Update" - it is imagebutton.
    > Customer when open that page can change all "Age" fields and then click
    > on "Update" button I need get all that new values from all fields. How I
    > can
    > do that?
    >
    > I can get new value only from current RepeaterItem, but I need from
    > all other rows.
    >
    > protected void UpdateCartEvent(object sender, EventArgs e)
    > {
    > RepeaterItem RItem = (RepeaterItem)((Control)sender).Parent;
    > foreach (Control CurItem in RItem.Controls)
    > {
    > if (CurItem.ID == "age")
    > {
    > }
    > }
    > }
    >
    > The problem is I can't get Parent from RepeaterItem. It should be Repeater
    > but it get my null.
     
    Teemu Keiski, Jul 10, 2006
    #2
    1. Advertising

  3. SushiSean

    SushiSean Guest

    Thank you for your answer, but It doesn't work.
    >>> Control rpt = (Repeater)RItem.NamingContainer;

    rpt - always null

    and if I use >>> foreach (RepeaterItem CurRow in Repeater1.Items)
    it show me old values.

    Can you check my example code?

    >>>>>>>>>>>>>>>>>> Code page <<<<<<<<<<<<<<<<<<<<<<


    protected void Page_Load(object sender, EventArgs e)
    {
    // Create a new DataTable.
    System.Data.DataTable myDataTable = new DataTable("ParentTable");
    // Declare variables for DataColumn and DataRow objects.
    DataColumn myDataColumn;
    DataRow myDataRow;

    // Create new DataColumn, set DataType, ColumnName and add to DataTable.

    myDataColumn = new DataColumn();
    myDataColumn.DataType = System.Type.GetType("System.String");
    myDataColumn.ColumnName = "id";
    myDataColumn.AutoIncrement = false;
    myDataColumn.Caption = "id";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the Column to the DataColumnCollection.
    myDataTable.Columns.Add(myDataColumn);

    // Create second column.
    myDataColumn = new DataColumn();
    myDataColumn.DataType = System.Type.GetType("System.String");
    myDataColumn.ColumnName = "ParentItem";
    myDataColumn.AutoIncrement = false;
    myDataColumn.Caption = "ParentItem";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the column to the table.
    myDataTable.Columns.Add(myDataColumn);

    // Instantiate the DataSet variable.
    DataSet myDataSet = new DataSet();
    // Add the new DataTable to the DataSet.
    myDataSet.Tables.Add(myDataTable);


    myDataRow = myDataTable.NewRow();
    myDataRow["id"] = "alex";
    myDataRow["ParentItem"] = "23";
    myDataTable.Rows.Add(myDataRow);

    myDataRow = myDataTable.NewRow();
    myDataRow["id"] = "John";
    myDataRow["ParentItem"] = "19";
    myDataTable.Rows.Add(myDataRow);

    // Set the DataSource of the Repeater.
    Repeater1.DataSource = myDataTable;
    Repeater1.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    RepeaterItem RItem = (RepeaterItem)((Control)sender).NamingContainer;
    Control rpt = (Repeater)RItem.NamingContainer;
    // rpt - always nulll


    foreach (RepeaterItem CurRow in Repeater1.Items)
    {
    TextBox typeSelected = (TextBox)CurRow.FindControl("numbers");
    string txt = typeSelected.Text;
    // txt always have old values
    Response.Write("<br> txt =" + txt);

    }
    }

    <asp:Label ID="myLabel1" Runat="server"></asp:Label>
    <br>
    <asp:Label ID="myLabel2" Runat="server"></asp:Label>
    <br>
    <ASP:Repeater id="Repeater1" runat="server">

    <HeaderTemplate>
    <table style="font: 8pt verdana">
    <tr style="background-color:DFA894">
    <th> Name </th>
    <th> Age </th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr style="background-color:FFECD8">
    <td><asp:TextBox ID="numbers" runat=server>2</asp:TextBox>
    </td>
    <td><asp:Button ID="Button1" runat="server"
    OnClick="Button1_Click" Text="Button" /></td>
    </tr>
    </ItemTemplate>

    <FooterTemplate>
    </table>
    </FooterTemplate>

    </ASP:Repeater>

    >>>>>>>>>>>>>>>>> Code page <<<<<<<<<<<<<<<<<<<
     
    SushiSean, Jul 11, 2006
    #3
  4. SushiSean

    Teemu Keiski Guest

    It shows you old values because you bind the grid on every request. The code
    to bind it initially in Page_Load should be inside if(!Page.IsPostBack){}.
    Button1_Click happens after Page_Load so your databinding code clears the
    values to defaults on postback.

    Second, you should take the parent Repeater like this:

    Repeater rpt = (Repeater)RItem.NamingContainer;

    I tested your code, runned it. here's what the code-behind looks like after
    changes. It works fine in my test page.

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    // Create a new DataTable.
    System.Data.DataTable myDataTable = new
    DataTable("ParentTable");
    // Declare variables for DataColumn and DataRow objects.
    DataColumn myDataColumn;
    DataRow myDataRow;

    // Create new DataColumn, set DataType, ColumnName and add to
    DataTable.

    myDataColumn = new DataColumn();
    myDataColumn.DataType = System.Type.GetType("System.String");
    myDataColumn.ColumnName = "id";
    myDataColumn.AutoIncrement = false;
    myDataColumn.Caption = "id";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the Column to the DataColumnCollection.
    myDataTable.Columns.Add(myDataColumn);

    // Create second column.
    myDataColumn = new DataColumn();
    myDataColumn.DataType = System.Type.GetType("System.String");
    myDataColumn.ColumnName = "ParentItem";
    myDataColumn.AutoIncrement = false;
    myDataColumn.Caption = "ParentItem";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the column to the table.
    myDataTable.Columns.Add(myDataColumn);

    // Instantiate the DataSet variable.
    DataSet myDataSet = new DataSet();
    // Add the new DataTable to the DataSet.
    myDataSet.Tables.Add(myDataTable);


    myDataRow = myDataTable.NewRow();
    myDataRow["id"] = "alex";
    myDataRow["ParentItem"] = "23";
    myDataTable.Rows.Add(myDataRow);

    myDataRow = myDataTable.NewRow();
    myDataRow["id"] = "John";
    myDataRow["ParentItem"] = "19";
    myDataTable.Rows.Add(myDataRow);

    // Set the DataSource of the Repeater.
    Repeater1.DataSource = myDataTable;
    Repeater1.DataBind();
    }


    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    RepeaterItem RItem =
    (RepeaterItem)((Control)sender).NamingContainer;
    Repeater rpt = (Repeater)RItem.NamingContainer;
    // rpt - always nulll


    foreach (RepeaterItem CurRow in rpt.Items)
    {
    TextBox typeSelected = (TextBox)CurRow.FindControl("numbers");
    string txt = typeSelected.Text;
    // txt always have old values
    Response.Write("<br> txt =" + txt);

    }
    }


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke






    "SushiSean" <> wrote in message
    news:...
    > Thank you for your answer, but It doesn't work.
    >>>> Control rpt = (Repeater)RItem.NamingContainer;

    > rpt - always null
    >
    > and if I use >>> foreach (RepeaterItem CurRow in Repeater1.Items)
    > it show me old values.
    >
    > Can you check my example code?
    >
    >>>>>>>>>>>>>>>>>>> Code page <<<<<<<<<<<<<<<<<<<<<<

    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > // Create a new DataTable.
    > System.Data.DataTable myDataTable = new DataTable("ParentTable");
    > // Declare variables for DataColumn and DataRow objects.
    > DataColumn myDataColumn;
    > DataRow myDataRow;
    >
    > // Create new DataColumn, set DataType, ColumnName and add to
    > DataTable.
    >
    > myDataColumn = new DataColumn();
    > myDataColumn.DataType = System.Type.GetType("System.String");
    > myDataColumn.ColumnName = "id";
    > myDataColumn.AutoIncrement = false;
    > myDataColumn.Caption = "id";
    > myDataColumn.ReadOnly = false;
    > myDataColumn.Unique = false;
    > // Add the Column to the DataColumnCollection.
    > myDataTable.Columns.Add(myDataColumn);
    >
    > // Create second column.
    > myDataColumn = new DataColumn();
    > myDataColumn.DataType = System.Type.GetType("System.String");
    > myDataColumn.ColumnName = "ParentItem";
    > myDataColumn.AutoIncrement = false;
    > myDataColumn.Caption = "ParentItem";
    > myDataColumn.ReadOnly = false;
    > myDataColumn.Unique = false;
    > // Add the column to the table.
    > myDataTable.Columns.Add(myDataColumn);
    >
    > // Instantiate the DataSet variable.
    > DataSet myDataSet = new DataSet();
    > // Add the new DataTable to the DataSet.
    > myDataSet.Tables.Add(myDataTable);
    >
    >
    > myDataRow = myDataTable.NewRow();
    > myDataRow["id"] = "alex";
    > myDataRow["ParentItem"] = "23";
    > myDataTable.Rows.Add(myDataRow);
    >
    > myDataRow = myDataTable.NewRow();
    > myDataRow["id"] = "John";
    > myDataRow["ParentItem"] = "19";
    > myDataTable.Rows.Add(myDataRow);
    >
    > // Set the DataSource of the Repeater.
    > Repeater1.DataSource = myDataTable;
    > Repeater1.DataBind();
    >
    > }
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    >
    > RepeaterItem RItem = (RepeaterItem)((Control)sender).NamingContainer;
    > Control rpt = (Repeater)RItem.NamingContainer;
    > // rpt - always nulll
    >
    >
    > foreach (RepeaterItem CurRow in Repeater1.Items)
    > {
    > TextBox typeSelected = (TextBox)CurRow.FindControl("numbers");
    > string txt = typeSelected.Text;
    > // txt always have old values
    > Response.Write("<br> txt =" + txt);
    >
    > }
    > }
    >
    > <asp:Label ID="myLabel1" Runat="server"></asp:Label>
    > <br>
    > <asp:Label ID="myLabel2" Runat="server"></asp:Label>
    > <br>
    > <ASP:Repeater id="Repeater1" runat="server">
    >
    > <HeaderTemplate>
    > <table style="font: 8pt verdana">
    > <tr style="background-color:DFA894">
    > <th> Name </th>
    > <th> Age </th>
    > </tr>
    > </HeaderTemplate>
    > <ItemTemplate>
    > <tr style="background-color:FFECD8">
    > <td><asp:TextBox ID="numbers" runat=server>2</asp:TextBox>
    > </td>
    > <td><asp:Button ID="Button1" runat="server"
    > OnClick="Button1_Click" Text="Button" /></td>
    > </tr>
    > </ItemTemplate>
    >
    > <FooterTemplate>
    > </table>
    > </FooterTemplate>
    >
    > </ASP:Repeater>
    >
    >>>>>>>>>>>>>>>>>> Code page <<<<<<<<<<<<<<<<<<<

    >
    >
    >
    >
    >
    >
    >
     
    Teemu Keiski, Jul 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon
    Replies:
    0
    Views:
    464
    simon
    Jun 16, 2005
  2. Replies:
    1
    Views:
    7,070
    Karl Seguin
    Aug 10, 2005
  3. Stefan Mueller
    Replies:
    5
    Views:
    12,534
    jamesxa
    Jun 16, 2009
  4. uncensored
    Replies:
    0
    Views:
    852
    uncensored
    Oct 25, 2006
  5. Microsoft

    Repeater inside a Repeater

    Microsoft, Aug 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    228
    Microsoft
    Aug 21, 2006
Loading...

Share This Page