0

I am dynamically populating the ID of a textbox which is saved in my database. I am setting it as a string when pulling the value out of a data set which determines the parameters for a search.

I now want to use the string parameter as the dynamic ID for an Attributes.Add method using a Client ID. The end result I need, is to populate a Date Picker for an ASP:Textbox, with an onfocus event.

Example as follows:

//setting the value of the item from the dataset 
 string textCode = ds.Tables["CONTROLS"].Rows[1]["TEXTCODE"].ToString();

//inserting the value for use with ClientID
textCode.Attributes.Add("onfocus", "datepicker('" + textCode.ClientID + "');");

I am getting the error of: "'string' does not contain a definition for 'ClientID' accepting a first argument of 'string' could be found (are you missing a using directive or an assembly reference")".

When I attempt to convert or set another variable to an HtmlControl which ClientID is available for as such:

HtmlControl txtCode = textCode;

I get the following error: "Cannot implicitly convert 'string' to 'System.Web.UI.HtmlControls.HtmlControl'"

How do I convert to use this dynamic ID?

Thank you in advance for your suggestions.

  • Is `txtCode` a control ID? In other words, are you looking for [FindControl()](https://msdn.microsoft.com/en-us/library/31hxzsdw.aspx)? – Frédéric Hamidi Apr 07 '16 at 18:33
  • txtCode is just a local variable I made for the conversion of textCode. – user3404204 Apr 07 '16 at 18:34
  • could you send (display over debugger) the content of your textCode variable? – Ivan Yuriev Apr 07 '16 at 18:38
  • The content of the textCode is "txtCNo" when hovered over. Essentially I have this value declared in my CONTROLS Dataset in the TEXTCODE column. When the page is built "txtCNo" is the actual ID of the ASP:Textbox. I am wanting to use the id which is currently set as a string, to be used for the Attributes.Add so i can call the dynamic ID for the datePicker JS function. – user3404204 Apr 07 '16 at 18:42
  • so, all you need is: Page.FindControl(textCode).Attributes.Add("onfocus", "datepicker('" + textCode + "');"); – Ivan Yuriev Apr 07 '16 at 19:38
  • Thanks I will try It. I'll let you know how it goes. – user3404204 Apr 07 '16 at 20:09

1 Answers1

0

More detailed answer after the comments.. I seems like you're storing the asp.net server control Id in your database, and after retrieving it from database you got textCode variable with the id of control on your page ("txtCNo").

So the first step is to find the control with this id on your page. The simple way is Page.FindControl(...) method, but you'd better look at this article for more advanced situations: Better way to find control in ASP.NET

I also noticed that you're trying to add onfocus event with call to some javascript function datepicker(''). There is a better way to send a client Id of the control that was found in first step. (For more info about server vs client IDs look at this article: https://msdn.microsoft.com/en-us/library/1d04y8ss.aspx)

So, your code should look something like:

//setting the value of the item from the dataset 
string controlId = ds.Tables["CONTROLS"].Rows[1]["TEXTCODE"].ToString();

var control = Page.FindControl(controlId);
if(control != null)
{
    control.Attributes.Add("onfocus", "datepicker('" + control.ClientID + "');");
}
Community
  • 1
  • 1
Ivan Yuriev
  • 488
  • 8
  • 14
  • I am not able to use Attributes.Add with the variable control when recreating your suggestion. Attributes.Add is not an option available. – user3404204 Apr 07 '16 at 20:42
  • This may be due the the fact that 'var control' is turning up as "null" when I debug, it does not have my returned value of 'txtCNo' at that point. – user3404204 Apr 07 '16 at 21:08