I'm new to this world.
As you can see in the picture. This is my Default.aspx page where I have implemented a Web User Control(uc) called "adress.ascx".
My uc control is divided in 2 categories, first is Asp.net controls category and second is Html Controls category.
What is the scenario: I want to copy the value from Html Textbox to Asp:TextBox.
I have 2 options, either I can do with asp:Button or with html button(without runat="server")
Here is my code
<h1>ASP.NET Controls</h1>
<br />
<asp:Label runat="server" Text="Street"></asp:Label>
<asp:TextBox runat="server" ID="tbStreet" Text=""></asp:TextBox><br /><br />
<asp:Button ID="btnAsp" Text="Button ASP" runat="server"
OnClick="btnAsp_Click"
/>
<h2>-------------------------------</h2>
<h1>Html Controls</h1>
Street
<input type="text" name="Street" id="street"><br />
<br />
<button type="button" id="btnStreet">Button HTML!</button>
And here is my code "code behind" in c#
protected void btnAsp_Click(object sender, EventArgs e)
{
string value = Request.Form["street"];
}
When I run this code, the value I get is null
I hope you will find this helpful .
**Aspx Page**
<input type='button' id='btnAsp1' value='Shop Now' class='form-control'
value='Button:1' onclick='javascript:__doPostBack("btnAsp1");'
style='background-color:#3465aa; color:white' >
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
**Code Behind**
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] != null &&
Request.Form["__EVENTTARGET"] == "btnAsp1")
{
btnAsp1_Click(null, null);
}
}
private void btnAsp1_Click(object sender, System.EventArgs e)
{
Response.Write("You Clicked on " +
Request.Form["__EVENTARGUMENT"].ToString());
}
Here is the solution.
Code for Defualt.aspx page.
<%# Register Src="~/WebUserControl.ascx" TagName="Add" TagPrefix="uc1"
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<uc1:Add runat="server" ID="ucAdres" WidthFirstCollumn="175px" />
</body>
</html>
And here is the code for WebUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<form method="post" runat="server"
>
<h1>ASP.NET Controls</h1>
<br />
<asp:Label runat="server" Text="Street" ID="lable"></asp:Label>
<asp:TextBox runat="server" ID="tbStreet" Text="Text Asp.net text box"></asp:TextBox><br />
<br />
<asp:Button ID="btnAsp" runat="server" Text="Button Asp.net" OnClick="btnAsp_Click"/><br />
<input type="submit" id="btnHtml" value="Html Button" />
<h2>-------------------------------</h2>
<h1>Html Controls</h1>
Street
<input type="text" name="street" id="street" value="Text coming from HTML5"">
<br />
<br />
<button type="button" id="btnStreet">Button HTML!</button>
</form>
And here is the code of WebUserControl.ascx.cs
protected void btnAsp_Click(object sender, EventArgs e)
{
string value = Page.Request.Form["street"].ToString();
if (value != null)
{
tbStreet.Text = value;
} else
{
tbStreet.Text = "Nooooooooo";
}
}
I explain again, that you have a WebUserControl where you have 2 types of Controls(Asp.net and html). What you want to do is to copy from HTML to asp.net without runat="server".
I often has problem of Form so finally I found that I have 2 Forms, 1 on WebUserControl and 1 on Default.aspx.
Your screenshot shows a breakpoint on the following line:
string value = Request.Form["street"];
with value being null. However, from Using Breakpoints on MSDN (emphasis mine):
When you run this code in the debugger, execution stops whenever the breakpoint is hit, before the code on that line is executed
If you go to the next line, the line will run and populate value. To copy this into the ASP.net control, the following should be sufficient:
protected void btnAsp_Click(object sender, EventArgs e)
{
tbStreet.Text = Request.Form["street"];
}
I would caution that what you are trying to do probably has a better solution, so even if this solves your immediate problem you may find that other issues crop up further down the line.
Related
Here is the main form where I have to take user inputs. I have used Design option to create the form in visual studio.
WebForm1.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ArthmeticClient.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter Value 1:
<input id="Text1" type="text" /><br />
<br />
Enter Value 2:
<input id="Text2" type="text" /><br />
<br />
Resul:
<input id="Text3" type="text" /><br />
<br />
<input id="Button1" type="button" value="Addition" />
<input id="Button2" type="button" value="Multiplication" /><br />
<br />
</div>
</form>
</body>
</html>
I have used the same id for the input tag but still it cannot find the content.
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ArthmeticClient
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Sum(num1, num2));
}
protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Multiply(num1, num2));
}
}
}
I have to just a small option, I have created simple form, I dont think there a problem with the id I have given. but it is still showing this error. Idk what is wrong.
Can anybody help...
I hava a MasterPage with 2 UserControls. When something happens in UserControl1.ascx, it has to update a TextBox in UserControl2.ascx.
I tried this inside UserControl1.ascx, but no success:
UserControl userControl = (UserControl)LoadControl("UserControl2.ascx");
var txt = (TextBox) userControl.FindControl("txtTest");
txt.Text = "Hello world";
Thanks for all help :)
The LoadControl is load a new control that is not exist on page...
Lets see this example.
You have a master page that has 2 User Controls.
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<br /><br />
<uc2:WebUserControl ID="WebUserControl2" runat="server" />
<br /><br />
<asp:Button runat="server" ID="btnOk" Text="ok" OnClick="btnOk_Click" />
</div>
</form>
and on code behind this
protected void btnOk_Click(object sender, EventArgs e)
{
WebUserControl1.TextOnMe = WebUserControl2.TextOnMe;
}
Now the user controls have this on html
<asp:TextBox runat="server" ID="txtText"></asp:TextBox>
and on code behind you get and set the Text like that on code behind
public string TextOnMe
{
get
{
return txtText.Text;
}
set
{
txtText.Text = value;
}
}
One Similar Answer
ASP.NET MasterPage only control?
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;
})
});
i am trying to learn classes and objects in C#, i want to get the textbox value and show it in a label using classes and get set property.
i try the below process, but i will not showing/output anything.
index.aspx code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="classes.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="fname" runat="server" /><br />
<asp:TextBox ID="lname" runat="server" /><br />
<asp:TextBox ID="password" runat="server" /><br />
<asp:Button Text="Submit" ID="submit" runat="server" OnClick="submit_Click" />
<br />
<br />
<br />
<br />
<asp:Label ID="FirstName" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Button click code
protected void submit_Click(object sender, EventArgs e)
{
basicinfo bn = new basicinfo();
FirstName.Text = bn.fname;
}
class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace classes
{
public class basicinfo
{
public string fname;
public string last_name;
public string password;
public string Name
{
get{return fname;}
set{fname=value;}
}
}
}
Can someone tell me is this the wrong way? Also please provide reference of any helping material/Links/video tutorials course through which i can clear my basic classes, get set, objects, methods idea, i am heaving trouble to understand it.
Update
if this how it works
protected void submit_Click(object sender, EventArgs e)
{
basicinfo bn = new basicinfo();
bn.Name = fname.Text;
FirstName.Text =bn.Name;
}
then why should we use classes and get, set properties?
we can simply do like
protected void submit_Click(object sender, EventArgs e)
{
FirstName.Text = fname.Text;
}
You are trying to save the object value in the Label but Your object is empty it does not contain textbox value. Your code should be
protected void submit_Click(object sender, EventArgs e)
{
basicinfo bn = new basicinfo();
bn.fname= fname.Text; //textbox value to object
FirstName.Text = bn.fname; //object value to label
}
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.