C# - Data from ___.aspx to ___.cshtml with a form - c#

I'm new to C# - ASP.NET and I'm trying to drop data ,from a filled form, from .aspx to .cshtml.
I made a form with three TextBoxes and a Button, a method for the "onClick". This method set the value of ViewData (name,date,user) with the values of the three TextBoxes. And I want to collect these value on the page targeted by the form.
I do not get any error but ViewData(name,date,user) seems to be empty.
Here's my code :
CSHTML
#{
ViewBag.Title = "Consulter";
}
#Html.Partial("MenuBar")
#Html.Partial("Connexion")
#ViewData["name"]
<style type="text/css">
...
</style>
...
<h2>...
ASPX
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<script language="C#" runat="server">
public void Ajouter(Object sender, EventArgs e)
{
ViewData["name"] = dateForm.Text;
ViewData["date"] = dateForm.Text;
ViewData["user"] = userForm.Text;
}
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<meta name="viewport" content="width=device-width" />
<title>AjouterForm</title>
</head>
<body>
<h1>Ajouter un produit</h1>
<div>
<form id="form1" runat="server" action="Consulter" defaultbutton="bouton1" method="get">
<div>
<asp:TextBox placeholder="Name" runat="server" ID="nameForm"/></br>
<asp:TextBox placeholder="Date" runat="server" ID="dateForm"/></br>
<asp:TextBox placeholder="User" runat="server" ID="userForm"/></br>
<asp:Button runat="server" text="Envoyer" ID="bouton1" onClick="Ajouter"/>
<asp:Label runat="server" ID="label1" />
</div>
</form>
</div>
</body>
</html>
If you know how to solve it or if you have another way to do it, it would be amazing :)
Thank you in advance.

Related

Can't save innerText / innerHTML of an editable div in asp.net c#

I try to save the changed text of an editable div. I put some text on page load into the div and change it in the browser, but I can't save the changes because the innerHTML content doesn't change.
This is my aspx file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Title</title>
</head>
<body>
<form id="form1" runat="server">
<div contenteditable="true" runat="server" id="txtText" ></div>
<asp:Button ID="btnSave" runat="server" Text="Speichern" OnClick="btnSave_Click" />
</form>
</body>
</html>
This is the code behind file:
public partial class editWordModule : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtText.InnerHTML = "This is the unmodified text";
}
protected void btnSave_Click(object sender, EventArgs e)
{
string test = txtText.InnerHtml;
//still shows "This is the unmodified text" while it was changed before in the div
}
}
}
So how do I save the edited innerHTML of the div?
Make a little trick with JavaScript.
When the button is clicked, get the innerHTML of DIV and put into a asp:HiddenField
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
function ChangeDiv() {
var div = document.getElementById("txtText");
var hdn = document.getElementById("hdnText");
hdn.value = div.innerHTML;
}
</script>
<form id="form1" runat="server">
<div contenteditable="true" id="txtText"></div>
<asp:HiddenField ID="hdnText" runat="server" ClientIDMode="Static" />
<asp:Button ID="btnSave" runat="server" Text="Speichern" OnClick="btnSave_Click" OnClientClick="ChangeDiv()" />
</form>
</body>
</html>
And in your code behind read the hidden field.
string hdn = hdnText.Value

Execute method before form submitted

I have the following code to submit a form with a hidden value.
The problem is - I can't set the value of MyNewValue before the form is submitted to the remote address https://some-remote-site/Form.aspx.
Does anybody know how I can set the hidden value when the user clicks the button and then submit the form to the same address?
MyFile.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 370px">
<form id="myForm" method="post" runat="server" action="https://some-remote-site/Form.aspx">
<fieldset>
<asp:HiddenField id="MyNewValue" Value="X" runat="server" />
<p>
<label for="Username">Username:</label>
<asp:TextBox id="Username" Text="Username" runat="server" />
</p>
<asp:Button ID="SubmitForm" runat="server" onclick="SubmitForm_Click" Text="Button" />
</fieldset>
</form>
</body>
MyFile.aspx.cs
// Not executed because action="https://some-remote-site/Form.aspx" is set
protected void SubmitForm_Click(object sender, EventArgs e)
{
MyNewValue.Value = DateTime.Now.ToString();
}
You can do it client-side using jQuery. So when the user clicks submit, and before submitting the form, you can update your hidden input and then return true to proceed the submission of your form.
This is it could be done:
$(document).ready(function(){
$('#myForm').on('submit', function(){
$('#MyNewValue').val('YOUR_VALUE');
return true;
})
});

Only Content Controls are Allowed (C# webcontrols)

So, I'm not sure what's going on. My boss wasn't pleased with me using MVC and Razor, so I'm being forced to code with this nasty webcontrol/codebehind style. =/
The error is:
Only Content controls are allowed directly in a content page that contains Content controls.
Here is the masterpage:
<%# Master Language="C#"%>
<!DOCTYPE html>
<html>
<head>
<title>Application Form</title>
</head>
<body>
<div id="container">
<asp:contentplaceholder id="contentPlaceHolder" runat="server" />
</div>
</body>
</html>
And here is the Default.aspx page that's throwing the error.
<%# Page Language="C#" Inherits="dumb.Default" MasterPageFile="MasterPage.master" %>
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<p>Please complete this application.</p>
<form action="Default.aspx" method="post" runat="server">
<div>
<span class="spaced">Name:</span><asp:TextBox id="name" runat="server" />
</div>
</form>
<p>
<asp:Label id="finalmessage" runat="server" />
</p>
</asp:Content>
And the silly Default.aspx.cs codebehind...
using System;
using System.Web;
using System.Web.UI;
namespace dumb
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack) {
finalmessage.Text = "Submission Processed Successfully!";
}
}
}
}
All of your content, including static HTML must be inside Content tags in the content page. You have a <h2> outside:
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
Should be:
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<h2>Application Form</h2>

passing value from ASP.NET to Java Scripts to HTML

I need value to be passing from ASP.NET page to JavaScript and then to HTML textfield.
My following code is able to read values from ASP.NET to JavaScript but unable to pass value form JavaScript to HTML TextField. Can anybody please correct my code.
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function can() {
var myVariable = '<%=ServerSideVariable %>';
document.write(myVariable);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" value='can()'/>
</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
{
public string ServerSideVariable { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
string pval = "Passed value;
ServerSideVariable = pval;
}
}
document.getElementById('Text1').value = myVariable;
But you need to move the assignment script block bellow the declaration of element Text1, or do it on document.ready
Anyway, you also need to error check.
It's best to use JQuery anyway, and get familiar with it.
As others have said with javascript OR like this:
(since there is nothing in your example that requires javascript -- KISS-- don't use javascript if you don't have to do so.)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" ><%=ServerSideVariable %></input>
</div>
</form>
</body>
</html>
Do this instead in a document.load.
document.getElementByID('Text1').value = myVariable;

How to make a “asp:Button” work inside a “mmenu nav “

I’m trying to integrate “jquery.mmenu” in a Visual Studio project.
It seems that “asp:Button” does not work inside a “mmenu nav “.
Nor a "asp:LinkButton" seems to fully perform.
Take the specific case of a login:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="test._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-2.1.4.min.js"></script>
<script src="Scripts/jquery-ui-1.11.4.min.js"></script>
<script src="Scripts/jquery.mmenu.min.all.js"></script>
<link href="Content/themes/base/all.css" rel="stylesheet" />
<link href="Content/themes/base/jquery.mmenu.all.css" rel="stylesheet" />
<title></title>
<script>
$(document).ready(function () {
$("#login").mmenu({
});
});
</script>
</head>
<body>
<div>
<form id="form1" runat="server">
Open login pannel<br />
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="Redirect" LoginText="login" LogoutText="logout" LogoutPageUrl="~/" /><br />
<nav id="login">
<div>
<asp:Label ID="UserNameLabel" runat="server">User:</asp:Label><br />
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox><br />
<asp:Label ID="PasswordLabel" runat="server">Password:</asp:Label><br />
<asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="LoginButton" runat="server" ClientIDMode="Static" Text="Login Button" OnClick="LoginButton_Click" /><br />
<asp:LinkButton ID="LoginLinkButton" runat="server" ClientIDMode="Static" OnClick="LoginLinkButton_Click">Login LinkButton</asp:LinkButton>
</div>
</nav>
</form>
</div>
</body>
</html>
The code behind looks like:
using System;
using System.Web.Security;
namespace test
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.RedirectFromLoginPage(UserNameTextBox.Text, false);
}
}
protected void LoginLinkButton_Click(object sender, EventArgs e)
{
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.RedirectFromLoginPage(UserNameTextBox.Text, false);
}
}
}
}
The asp:Button does not post back.
The asp:LinkButton does not login.
Is there a way to have it working?
Thanks.
Web forms most of the time change the name of the element and jquery needs the exact name to find your button. Add this property to your button and test again
ClientIDMode="Static"
The mmenu plugin grabs the NAV#login from the HTML and puts it back directly inside the BODY. Meaning, the input and button are no longer inside the FORM (and thus won't submit the form).
Basically, if you put the FORM inside the NAV#login all should work as expected.

Categories

Resources