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

Public Sub MonitorChanges(ByVal wc As WebControl)
        If wc Is Nothing Then Exit Sub
        If TypeOf wc Is CheckBoxList OrElse TypeOf wc Is RadioButtonList Then
            'Add an array element for each item in the checkbox/radiobutton list
            For i As Integer = 0 To CType(wc, ListControl).Items.Count - 1
                Page.RegisterArrayDeclaration("monitorChangesIDs", """" & String.Concat(wc.ClientID, "_", i) & """")
                Page.RegisterArrayDeclaration("monitorChangesValues", "null")
            Next
        Else
            Page.RegisterArrayDeclaration("monitorChangesIDs", """" & wc.ClientID & """")
            Page.RegisterArrayDeclaration("monitorChangesValues", "null")
        End If

        'AssignMonitorChangeValuesOnPageLoad()
    End Sub

Recommended Answers

All 4 Replies

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.

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

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

MonitorChanges(ctrl)

here is the example :

Default.aspx page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private  class formValues
    {
       public   string textbox1 = "";
        public  string texbox2 = "";
        public  bool anyChanges = false;
    }
    formValues frmValues = new formValues();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != frmValues.textbox1)
            frmValues.anyChanges = true;
        if (TextBox2.Text != frmValues.texbox2)
            frmValues.anyChanges = true;
        Response.Write(frmValues.anyChanges.ToString());
    }
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.