ASP.NET - findcontrol in my page

Asked By thadews
15-Jul-11 08:32 AM
p1 = planetlist.SelectedValue.ToString();
        foreach (Control page_control in Page.Controls)
  {
    foreach (Control c in page_control .Controls)
    {
    if (c is TextBox)
    {
        if (c == (TextBox)Page.FindControl("txt1")) 
      {
          
          ((TextBox)c).Text = p1.ToString();
          
      }
      else if (c == (TextBox)Page.FindControl("TextBox1"))
      {
          ((TextBox)c).Text = p1.ToString();
      }
      else if (c == (TextBox)Page.FindControl("TextBox2"))
      {
          ((TextBox)c).Text = p1.ToString();
      }
    }
    }
  }
i wrote this thanks
but i got output in three textboxes at the same time.But i need a output in single box like this ,if control is equal to textbox1 then output is in textbox1 or if its textbox2 .then output is in textbox2.
can anyone help me to solve this problem.

  Vickey F replied to thadews
15-Jul-11 08:35 AM
Change your code like this-

 
p1 = planetlist.SelectedValue.ToString();
        foreach (Control page_control in Page.Controls)
  {
    foreach (Control c in page_control .Controls)
    {
    if (c is TextBox)
    {
        if (c == (TextBox)Page.FindControl("txt1")) 
      {
          
          ((TextBox)c).Text = p1.ToString();
      break;
          
      }
      else if (c == (TextBox)Page.FindControl("TextBox1"))
      {
          ((TextBox)c).Text = p1.ToString();
       break;   
    }
      else if (c == (TextBox)Page.FindControl("TextBox2"))
      {
          ((TextBox)c).Text = p1.ToString();
         break; 
    }
    }
    }
  }

Try this code and let me know.
  dipa ahuja replied to thadews
15-Jul-11 08:50 AM
protected void Button3_Click(object sender, EventArgs e)
  {
    string p1 = planetlist.SelectedValue.ToString();
    string value = p1;
    foreach (Control c in Page.Controls)
    {
      foreach (Control ctrl in c.Controls)
      {
        if (ctrl is TextBox)
        {
          value = value + " " + ((TextBox)ctrl).Text.ToString();
          break;
        }
      }
    }
  }
  thadews replied to Vickey F
15-Jul-11 08:51 AM
it gives output in first textbox only.it changed ih first textbox only.
if i put cursor in secondbox,the output in first textbox only but i need the output where i put my cursor (i,e) which textbox is selected.
thanks.I m new to this.pls help to solve this problem.i worked for past two days but i cant find the solution
With thanks ,
amali

  Reena Jain replied to thadews
15-Jul-11 08:52 AM
Hi,

by your code its not possible if you compare the c i.e. control is belong to all control available in c#.net, which is works as super class of all controls. so you if you will compare any control with page_control.Controls parameter it will return true.

provide you logic to help you out
  thadews replied to Reena Jain
15-Jul-11 08:59 AM
Thank you,

My logic is a horoscope it contains 11 textboxes with one dropdown list and one add and remove button.
first v want to select the dropdown item and put cursor in textbox  where we want to set that selected item then v cilck the add button it set the value to the corrospond textbox whic is selected by us. like v  select  the textboxes one by one and click button . it gives value to textbox.

but according to my code ,if we select the first text box and click add button ,First three boxes filled by the selected value(bcoz i wrote code for three boxes) but i need out put in single box which was select by me.

This is issue.can u help me pls.
With thanks,
amali
  James H replied to thadews
15-Jul-11 09:18 AM
Try this way:
 
protected void Button3_Click(object sender, EventArgs e)
  {
  string value = p1;
  foreach (Control c in Page.Controls)
  {
    foreach (Control ctrl in c.Controls)
    {
    if (ctrl is TextBox)
    {
      if (value == ((TextBox)ctrl).Text.ToString()) ;
      {
      //value mathecd
      }
    }
    }
  }
  }
Create New Account
help
faqs on masterpages Hi all, can anybody tell me the FAQs on Masterpages in asp.net. Thanks alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application A single master page defines the look and feel and standard behavior that you want for all of the
Gridview Edit, Update and Delete Hi as am new to ASP.NET i need some help in reviewing the code and find out the problem I have first of all. i wonder wat could be the problem. anyhelp would be greatly appreciated. <%@ Page Language = "C#" AutoEventWireup = "true" CodeBehind = "Default.aspx.cs" Inherits = "WebApplication2._Default" %> <!DOCTYPE html PUBLIC "- / / W3C BackColor = "White" / > <Columns> <asp:TemplateField HeaderText = "ClientID" SortExpression = "ClientID"> <ItemTemplate> <%# Eval("ClientID") %> < / ItemTemplate> <EditItemTemplate> <asp:TextBox ID = "TextBox1" runat = "server" Text = '<%# Eval("ClientID") %> ' ReadOnly = "true"> < / asp:TextBox> < / EditItemTemplate> <FooterTemplate> <asp:TextBox ID = "newTextBox1" runat = "server"> < / asp:TextBox> < / FooterTemplate> < / asp:TemplateField> <asp:TemplateField HeaderText = "Salutation" SortExpression = "Salutation"> <ItemTemplate> <%# Eval("Salutation") %> < / ItemTemplate> <EditItemTemplate> <asp TextBox ID = "TextBox2" runat = "server" Text = '<%# Eval("Salutation") %> '> < / asp:TextBox> < / EditItemTemplate> <FooterTemplate> <asp:TextBox ID = "newTextBox2
Its simple but i not able to find Sol'n I hv 10 different textbox whoes Id start from "txtmax" and then followed by 1 2 3. . I want to ToString()).Text = txtmax1.Text; } } But I m not able to, as showing error. . Plz help! ( TextBox ) this .Controls.Find( "" , true ).Text = txtmax1.text; for (int i = 1; i < 9; i++) { this.Controls["textBox" + (i + 7)].Text = txtmax.text.ToString(); } Hi You need code like this way. if (txtmax1 Text! = "" | | TextBox13.Text! = "" ) { string textboxName = "txtmax" ; for ( int i = 1; i < 11; i++) { (TextBox)(textboxName + i.ToString()).Text = txtmax.Text; } } Thank you www.codegain.com Hi, try the below if (txtmax1.Text! = "" | | TextBox13.Text! = "" ) { string textboxName = "txtmax" ; for ( int i = 2; i < = 13; i++) { TextBox txtTemp = (TextBox)FindControl("txtmax" + i.ToString()); txtTemp.Text = txtmax1.Text; } } Hi, It has following error 'System.Web.UI this[int]' has some invalid arguments for ( int i = 1; i < 9; i++) { this .Controls[ "textBox" + (i + 7)].Text = txtmax1.text.ToString(); } Hi, It display following error 'System.Web.UI.ControlCollection
How to sum and display total in a label using javascript ASP.NET I am having a grid view as follows I have written a javascript to display ID = "lblNet" runat = "Server" Text = ' <% # Eval("total") %> ' / > < / ItemTemplate > < / asp : TemplateField > protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { bindGrid(); } } void bindGrid() { SqlDataAdapter da = new SqlDataAdapter ( "select sno, code, price, qty Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } I will not get the Amount in that textbox until and unless user selects a value from dropdown and enters the quantity in the textbox available. The Amount will get displayed when User enters quantity and onblur event I will e.Row.DataItem, "Amount")); } if(e.Row.RowType = = DataControlRowType.Footer) { Label lblamount = (Label)e.Row.FindControl("lblTotal"); lblamount.Text = total.ToString(); } } Regards, give the quantity textboxes a name like "quantity", then label' ). innerHTML = "TOTAL:" + sum ; Hello, Take Gridview templatefield as following way < asp:TemplateField > < ItemTemplate > < asp:TextBox ID = "txtQuantity" onchange = "Total()" runat = "server" > < / asp:TextBox > < / ItemTemplate > < / asp:TemplateField > < asp:TemplateField > < ItemTemplate > < asp:TextBox ID = "txtRate" onchange = "Total()" runat = "server" > < / asp
ASP.NET DataList and DataRepeater Controls This is the third article in a three part series of articles pertaining to the understanding and application of ASP.NET 2.0 data controls. This article will address common functionality that may be required from ASP.NET DataList and DataRepeater data controls ASP.NET 2.0 Data Controls Author: Douglas Minnaar The intent of this series is to provide