Cannot apply indexing with [] to an expression of type 'System.Web.UI.HtmlControls.HtmlForm Web User Control not loading

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

  •  02-09-2022
  •  | 
  •  

Question

I've got a web user control that I'm posting to a form but the second user control "PageStateTemplate" is registering as a not known element, and on the actual code for the web control, the error found in the title comes up. This comes up for all lines that contain "ViewState".

<%@ Control Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="TemplateEngine.Docx" %>
<%@ Import Namespace="DocumentFormat.OpenXml.Packaging" %>
<%@ Import Namespace="DocumentFormat.OpenXml.Wordprocessing" %>
<%@ Import Namespace ="FileUpload" %>


<script runat="server"> 


   protected void Page_Load(object sender, EventArgs e)
   {
        if(!IsPostBack)
         {
            string str = "Welcome to Aspdotnet-Suresh Site";
         if(ViewState["SampleText"]==null)
            {
             ViewState["SampleText"] = str;
            }

         }
   }

   protected void btnClick_Click(object sender, EventArgs e)
   {
    lblString.Text = ViewState["SampleText"].ToString();
   }



</script>

<form id="ViewState" runat="server">
<div>

ViewState Data: <b><asp:Label ID="lblString" runat="server"/></b>
<asp:Button ID="btnClick" runat="server" Text="Get ViewState Data"
onclick="btnClick_Click"/>

</div>
</form>
Was it helpful?

Solution

In web forms should be only one tag form with attribute runat="server" and all controls inside this form, so inside your user control you does not need form with runat="server", usually it place in page, or masterpage

So for solve - remove form declaration from your user control

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top