Home All Groups Group Topic Archive Search About
Author
10 Jul 2006 5:17 PM
SushiSean
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.

Author
10 Jul 2006 8:10 PM
Teemu Keiski
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

Show quoteHide quote
"SushiSean" <SushiS***@discussions.microsoft.com> wrote in message
news:B6F3163E-0844-4C58-8602-6283F0B043F0@microsoft.com...
> 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.
Author
11 Jul 2006 9:03 AM
SushiSean
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>

Show quoteHide quote
>>>>>>>>>>>>>>>>> Code page <<<<<<<<<<<<<<<<<<<
Author
11 Jul 2006 9:54 AM
Teemu Keiski
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






Show quoteHide quote
"SushiSean" <SushiS***@discussions.microsoft.com> wrote in message
news:4BC81115-B58E-4E39-B19F-2E663A06915B@microsoft.com...
> 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 <<<<<<<<<<<<<<<<<<<
>
>
>
>
>
>
>