I am trying to refer to object from .cshtml file in .cs file but it doesn't work. It says the name doesn't exist.
cshtml.cs file
public class ParticipateModel : PageModel
{
public string Message { get; set; }
public void OnGet()
{
Message = "Your application description page.";
}
protected void sendDataToKonkurs(object sender, EventArgs e)
{
string name = txtName.Text;
}
cshtml file
<input type="text" class="form-control" id="txtName" maxlength="20" formmethod="post" style="margin-bottom:20px">
this example might help you front end code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="DefaultControls.aspx.cs"
Inherits="ControlsMix.DefaultControls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="CbClientName" runat="server" Width="300px">
</asp:DropDownList>
</div>
</form>
backend c# code which loads the values into combo box
protected void Page_Load(object sender, EventArgs e)
{
CbClientName.Items.Add("John");
CbClientName.Items.Add("Smith");
CbClientName.Items.Add("Dylan");
CbClientName.Items.Add("Ben");
}
just ensure your id="whatevername" is the same in the backend of the code this binds the control in asp.net
Related
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
I want to use FileUpload Control to save Image to Database. The HasFiles return always false after I click on BtnSave. I tried with and without UpdatePanel but could not find the solution.
My code is:
protected void BtnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
var ChNumber = txtNumber.Text;
string img = this.FileUpload1.PostedFile.FileName;
//Save image to database function
}
else
{
lblStatus.Text="Image not Uploaded";
}
}
Try with a simple form without UpdatePanel. Here's the markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="BtnSave" runat="server" Text="Button" OnClick="BtnSave_Click" />
<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
And here's the code:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
var ChNumber = txtNumber.Text;
string img = this.FileUpload1.PostedFile.FileName;
//Save image to database function
}
else
{
lblStatus.Text = "Image not Uploaded";
}
}
}
I set a brekapoint at if (FileUpload1.HasFile == true) and see the uploaded file is there.
I would like to 'transfer' a textbox value into a html code. E.g the textbox value is a website like http://www.google.com in form1.aspx. and i would like the value to be put into the html code in form2.aspx . Might i ask how i should be going around on this?
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
Like i would like the textbox1 value to replace the current www.google.com
Here's how you can do this:
Webform1 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SOTest1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Your text: ">
</asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Webform1 Code:
using System;
namespace SOTest1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string myString = Server.HtmlEncode(TextBox1.Text);
Response.Redirect("Webform2.aspx?mystring=" + myString);
}
}
}
Webform2 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SOTest1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
</div>
</form>
</body>
</html>
Webform2 Code:
using System;
namespace SOTest1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mystring = Request.QueryString["mystring"];
myAnchor.HRef = mystring;
myAnchor.InnerText = mystring;
}
}
}
I search on internet but did not find any best solution, i found one way, where you need to code html tags like
<input type='text'> ... etc etc
and retrieve that value on another page as
Request.Form["name of input text field"];
W3Schools -ASP Forms and User Input
exists any better way to retrieve that post value??
ASP.NET WebForms has support for cross-page posting. Read http://msdn.microsoft.com/en-us/library/ms178139.aspx and http://msdn.microsoft.com/en-us/library/ms178140.aspx for more information.
Example:
WebForm1.aspx:
Note the asp:button has its PostBackUrl property set.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormApplication.WebForm1" EnableViewStateMac="false" %>
<!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:Button ID="Button1" runat="server" Text="Button" PostBackUrl="WebForm2.aspx" />
</div>
</form>
</body>
</html>
WebForm2.aspx:
Note that I've added the PreviousPageType directive here.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebFormApplication.WebForm2" %>
<%# PreviousPageType VirtualPath="WebForm1.aspx" %>
<!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>
This is page 2
<asp:Label Text="n/a" runat="server" ID="Label1" ></asp:Label>
</div>
</form>
</body>
</html>
WebForm2.aspx.cs: (Codebehind)
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(PreviousPage.IsCrossPagePostBack)
{
var tb = PreviousPage.FindControl("TextBox1") as TextBox;
Label1.Text = tb.Text;
}
}
}
The example you provide is useful for asp pages but I recommend to use QueryString collection for passing parameters between webforms in asp.net
Here is an example of how to use it :
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string v = Request.QueryString["Param1"];
if (v != null)
{
Response.Write("param is ");
Response.Write(v);
}
}
}
You could use Response.Redirect for concatenate the URL :
string _url = string.Format("../WebFormA.aspx?Param1={0}&Param2={1}", _param1, _param2);
Response.Redirect(_url);
Regards.
i"m using asp.net FileUpload , after user input file he click on save button
in the c# i have this function
protected void btnUploadImages_Click(object sender, EventArgs e)
{
SaveImages(FileUpload1, "", returnAlbumId, out returnPhotoId);
}
this function save the image from the FileUpload1 so far all work as it"s should
but after the postback when i push the refresh button on the page i"m go to this function again , SaveImages function save the same image again .
the FileUpload1 didn't clear after the postback
thanks
Even i got the Same Problem I have resolved like Below.
After uploading the File If you Redirect to same page or some other page in your project. After Redirection Response will not be there once you redirected.
In My ASPX
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
In My Code Behind
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
path = path + FileUpload1.FileName;
FileUpload1.SaveAs(path);
Response.Redirect("WebForm.aspx"); // Responce will be cleared. This Redirection will do the Trick
//Put the debugger and check it will work
}
}
Here, to show the success and error messages try to use sessions.