ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to get the Text of a Textbox inside a repeater? Posted by Fred_silb on 5 Jan 2004 at 7:18 AM
Hello everyone,

Does anyone knows how to get the Text, that a user entered, of a Textbox inside a repeater? I'm not able to find a solution for this problem.

It seems that I can only retrieve the Text of controls with default Text and not with databound or Text that the user entered.

I'm able to retrieve the Text of a Textbox inside a repeater with a default value i.e.:

 
<asp:TextBox id="TextBox1" runat="server">asd</asp:TextBox> 


Then in codebehind I use this:

 
System.Web.UI.WebControls.TextBox MyText=Repeater1.Items[0].FindControl("TextBox1"); 


That works well but with this code it doesn't work either:

 
<asp:Label ID="Label1" Runat="server"> 
<%# DataBinder.Eval(Container.DataItem,"Key")%> 
</asp:Label> 


 
System.Web.UI.WebControls.Label MyLabel=Repeater1.Items[0].FindControl("Label1"); 


I do get the control but with the Text empty =""

If anyone has any idea please let me know.

Here it is the html code:

 
<asp:Repeater id="Repeater1" runat="server"> 
<HeaderTemplate> 
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="300" border="0"> 
</HeaderTemplate> 
<ItemTemplate> 
<tr> 
<td><asp:Label id="Label1" runat="server"> 
<%# DataBinder.Eval(Container.DataItem,"Key")%> 
</asp:Label> 
</td> 
</tr> 
<tr> 
<td> 
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox> 
</td> 
</tr> 
</ItemTemplate> 
<FooterTemplate> 
</table> 
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button> 
</FooterTemplate> 
</asp:Repeater> 


And here the codebehind:

 
private void Page_Load(object sender, System.EventArgs e) 
{ 
// Put user code to initialize the page here 
if(IsPostBack) 
{ 
} 

else 
{ 
MyHashTable=new Hashtable(2); 
MyHashTable.Add("Hello",1); 
MyHashTable.Add("Bye",2); 

this.Repeater1.DataSource=MyHashTable; 
this.Repeater1.DataBind(); 
} 
} 

private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) 
{ 
System.Web.UI.WebControls.TextBox MyTextBox=System.Web.UI.WebControls.TextBox)e.Item.FindControl("TextBox1"); 
System.Web.UI.WebControls.Label MyLabel=(System.Web.UI.WebControls.Label)e.Item.FindControl("Label1"); 
string mmmm=MyLabel.Text.ToString().TrimEnd(); 
mmmm=MyTextBox.Text.ToString().TrimEnd(); 
} 

private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) 
{ 
System.Web.UI.WebControls.TextBox MyTextBox=System.Web.UI.WebControls.TextBox)e.Item.FindControl("TextBox1"); 
System.Web.UI.WebControls.Label MyLabel=(System.Web.UI.WebControls.Label)e.Item.FindControl("Label1"); 
string mmmm=MyLabel.Text.ToString().TrimEnd(); 
mmmm=MyTextBox.Text.ToString().TrimEnd(); 
} 


Thanks a lot in advance

P.D: I don't want to assign a value to a textbox, I want an empty textbox so the user can enter something in there and then I want to retrieve what he wrote.
Report
Re: How to get the Text of a Textbox inside a repeater? Posted by iwilld0it on 7 Jan 2004 at 6:58 AM
Here is the solution to your problem. For the label control change the databind expression like so ...

From

 <asp:Label ID="Label1" Runat="server"> 
 <%# DataBinder.Eval(Container.DataItem,"Key")%> 
 </asp:Label> 


To

 <asp:Label ID="Label1" Text='<%# DataBinder.Eval(Container.DataItem,"Key")%>' Runat="server" /> 


use System.Web.UI.WebControls;
...

private void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e) 
{ 
   TextBox MyTextBox = (TextBox)Repeater1.Item[0].FindControl("TextBox1"); 
   Label MyLabel(Label)Repeater1.Item[0].FindControl("Label1"); 
   
   Response.Write(MyTextBox.Text.Trim() + "<br>");
   Response.Write(MyLabel.Text.Trim() + "<br>");
} 


Forgive me if my C# is a little off, considering i'm a VB person.

Report
Re: How to get the Text of a Textbox inside a repeater? Posted by Fred_silb on 7 Jan 2004 at 9:32 AM
Thanks for your time but what I'd like is to retrive(get) the Text that a user wrote in a Textbox that is inside a repeater.

Thanks
Report
Re: How to get the Text of a Textbox inside a repeater? Posted by Fred_silb on 7 Jan 2004 at 1:41 PM
Ok, I went to barnes and nobles and I started reading some books until I found the solution to my problem in this book:

Programming Microsoft ASP.Net
ISBN 0-7356-1903-4
by Dino Esposito

Chapeter 9 is the one that talks about the repeater and datalists.

Anyway here I'm posting the code so everyone that has the same problem can solve it:

<form id="Form1" method="post" runat="server">
			<asp:repeater id="Repeater1" runat="server">
				<HeaderTemplate>
					<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="300" border="0">
				</HeaderTemplate>
				<ItemTemplate>
					<tr>
						<td>
							<span style="display:none;">
								<%# m_MyTextBox = (TextBox) FindTextBox(Container) %>
							</span>
							<asp:TextBox runat="server" id="TextBox1" />
						</td>
					</tr>
				</ItemTemplate>
				<FooterTemplate>
					</table>
					<asp:Button id="Button1" runat="server" Text="Button" OnClick="OnSubmit"></asp:Button>
				</FooterTemplate>
			</asp:repeater>
			<asp:Label id="Label1" runat="server">Label</asp:Label>
</form>


If you check well only this needs to be added:

<span style="display:none;">
								<%# m_MyTextBox = (TextBox) FindTextBox(Container) %>
							</span>


The codebehind code is like this:

protected System.Web.UI.WebControls.TextBox m_MyTextBox;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(IsPostBack)
			{
			}

			else
			{
				MyHashTable=new Hashtable(2);

				MyHashTable.Add("Hello",1);
				MyHashTable.Add("Bye",2);

				this.Repeater1.DataSource=MyHashTable;
				this.Repeater1.DataBind();
			}
		}

			#region Web Form Designer generated code
			override protected void OnInit(EventArgs e)
			{
				//
				// CODEGEN: This call is required by the ASP.NET Web Form Designer.
				//
				InitializeComponent();
				base.OnInit(e);
			}
			
			/// <summary>
			/// Required method for Designer support - do not modify
			/// the contents of this method with the code editor.
			/// </summary>
			private void InitializeComponent()
			{    
				this.Load += new System.EventHandler(this.Page_Load);

			}
			#endregion

		protected System.Web.UI.WebControls.TextBox FindTextBox(System.Web.UI.Control container)
		{
			System.Web.UI.WebControls.TextBox MyT=(System.Web.UI.WebControls.TextBox)container.FindControl("TextBox1");

			return MyT;
		}

		protected void OnSubmit(object sender, EventArgs e)
		{
			TextBox MyT = (TextBox) Repeater1.Items[0].FindControl("TextBox1");
			Label1.Text=MyT.Text;
		}


And here is the version without codebehind for the one that dont like it:
<%@ Page language="c#" %>
<HTML>
	<HEAD>
		<title>WebForm4</title>

		<SCRIPT runat="server">
    
    TextBox m_MyTextBox;
    
    public void Page_Load(object sender, EventArgs e)
    {
		if(IsPostBack)
		{
		}
		
		else
		{
			BindData();
		}
    }
    
    private void BindData()
    {
		Hashtable MyHashTable;
		
		MyHashTable=new Hashtable(2);

		MyHashTable.Add("Hello",1);
		MyHashTable.Add("Bye",2);

		this.Repeater1.DataSource=MyHashTable;
		this.Repeater1.DataBind();
    }
    
    private TextBox FindTextBox(Control container)
    {
		TextBox MyT = (TextBox) container.FindControl("TextBox1");
		
		//if (MyT == null)
		//	return null;
		
	// Must anticipate the state restoration here. When this function executes we're
	// in the middle of the DataBind call on the given RepeaterItem object and 
	// before the ItemDataBound event fires. At this time, the Checked property of the 
	// CheckBox (as well as the Text property of a TextBox) has not been updated yet.
		//ctl.Checked = (Page.Request.Form[ctl.UniqueID]=="on");
	
		return MyT;
    }
    
    private void OnSubmit(object sender, EventArgs e)
    {
		TextBox MyT = (TextBox) Repeater1.Items[0].FindControl("TextBox1");
		Label1.Text=MyT.Text;
    }
		</SCRIPT>
	</HEAD>
	<body MS_POSITIONING="FlowLayout">
		<form id="Form1" method="post" runat="server">
			<asp:repeater id="Repeater1" runat="server">
				<HeaderTemplate>
					<div style="background-color:yellow;"><big><b>Customers</b></big></div>
					<small>Check and refresh to see more information</small><br>
				</HeaderTemplate>
				<ItemTemplate>
					<span style="display:none;">
						<%# m_MyTextBox = (TextBox) FindTextBox(Container) %>
					</span>
					<asp:TextBox runat="server" id="TextBox1" />
				</ItemTemplate>
				<FooterTemplate>
					<asp:Button id="Button1" runat="server" Text="Button" OnClick="OnSubmit"></asp:Button>
				</FooterTemplate>
			</asp:repeater>
			<asp:Label id="Label1" runat="server">Label</asp:Label></form>
	</body>
</HTML>



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.