C# How to search for a dynamically created HTML control

Hi,

I have a question in which I need to try to search a web form or page for a dynamically created HTML control.  The reason why I ask this question is that I am doing some SharePoint 2007 development in which although I clearly define a ASP.NET Textbox and assign it an ID, when SharePoint displays it it converts it into an input html textbox.  The only thing that I see through the source is that it changes my id with a string of characters attached to my ID

For Example:  ASP.NET ID = ff5_1
                      SharePoint's ID for my control = ctl00_m_g_cd8acf22_5185_4f47_91d6_d207ac0c3905_ff5_1

How can I search for this control and then fill it with a value like in C# Textbox

Below is what works for a ASP.NET Textbox.  I just do not know how to do it for a Input(Text)
foreach (Control ctrl in Form.Controls)
            {
                if (ctrl is TextBox)
                {
                    _test = ((TextBox)(ctrl)).ID;
                    if (_test.Contains("Vendor"))
                    {
                        TextBox _Vendor = ((TextBox)(ctrl));
                        _Vendor.Text = "Test Vendor";
                    }
                    if (_test.Contains("Amount"))
                    {
 
                        TextBox _Amount = ((TextBox)(ctrl));
                        _Amount.Text = "1000";
                    }
                }
            }

Open in new window

thomasm1948Asked:
Who is Participating?
 
Jamie McAllister MVPSharePoint ConsultantCommented:
Hmmm. Where is this control really existing? Just in the markup rendered on the client, or at the Server so that .NET can act upon it? Without the runat=server monica, I'd say this code only exists in the Rendered markup.

As such, perhaps you need to act on this using javascript? You could output a dynamic value into your javascript from your .NET code using a <%=blah%> directive in your page, and that could be the value your javascript fills the field with.

How does the javascript find the control? Well, the ID isn't too helpful (could it change?), but you can search on several things at once. I attach some JS so you can see what I mean.
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
 
  var len = identifier.length;
 
  var tags = document.getElementsByTagName(tagName);
 
  for (var i=0; i < tags.length; i++) {
 
    var tempString = tags[i].id;
 
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == 
 
tempString.length - len)) {
 
      return tags[i];
 
    }
 
  }
 
  return null;
 
}

Open in new window

0
 
thomasm1948Author Commented:
OK Well I can get the code above to work with an HTML input text only if it says runat="server".  but unfortunently SharePoint does not put that in for the input(text)

Really need some help on this.  The mian thing that I need to do is to find the textbox and then fill it with a value.

thank you for al of your help in advance
foreach (Control ctrl in Form.Controls)
        {
 
            if (ctrl is HtmlInputText)
            {
               
                _test = ((HtmlInputText)(ctrl)).ID;
                if (_test.Contains("Text1"))
                {
                    
                    HtmlInputText _htmlText = ((HtmlInputText)(ctrl));
                    _htmlText.Value = "TEST Only Works with Runat Server";
                }
            }
        }       

Open in new window

0
 
thomasm1948Author Commented:
Is it possible that C# can create the JavaScript to find the control and then input a value in it.  if so can you please write a sample code

thank you for all of your help
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Jamie McAllister MVPSharePoint ConsultantCommented:
My earlier post details how you'd pass a value to the javascript, and gives some sample Javascript you could change to meet your requirement.

I don't have time to write the code for you I'm afraid. I'm fully engaged on assignments right now.

0
 
thomasm1948Author Commented:
I tried doing this in C# but I get a JavaScript Error

string Script = "function ShowName() { var txt=document.getElementById('Text1'); txt.value='Test');}"; //"function ShowName() { var txt=document.getElementById('Text1'); txt.value='Test');}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowName", Script, true);


In the code above I am just trying to add static text to a html textbox and I am unable to do it.  Also I would need to be able to grab that text and use it in my code behind.  It would make things easier if I could use the runat="server" but in this case I do not have control of that
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
Don't you need to get the .InnerHTML or InnerText of your element and make changes?

btw can you just explain how and why your dynamic control is being created?
0
 
thomasm1948Author Commented:
The ASP.NET textbox is created in SharePoint Designer.  Sharepoint Designer supports inline C# code.  but my code will not work because when SharePoint displays my textboxes in the browser it changes them from a ASP.NET Textbox to an input(text) box and changes the ID.  The ID however does include mine in its string.  The main draw that I am having is that Sharepoint does not put the runat="server" in the properties of the input(text) box even though I specified it through Sharepoint Designer.  So I am unable to search for through C# utilizing HtmlInputText.  If the runat=server' is there then it would work

The main goal in this project is to make an autfill option.  To do this I must take one textbox's values and then pass it through my database (SQL 2000) and then return the data and fill in the textboxes.  One you hit the save button then the data will be saved as metadata in SharePoint

I have done this before without SharePoint; just in a regular ASP.NET page and it works great.  It is just SharePoint is killing me on this.  i have not used JavaScript for about 4 years now and have done most things through C# or VB.NET

thank you for your help as it is greatly appreciated
0
 
thomasm1948Author Commented:
I figured it out.  I had to create a custom user control (ASCX) that could be called into the ShatePoint list edit form.  all custom controls override the default user controls and then have that user control search the page for the correct control
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.