Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# How to search for a dynamically created HTML control

Posted on 2009-04-30
8
Medium Priority
?
232 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:thomasm1948
  • 5
  • 3
8 Comments
 

Author Comment

by:thomasm1948
ID: 24271972
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
 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 1500 total points
ID: 24272100
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
 

Author Comment

by:thomasm1948
ID: 24272295
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 24273897
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
 

Author Comment

by:thomasm1948
ID: 24273984
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
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 24274179
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
 

Author Comment

by:thomasm1948
ID: 24274629
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
 

Author Closing Comment

by:thomasm1948
ID: 31576506
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question