Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 2
Reputation: chakkilam is an unknown quantity at this point 
Solved Threads: 0
chakkilam chakkilam is offline Offline
Newbie Poster

Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

 
0
  #1
Feb 21st, 2009
Hi All
I am getting below error. basically I want to loop through all controls in a page and make sure they have not changed the values. if they have i want to prompt a message asking to save.
while looping the controls I am not able to send it as a string its erroring out. can some one please suggest how to convert String to a webcontrol
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'.

Source Error:



Line 19: For Each ctrl As Control In Page.Controls
Line 20: 'w = CType(ctrl, WebControl)
Line 21: MonitorChanges(ctrl.ID)
Line 22: Next
Line 23: End Sub



  1. Public Sub MonitorChanges(ByVal wc As WebControl)
  2. If wc Is Nothing Then Exit Sub
  3. If TypeOf wc Is CheckBoxList OrElse TypeOf wc Is RadioButtonList Then
  4. 'Add an array element for each item in the checkbox/radiobutton list
  5. For i As Integer = 0 To CType(wc, ListControl).Items.Count - 1
  6. Page.RegisterArrayDeclaration("monitorChangesIDs", """" & String.Concat(wc.ClientID, "_", i) & """")
  7. Page.RegisterArrayDeclaration("monitorChangesValues", "null")
  8. Next
  9. Else
  10. Page.RegisterArrayDeclaration("monitorChangesIDs", """" & wc.ClientID & """")
  11. Page.RegisterArrayDeclaration("monitorChangesValues", "null")
  12. End If
  13.  
  14. 'AssignMonitorChangeValuesOnPageLoad()
  15. End Sub
Last edited by peter_budo; Feb 21st, 2009 at 4:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

 
0
  #2
Feb 22nd, 2009
you dont need to loop through the controls to see the changes. Create a class with the default form values, then compare the form values to the class values, if there is a change save to database.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: chakkilam is an unknown quantity at this point 
Solved Threads: 0
chakkilam chakkilam is offline Offline
Newbie Poster

Re: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

 
0
  #3
Feb 23rd, 2009
Serkan,
Very kind of you for the reply. I am very new to asp.net technology and accomplishing things by learning ,copying what i got from google and other forums.
Can you be kind enough to send me the code if its not too lengthy or huge deal.
Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

 
0
  #4
Feb 23rd, 2009
To solve your compilation error issue rewrite line 21 like this:

MonitorChanges(ctrl)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.WebControl'

 
0
  #5
Feb 23rd, 2009
here is the example :

Default.aspx page :
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7. <title></title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12.  
  13. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  14. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  15. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  16.  
  17. </div>
  18. </form>
  19. </body>
  20. </html>

Default.aspx.cs :

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. private class formValues
  11. {
  12. public string textbox1 = "";
  13. public string texbox2 = "";
  14. public bool anyChanges = false;
  15. }
  16. formValues frmValues = new formValues();
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19.  
  20. }
  21. protected void Button1_Click(object sender, EventArgs e)
  22. {
  23. if (TextBox1.Text != frmValues.textbox1)
  24. frmValues.anyChanges = true;
  25. if (TextBox2.Text != frmValues.texbox2)
  26. frmValues.anyChanges = true;
  27. Response.Write(frmValues.anyChanges.ToString());
  28. }
  29. }

you set your initial values in the class, then you compare your class values to the form values, run this example on your local pc and you will understand, you can convert the code to its VB equivalent using one of the free converters searcing google.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC