943,633 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 6311
  • ASP.NET RSS
Feb 21st, 2009
0

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

Expand Post »
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



ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chakkilam is offline Offline
2 posts
since Feb 2009
Feb 22nd, 2009
0

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

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.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Feb 23rd, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chakkilam is offline Offline
2 posts
since Feb 2009
Feb 23rd, 2009
0

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

To solve your compilation error issue rewrite line 21 like this:

MonitorChanges(ctrl)
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Feb 23rd, 2009
0

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

here is the example :

Default.aspx page :
ASP.NET Syntax (Toggle Plain Text)
  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 :

ASP.NET Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: forms authentication in asp.net
Next Thread in ASP.NET Forum Timeline: Extend Properties of Silverlight Controls...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC