views:

25

answers:

2

We have a win application that shows a web form in a web browser. In order to get data from this web form we are using a hidden text box and get its text using HtmlDocument object of web browser control. I want to make an abstraction of this web form that has this text box element so that other forms can use this abstraction. I made a web control and put the text box on it.I thought that if I put this control on my page it would have the text box.When i ran my application I noticed that the text box had been rendered but had its control name in its name (WebControl$TextBoxName) and its id(WebControl_TextBoxName) and the win app throw an exception since it couldn't find the element by its id(TextBoxName). So here's my question: How can I make an abstract web form/web control that has some elements on it and I can use it to make my final forms have these elements on them? (their names and ids should not be changed) Thank you for your help

A: 

Though I've never used the browser control in WinForms, I think what you want to use is a Master Page. Assuming what you're rendering in the browser control is an ASPX page, create a Master Page with the hidden text box that you want to grab your data from, and tell all of the pages you want to have that common control on to use your Master Page. When the page renders, the control id will then be "ctl00_TextBoxName". There is no way of getting around the ID concatenation, since unique IDs are needed and that's the only way to guarantee uniqueness with all the nested control abilities of ASP.NET. However, doing this will guarantee you always have that control named the same on every new form you create that inherits the Master Page. Hope that helps!

In summary (because who reads paragraphs?):

  • Create Master Page
  • Place your common control in the Master Page
  • Have your Form inherit the Master Page

You can read up on how Master Pages work in MSDN's Documentation.

Ryan Hayes
A: 

dotNet 4.0 supports static id's so they don't get mangled, read up on Client Id Mode

Alternatively, you could override the render of your control to output a standard html hidden form field with whatever ID you want, and then also add a custom property that will return the textbox that will hide the fact that it isn't an asp.net server control.

Prescott