Add a button to a webpart that enables a text box (C#), getting an error: Cannot implicitly convert type 'bool' to 'string'

StackOverflow https://stackoverflow.com/questions/18988096

Question

I have added a button to webpart - I want the functionality to enable the disabled text box next to it. On Deploy, I am getting an error: Cannot implicitly convert type 'bool' to 'string'

When I convert to string to try to overcome the issue (JDHFBtn.OnClientClick = Convert.ToString(JDHFTxt.Enabled = true);), it makes the text box always editable. I am looking to resolve the issue. Please forgive my ignorance - I am new to C# and web parts, and am not much advanced in programming. Thank you.

Here is the code in question:

    sb.Append("</td>");
    sb.Append("         </tr>");
    sb.Append("         <tr class=\"row2\">");
    sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">SPR/JDHF Allotment:</td>");
    sb.Append("             <td class = \"rowText\" td width=\"75%\">"); 
                                lc3 = new LiteralControl(sb.ToString());
                                Controls.Add(lc3); 
                                TextBox JDHFTxt = new TextBox(); 
                                JDHFTxt .ID = "txtJDHF"; 
                                JDHFTxt .Enabled = false;
                                JDHFTxt .Width = 100;
                                JDHFTxt .Text = ConvertToFunding(DrACE["SPRAllotment"].ToString()); 
                                Controls.Add(JDHFTxt); 
                                sb = new StringBuilder();

                                lc7 = new LiteralControl();
                                Controls.Add(lc7);
                                Button JDHFBtn = new Button();
                                JDHFBtn.ID = "btnJDHF";
                                JDHFBtn.CssClass = "plainButton";
                                JDHFBtn.Text = "Edit";
                                //JDHFBtn.OnClientClick = JDHFTxt.Enabled = true;
                                //JDHFBtn.Attributes.Add("OnClick, JDHFTxt.Enabled = true);
                                JDHFBtn.OnClientClick = Convert.ToString(JDHFTxt.Enabled = true);
                                Controls.Add(JDHFBtn);
                                sb = new StringBuilder();
sb.Append("</td>");
sb.Append("         </tr>");
Was it helpful?

Solution

That is because you are assigning the value true to the textbox not comparing. Change the code to Convert.ToString(JDHFTxt.Enabled);

EDIT After reading your comments this is what you want:

JDHFBtn.OnClientClick = 
      "document.getElementById('" + JDHFTxt.ClientID + "').disabled = false;" +
      "return false;";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top