ASP.Net - Loop all HTML components in the current page - c#

I wish I could go all the elements of an ASP.Net page (HTML elements) and manipulate its text property / value. How could I do that?
I've been checking, and the property this.Page.Form.Controls apparently gets all the elements, but I would only page elements that caught the event. Does anyone have any idea.
I'll put an example of code that I'm currently studying and trying adpater for my needs.
Thanks
string x = String.Empty;
string y = String.Empty;
foreach (Control ctrl in this.Page.Form.Controls){
if (ctrl is TextBox){
x += ((TextBox)(ctrl)).ID + ((TextBox)(ctrl)).Parent + "\n";
} else {
y += ctrl.GetType().Name + ctrl.GetType().Namespace + "\n";
}
}
Obs.: I am using some components of the Telerik components.

The controls on a page are stored in a tree data structure. You could use a recursive method to do this:
private void SetText(ControlCollection controls, String textToSet)
{
foreach(Control c in controls)
{
if (c is ITextControl)
((ITextControl)c).Text = textToSet;
if (c.HasControls())
SetText(c.Controls, textToSet);
}
}
and you would call this method somewhere like let's say, in the OnPreRender Event (to make sure that you get all controls that have been added to the page) and pass the Page.Controls ControlCollection:
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
SetText(Page.Controls, "new text");
}

I'll have a stab at an answer, although I'm not sure it's what you're after (see my comments to the question).
If what you want to do is to quickly handle all the TextBoxes of a form (say) in one manner, and all of its DropDownLists in another manner, then what you're looking for may be OfType
DoSomethingWithAllTextBoxes( Page.Form.Controls.OfType<TextBox>() );
DoSomethingWithAllDropDownLists( Page.Form.Controls.OfType<DropDownList>() );
private void DoSomethingWithAllTextBoxes( IEnumerable<TextBox> textboxes) {
foreach(TextBox txt in textboxes) {
txt.Text = "Modified";
}
}
If you're looking for event binding, you could run code like this one in OnInit
protected override void OnInit(EventArgs e) {
foreach(TextBox txt in Page.Form.Controls.OfType<TextBox>() ) {
txt.OnTextChanged += TextChangedEventListener;
}
base.OnInit(e);
}

Attach the elements you want to an event and then act of them when its fired. This example is for demonstration but you could define a custom event.
ASPX:
<asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:TextBox ID="TextBox2" AutoPostBack="true" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
Code-Behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
//do something
}

Related

How to find all textboxes in a gridview row?

I have a GridView on a website, that have different controls in each row (eg.: textbox, label, dropdownlist). I need to find all textboxes and set the enabled property to false, so the user won't be able to edit them. I tried the code below, but it dosn't work, 'c' never recognised as a textbox, so it never changes the property.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (a)
{
foreach (Control c in e.Row.Controls)
{
if (c is TextBox)
{
((TextBox)(c)).Enabled = false;
}
}
}
}
I think you should try like this:
TextBox tb = e.Row.FindControl("textbox_name") as TextBox;
tb.Enabled = false;
Your textboxes must be nested within other controls, most likely cells inside the row. That is why you cannot find them just iterating through immediate children.
If you have a list of IDs of the text boxes, you should use FindControl:
((TextBox)e.Row.FindControl("TextBoxID")).Enabled = false;
Otherwise you will need to recursively find your controls of necessary type. See this thread for code sample.
One more option, if a is relatively easy to calculate, is to use in the markup directly, like so:
<asp:TextBox ... Enabled='<%# a %>' />
This depends a lot on the details of how a is derived. If it is a protected or public field of the page class, just the code above should work. If it is calculated based on row, you may need to turn it into protected method and pass params into it:
Enabled='<%# GetEnabled(Eval("Prop1"), Eval("Prop2")) %>'
And also want to put some updation.
There are different types of rows in gridview (header,footer,datarow,etc)
so for making little faster for the control find.
Try below (check the if condition)
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the TextBox control.
TextBox txtName = (e.Row.FindControl("txtName") as TextBox);
txtName.Enabled = false;
//or
TextBox txtName1 = (TextBox)e.Row.FindControl("txtName");
txtName1.Enabled = false;
}
}
You should search controls inside the cell instead of Row.
foreach (TableCell cell in e.Row.Cells)
{
foreach (Control c in cell.Controls)
{
if (c is TextBox)
{
((TextBox)(c)).Enabled = false;
}
}
}

Label Does Not Change After TextBox OnTextChanged Event ASP.NET

I have a username TextBox and a Label which should update to (V or X) when the TextBox text is changed. The label is updated only if, for example I press a button which automatically refreshes the page.
Here is the code:
<asp:TextBox ID="username" runat="server" OnTextChanged="checkUsername" Width="80%"></asp:TextBox>
<asp:Label ID="usernameCheck" runat="server" CssClass="checkL"></asp:Label>
And the aspx.cs
protected void checkUsername(object sender, EventArgs e)
{
if (username.Text.Length < 3 || username.Text.Length > 15)
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
if (myBl.checkUsername(Convert.ToString(username)))
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
//---Label = V (in green)
usernameCheck.Text = "\u2713";
}
}
}
Thanks for any help.
You need to add AutoPostBack="true" to your TextBox. This will cause it to post back and for that server side event to fire.
There are much better ways of doing what you are trying to accomplish though, most of which do not require a full page postback. I would try to make an AJAX call using the javascript change event, and using something like a callback method.

Looping through Insert Template in Formview to Obtain Controls Id returns nothing

I have a formview with some textboxes. The value of these textboxes need to be encrypted at the time of item inserting before saving into the db table.
I have been trying the below code with various variations with out any luck. It doent return anything or ends up with object reference null message.
protected void formview1_iteminserting(object sender, FormViewInsertEventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
foreach (Control c in FormView1.Controls)
{
if (c is TextBox)
{
TextBox txtBox = (TextBox)c;
Response.Write(txtBox.ID);
}
} }
In this case you could do a trick to achieve this using a loop:
Remember
If you have used the VS wizard to populate your FormView data control using a SqlDataSource then your text-box controls's ID will be look like this: [Name of Data Field] + TextBox
//auto generated by VS Wizard
<asp:TextBox Text='<%# Bind("NameText") %>' ID="NameTextBox" />
But if you have created them yourself you could still follow this rule to name your control IDs the same as above. and this is important for our trick.
Now you could simply use this code to get all of your text-box controls's value.
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
foreach (dynamic value in e.Values)
{
TextBox txtBox = FormView1.FindControl(value.Key + "TextBox") as TextBox;
if (txtBox != null)
{
Response.Write(txtBox.ID);
}
}
}
Note There won't be any exceptions if the control not found because it will be automatically handled by the as operator keyword for you ;-)

Persistent dynamic control in ASP.Net

<asp:Button onclick="Some_event" Text="Add TextBox" ID="id1" runat="server" />
//once clicked:
<asp:TextBox ID="txt1" ......></asp:TextBox>
//when clicked again:
<asp:TextBox ID="txt1" ......></asp:TextBox>
<asp:TextBox ID="txt2" ......></asp:TextBox>
//and so on...
Is there a way to create dynamic controls which will persist even after the postback? In other words, when the user clicks on the button, a new textbox will be generated and when clicks again the first one will remain while a second one will be generated. How can I do this using asp.net ? I know that if I can create the controls in the page_init event then they will persist but I dont know if it possible to handle a button click before the page_init occurs, therefore there must be another way.
Yes, this is possible. One way to do this using purely ASP.NET (which seems like what you're asking for) would be to keep a count of the TextBox controls that you have added (storing that value in the ViewState) and recreate the TextBox controls in the Page_Load event. Of course, nowadays most people would probably use Javascript or jQuery to handle this task client side, but I put together a quick example to demonstrate how it works with postbacks:
Front page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicControls.aspx.cs" Inherits="MyAspnetApp.DynamicControls" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAddTextBox" runat="server" Text="Add" OnClick="btnAddTextBox_Click" />
<asp:Button ID="btnWriteValues" runat="server" Text="Write" OnClick="btnWriteValues_Click" />
<asp:PlaceHolder ID="phControls" runat="server" />
</div>
</form>
</body>
</html>
Code behind:
using System;
using System.Web.UI.WebControls;
namespace MyAspnetApp
{
public partial class DynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Recreate textbox controls
if(Page.IsPostBack)
{
for (var i = 0; i < TextBoxCount; i++)
AddTextBox(i);
}
}
private int TextBoxCount
{
get
{
var count = ViewState["txtBoxCount"];
return (count == null) ? 0 : (int) count;
}
set { ViewState["txtBoxCount"] = value; }
}
private void AddTextBox(int index)
{
var txt = new TextBox {ID = string.Concat("txtDynamic", index)};
txt.Style.Add("display", "block");
phControls.Controls.Add(txt);
}
protected void btnAddTextBox_Click(object sender, EventArgs e)
{
AddTextBox(TextBoxCount);
TextBoxCount++;
}
protected void btnWriteValues_Click(object sender, EventArgs e)
{
foreach(var control in phControls.Controls)
{
var textBox = control as TextBox;
if (textBox == null) continue;
Response.Write(string.Concat(textBox.Text, "<br />"));
}
}
}
}
Since you are recreating the controls on each postback, the values entered into the textboxes will be persisted across each postback. I added btnWriteValues_Click to quickly demonstrate how to read the values out of the textboxes.
EDIT
I updated the example to add a Panel containing a TextBox and a Remove Button. The trick here is that the Remove button does not delete the container Panel, it merely makes it not Visible. This is done so that all of the control IDs remain the same, so the data entered stays with each TextBox. If we were to remove the TextBox entirely, the data after the TextBox that was removed would shift down one TextBox on the next postback (just to explain this a little more clearly, if we have txt1, txt2 and txt3, and we remove txt2, on the next postback we'll create two textboxes, txt1 and txt2, and the value that was in txt3 would be lost).
public partial class DynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
for (var i = 0; i < TextBoxCount; i++)
AddTextBox(i);
}
}
protected void btnAddTextBox_Click(object sender, EventArgs e)
{
AddTextBox(TextBoxCount);
TextBoxCount++;
}
protected void btnWriteValues_Click(object sender, EventArgs e)
{
foreach(var control in phControls.Controls)
{
var panel = control as Panel;
if (panel == null || !panel.Visible) continue;
foreach (var control2 in panel.Controls)
{
var textBox = control2 as TextBox;
if (textBox == null) continue;
Response.Write(string.Concat(textBox.Text, "<br />"));
}
}
}
private int TextBoxCount
{
get
{
var count = ViewState["txtBoxCount"];
return (count == null) ? 0 : (int) count;
}
set { ViewState["txtBoxCount"] = value; }
}
private void AddTextBox(int index)
{
var panel = new Panel();
panel.Controls.Add(new TextBox {ID = string.Concat("txtDynamic", index)});
var btn = new Button { Text="Remove" };
btn.Click += btnRemove_Click;
panel.Controls.Add(btn);
phControls.Controls.Add(panel);
}
private void btnRemove_Click(object sender, EventArgs e)
{
var btnRemove = sender as Button;
if (btnRemove == null) return;
btnRemove.Parent.Visible = false;
}
}
I read an article by Scott Mitchell that explains that ViewState only persists changed control state across post-back, and not the actual controls themselves. I did not have your exact scenario, but a project I was working on required dynamically added user controls and I had to add them on every postback. In that case, it is still useful to create them in Init so that they can retain their state. Here is the link: Understanding ASP.NET View State. Check section “View State and Dynamically Added Controls”.
You may have to keep track of all the controls that you are adding (in session state for example) and re-create them on post back. I just did a small test where I keep a List<string> of all the Textbox ids in Session. On postback, I recreate all the textboxes.

How to get the bound item from within an ASP.NET repeater

I have to set a LinkButton's OnClientClick attribute but I don't know what this value is until the LinkButton is bound to. I'm trying to set the value when the repeater binds, but I can't workout how to get the 'boundItem/dataContext' value...
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton Text="HelloWorld" ID="Hyper1" runat="server" OnDataBinding="Repeater1_DataBinding" >
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<TestObject>();
list.Add(new TestObject() {TestValue = "testing1"});
list.Add(new TestObject() { TestValue = "testing2" });
list.Add(new TestObject() { TestValue = "testing3" });
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
public void Repeater1_DataBinding(object sender, EventArgs e)
{
var link = sender as HyperLink;
//link.DataItem ???
}
Is there anyway to find out what the current rows bound item is?
Maybe you need to use ItemDataBound event. It provides RepeaterItemEventArgs argument which has DataItem available
this.Repeater1.ItemDataBound += Repeater1_ItemDataBound;
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var dataItem = e.Item.DataItem;
}
I assume you are trying to get the value for the row that is currently being databound?
You can change your function to:
public void Repeater1_DataBinding(object sender, EventArgs e)
{
var link = sender as HyperLink;
string valueYouWant = Eval("TestValue").ToString();
// You could then assign the HyperLink control to whatever you need
link.Target = string.Format("yourpage.aspx?id={0}", valueYouWant);
}
valueYouWant now has the value of the field TestValue for the current row that is being databound. Using the DataBinding event is the best way to do this compared to the ItemDataBound because you don't have to search for a control and localize the code specifically to a control instead of a whole template.
The MSDN library had this as a sample event handler:
public void BindData(object sender, EventArgs e)
{
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView) container.DataItem)[column].ToString();
}
(see http://msdn.microsoft.com/en-us/library/system.web.ui.control.databinding.aspx)
As you can see it is a simple demonstration of how to access the data item and get data from it. Adapting this to your scenario is an exercise left to the reader. :)

Categories