Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Repeater and text boxes inside

Reply
Thread Tools

Repeater and text boxes inside

 
 
SushiSean
Guest
Posts: n/a
 
      07-10-2006
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.
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-10-2006
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:B6F3163E-0844-4C58-8602-...
> 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.



 
Reply With Quote
 
 
 
 
SushiSean
Guest
Posts: n/a
 
      07-11-2006
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-colorFA894">
<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 <<<<<<<<<<<<<<<<<<<








 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-11-2006
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:4BC81115-B58E-4E39-B19F-...
> 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-colorFA894">
> <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 <<<<<<<<<<<<<<<<<<<

>
>
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Space between input boxes and selection boxes is not the same in Internet Explorer Stefan Mueller HTML 5 06-16-2009 02:06 PM
repeater inside a repeater problem. uncensored ASP .Net 0 10-25-2006 06:10 PM
Repeater inside a Repeater Microsoft ASP .Net Web Controls 0 08-21-2006 09:13 AM
Repeater inside a repeater....how? voidfill3d@yahoo.com ASP .Net 1 08-10-2005 01:58 PM
reading values from select boxes inside repeater simon ASP .Net 0 06-16-2005 09:54 AM



Advertisments
 



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