Issues with User Control inside repeater - asp.net

I am using ASP.NET
I have created a user control that look like this:
when pressing the + : the score is raised by 0.5 and the opposite for minus.
The user control contains an update panel, and the pages itself containing the script manager.
When I put this user control in page and NOT inside repeater, this works perfect.
When I put this as part of a repeater. this not work at all.
I tried to delete all the update panels and still not working.
This is the error I'm getting
Don't know how to fix this.
This is the code of the user control:

Often, this error shows due to not post back:
If(!IsPostBack){
// your controls code here
}
Or, Check is your web page not using <form> tag more than once:
<form id="form1" runat="server">
<form id="form2" runat="server">
<!-- Your user controls -->
</form>
</form>
You have to use only one <form> tag in your page.
Or, try in your *.aspx page like:
EnableEventValidation="false"

Related

Change DNN Form into normal GET form

I'm working on making some changes to a Dot Net Nuke website with a customized skin. I found out that the header to the skins file was located in 'Default.aspx' here.
The form has some very strange behavior. I have had to disable the enter button because pressing within the form causes the webpage to go to "/HOME.aspx" however that action is never specified within the Default.aspx.
The code is as follows.
<dnn:Form id="Form" runat="server" ENCTYPE="multipart/form-data" >
<asp:Label ID="SkinError" runat="server" CssClass="NormalRed" Visible="False"></asp:Label>
<asp:PlaceHolder ID="SkinPlaceHolder" runat="server" />
<input id="ScrollTop" runat="server" name="ScrollTop" type="hidden" />
<input id="__dnnVariable" runat="server" name="__dnnVariable" type="hidden" />
</dnn:Form>
The form after being processed displays in the browser as.
<form name="Form" method="post" action="/HOME.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="Form" enctype="multipart/form-data">
What I want the code to display as is simply.
<form name="Form" method="get" action="/SearchResults.aspx" id="Form">
I tried removing the dnn code with the html directly but removing the dnn form causes the website to crash.
EDIT
What I'm trying to do can be seen at http://www.ontariosheep.org
Notice if you press the button the search works but pressing enter causes the page to refresh.
You can use some Javascript to do this:
jQuery('#SearchBox').keypress(function(e){
if(e.which == 13){
e.preventDefault();CallSearchPage('http://www.ontariosheep.org/SearchResults.aspx');
}
});
You would need to put that in script tags and also in a jQuery document ready area... like
<script>
jQuery(document).ready(function(){
//code above here
});
</script>
Changing the behavior of the form in DNN is not something you are going to do easily. DNN uses the ASP.NET Web Forms model, so the action for the page is always the current page.
If you want to customize this the only real way is to modify the form action via JavaScript on a specific page, but note that doing that prior to a button click or similar trigger WILL break all administration functions on the page that require a postback to the server.
What are you trying to accomplish?

userControl repeated with Form tag disappearing

I have a Contact userControl that have "save contact" as submit button and fields inside form tag we repeat this userControl with Code 20 times in one page
My problem is the Form Tag in the first userControl is hiding somehow --- i checked the userControl with developer Tool IE9 , firebug Firefox7 and the Form is not appearing in the first userControl and its appearing with the rest 19 Controls
I tried to View Source and take html copy in new html file in VS -- i found form is exist
i dont know if iam clear enough but please advice if you need more info
If you're running a form tag at the server, inside a user control, and then displaying this user control more than once on the page, then you will have multiple form tags running at server on one page, which is not allowed by ASP.NET
Take the form tag outside of the user control like:
<form runat="server">
<uc:Contact />
<uc:Contact />
<uc:Contact />
</form>

a page can have only one server-side form tag

I am using master page and when I run this page, it shows the following error message:
a page can have only one server-side form tag
How can I solve this problem?
I think you did like this:
<asp:Content ID="Content2" ContentPlaceHolderID="MasterContent" runat="server">
<form id="form1" runat="server">
</form>
</asp:Content>
The form tag isn't needed. because you already have the same tag in the master page.
So you just remove that and it should be working.
It sounds like you have a form tag in a Master Page and in the Page that is throwing the error.
You can have only one.
Does your page contain these
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:content>
tags, and are all your controls inside these? You should only have the Form
tags in the MasterPage.
Here are some of my
understanding and suggestion:
Html element can be put in the body of html pages and html page does
support multiple elements, however they can not be nested each
other, you can find the detailed description from the W3C html
specification:
The FORM element
http://www.w3.org/MarkUp/html3/forms.html
And as for ASP.NET web form page, it is based on a single server-side form
element which contains all the controls inside it, so generally we do not
recommend that we put multiple elements. However, this is still
supported in ASP.NET page(master page) and I think the problem in your
master page should be caused by the unsupported nested element, and
multiple in the same level should be ok. e.g:
In addition, if what you want to do through multiple forms is just make our
page posting to multiple pages, I think you can consider using the new
feature for cross-page posting in ASP.NET 2.0. This can help us use button
controls to postback to different pages without having multpile forms on
the page:
Cross-Page Posting in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/lib...39(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...40(VS.80).aspx
Use only one server side form tag.
Check your Master page for <form runat="server"> - there should be only one.
Why do you need more than one?
Sometime when you render the current page as shown in below code will generate the same error
StringWriter str_wrt = new StringWriter();
HtmlTextWriter html_wrt = new HtmlTextWriter(str_wrt);
Page.RenderControl(html_wrt);
String HTML = str_wrt.ToString();
so how can we sort it?
please remove " runat="server" " from "form" tag then it will definetly works.

Asp.Net Add UserControl to a Page

I have an ASP.Net web site and on one of the pages I'm using a repeater to render several iterations of a UserControl, that UserControl contains a second UserContol that has two text boxes that my User must enter information into. I want to be able to have my user push a button and add another instance of the second UserControl (with the two textboxes) to my original UserControl, so that the user has 4 textboxes displayed on the screen for the first UserControl. The problem I am seeing if I try to add the second UserControl to a given iteration of the first, is that the page postback causes any other of these second user controls to be deleted from the page.
Does anyone know of a way to do this using JQuery? I've had three posts that describe how to solve this problem using server side dynamic controls, and/or AJAX, but we've decided to focus on a JQuery solution because this server side mechanism is too costly in terms of resources for us.
I've been working on the suggestion by Zincorp below, and now have the JQuery working to clone a textbox, but having trouble using the server side Request.Form collection to iterate over the controls. Can anyone give adivce on how to iterate over the Request.Form collection?
OK, I think the problem with iterating over the controls using the Request.Form.AllKeys collection turned out to be that I was using an HTML Textbox, rather than an ASP TextBox Control. Apparently the Request.Forms.AllKeys collection only contains ASP controls, not HTML controls.
The problem I am seeing now is that when I clone the control in JQuery, and then submit my page with the submit button, the 2 controls have the same ID, and so are combined (I think) by http into one ASP TextBox Controls containing both values, with a comma delimiter (e.g.- 40,20). Anyone know how to get a new ID assigned to the cloned ASP TextBox?
Here is the updated markup in a sample ASP.Net web appliction:
<%# 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="ProjectDisplay" Text="Project" />
<asp:TextBox ID="ProjectValue" runat="server" ></asp:TextBox>
<div id="mydiv" ></div>
<br />
<br />
<br />
<input id="AddProject" type="button" value="Add Project" />
<br />
<br />
<asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
</div>
</form>
</body>
</html>
<script language="jquery" src="js/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" >
$(document).ready(function() {
$("#AddProject").click(function() {
var x = $("#ProjectValue").clone();
x.appendTo("#mydiv");
});
});
</script>
And here is the updated server side code where I'm trying to iterated over items in the Request.Form collection to get information from it:
public partial class _Default : System.Web.UI.Page
{
protected void Submit_Click(object sender, EventArgs e)
{
foreach (string s in Request.Form.Keys)
{
object x = Request.Form[s];
}
}
}
Before choosing a solution for the problem, consider the problem first:
You effectively need to:
1) Duplicate controls/markup on the client-side
2) Obtain these values on the server-side on a postback
3) For each of the added "user controls" on the client-side, add them as children of the first user control on the server side.
I'm not going to give the code, but here are some ideas:
1) Use jQuery's .clone() method (http://api.jquery.com/clone/) to duplicate the markup being rendered by the usercontrol containing the textboxes. Perhaps wrap them in a div without runat="server" to so that you can easily obtain it by ID. You'll probably need to then recursively iterate through the children in the cloned element and append a # to them to avoid any conflicting identifiers. (eg. nameTextBox_1, nameTextBox_2, etc.)
2) On a postback, use the Request.Form collection on the server side to obtain the textbox values. Iterate through it and snag all of the items whose keys start with "nameTextBox_".
3) For each added "user control" on the client side, create the actual user control on the server side, assign to it the values entered in the textboxes, and then add it to the child controls of the first one. This way the state is maintained upon returning to the user.
The short answer is that you would have to add the controls before the ViewState is initialized.
The video on this site has a nice guide on adding dynamic controls. http://www.asp.net/%28S%28wnmvzu45umnszm455c1qs522%29%29/learn/ajax-videos/video-286.aspx
This is probably not a ViewState issue. That may be the first place to look, but after that you need to make sure that your dynamic controls are actually being CREATED on each load.
Generally speaking, the ViewState is only responsible for restoring STATE to existing controls (hence the name). It is not responsible for recreating controls.
I believe you are experiencing a viewstate problem. Your dynamic controls are not being persisted. If you haven't already, read Dave Reed's excellent article Truly Understanding Viewstate. Pay particular attention to the section "5. Initializing dynamically created controls programmatically" which applies to the trouble you are experiencing.

How can you tell if form runat=server has been set?

When coding an Asp.Net page, you generally add a runat attribute in the aspx:
<form id="form1" runat="server">
Is it possible to tell in the code behind if the user hasn't done this i.e. they only did this:
<form id="form1">
Here the form has the id "form1" but in my case I don't know this. Code behind such as this is what I am looking for:
if(Page.HasForm)
{
}
You can only ever have one form tag with "runat=server" on it per .aspx page. All you have to do is to check to see if Page.Form is null or not. If it's null, then there's no form that has been marked to runat server.
if (Page.Form != null)
{
}
It's the runat="server" part that makes the .aspx page process an element and create a corresponding object on the server side. If a component is not running on the server, then it's not added to the page's control hierarchy.
var v = this.Form.TagName; //gets the name of the form that is maked as runat.
Of course if its not maked as runat then your code behind won't run anyway...
When you code in C# or Visual Basic in the code page, you will not have access to the object that do not have the runat=server option set.
You can easily access all the controls from a page using the me.controls page or something of the sort (I don't know the exact code but it's close to this) and check the type of the control to get the form.
Why do you need to know that? If a page does not have a runat=server form, it can't really be used as a server page.
You'd be able to access the form from the codebehind:
Response.Write(form1.Name);
Without the runat="server", you'd just get a compiler error.

Resources