I'm sorry in advance if this seems to be a beginners question and wasted some of your time, but it does really bother me and I would really appreciate it if someone could help.
I'm simply creating a "zone" if you will, where the user inputs a word/sentence which are tasks, and then can check if the task was completed or not and/or if the word/sentence needs to be changed.
So in the end, what I need is a textbox lets say where the user inputs a specific number supposed x, and generates x times this div.
Here is the code (unfortunately I could not post pics since I do not have enough reputation..)
ASP.NET code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GMode.tiz.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>whatevz</title>
<link href="StyleSheet1.css" rel="stylesheet" />
<script src="../jquery-1.11.3.min.js"></script>
<script src="../functions.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"></asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"></asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GMode.tiz
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCheck_Click(object sender, EventArgs e)
{
lbl.CssClass = "done";
}
protected void btnCancel_Click(object sender, EventArgs e)
{
lbl.CssClass = "undone";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (hi.Text == "")
{
lbl.Text = "No Value";
}
else
{
lbl.Text = hi.Text;
}
}
}
}
So basically I want to dynamically create <div class="singleContainer"> with everything inside it x times (x defined by the user).
I'm going about this more on the fact like it's C++. When you can dynamically create an array with a specific number x.
Please tell me if you need more of the code. There are still the css and js files that I did not post.
Thank you,
Jack
I think that for this the best option is to use AngularJS, you can do something like this:
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>
<script>
var app = angular.module('app', [])
app.controller('PostsCtrl', function ($scope) {
$scope.change = function (){
$scope.listDivs = [];
for(var i=0;i<$scope.createDiv;i++) {
$scope.listDivs.push(i);
}
}
})
</script>
</head>
<body ng-app='app'>
<div ng-controller='PostsCtrl' class='container'>
<input ng-model='createDiv' ng-change="change()"/>
<ul class='list-group'>
<li ng-repeat="post in listDivs" class='list-group-item'>
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"> </asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"> </asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</li>
</ul>
</div>
</body>
</html>
Put your div into a usercontrol (ascx file).
In your main page add a placeholder:
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
in code behind you can add your control as often as you want:
yourControl= LoadControl("~/Controls/yourControl.ascx");
PlaceHolder1.Controls.Add(yourControl);
Related
while clicking on button it give me an error "System.NullReferenceException: Object reference not set to an instance of an object"
aspx Code
<body>
<form id="form1" runat="server">
<div id="choto">
</div>
<asp:Button ID="btn" runat="server" onclick="btn_Click" Text="Submit" />
</form>
</body>
JS code
<script type="text/javascript">
document.getElementById("choto").innerHTML = "<input name=txt1 type=\"text\" id=\"txt1\" ru" + "nat=\"server\" />";
</script>
C# code
protected void btn_Click(object sender, EventArgs e)
{
// HtmlInputText txt = new HtmlInputText();
HtmlInputText txt = (HtmlInputText)FindControl("txt1");
txt.Value = "Shakeel";
}
Simply put, the input with the id "txt1" is not an ASP.NET control, therefore ASP.NET cannot find it. ASP.NET controls must be defined on the server, or the framework won't know about them.
<body>
<form id="form1" runat="server">
<div id="choto">
<asp:TextBox ID="txt1" runat="server" style="display:none;" />
</div>
<asp:Button ID="btn" runat="server" onclick="btn_Click" Text="Submit" />
</form>
</body>
ASP.NET controls cannot be added via JavaScript, however, they can be made to be invisible/visible using JavaScript.
<script type="text/javascript">
document.getElementById('<%# txt1.ClientID %>').style.display = "block";
</script>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Text box. On button click event the value of Text box should be displayed in a label as result.
On clicking the button again and giving some input in the Text box the input should get added to the result.
I mean repeating the button clicks and providing input should get added o the result.
How to perform this?
I am still getting error
"System.FormatException: Input string was not in a correct format."
Error is regarding: lblconsumed.Text = (int.Parse(lblconsumed.Text) + userValue).ToString();
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.ServRef;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
ServRef.ProteinServiceClient sc = new ServRef.ProteinServiceClient();
UserDetail userinfo = new UserDetail();
protected void Page_Load(object sender, EventArgs e)
{
// ddlUser.DataSource = sc.AddUser(userinfo);
BindUserDetails();
}
protected void BindUserDetails()
{
IList<UserDetail> objUserDetails = new List<UserDetail>();
//objUserDetails = objService.GetUserDetails("");
objUserDetails = sc.GetAllUser("");
ddlUser.DataSource = objUserDetails;
ddlUser.DataBind();
}
protected void btnAddUser_Click(object sender, EventArgs e)
{
userinfo.user_name = tbname.Text.Trim();
userinfo.user_goal = Convert.ToInt32(tbgoal.Text.Trim());
string result = sc.AddUser(userinfo);
lblgoal.Text = Convert.ToString(userinfo.user_goal);
}
protected void Button2_Click(object sender, EventArgs e)
{
int userValue;
if (int.TryParse(tbamount.Text, out userValue))
{
lblconsumed.Text = (int.Parse(lblconsumed.Text) + userValue).ToString();
}
}
}
}`
My aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Style.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div id="main">
<h2>Protein Tracker</h2>
<div id="selectuser">
<label for="select-users">Select a user : </label>
<asp:DropDownList ID="ddlUser" runat="server"
Width="60px" AutoPostBack="True"
DataTextField="user_name"
DataValueField="user_name" ></asp:DropDownList>
</div>
<hr />
<div id="adduser">
<h2>Add new user</h2>
<label for="name">User Name :</label>
<asp:TextBox ID="tbname" runat="server" style="margin-left: 10px"></asp:TextBox>
<br /><br />
<label for="goal">User Goal :</label>
<asp:TextBox ID="tbgoal" runat="server" style="margin-left: 20px"></asp:TextBox>
<br /><br />
<asp:Button ID="btnAddUser" runat="server" Text="Add" style="margin-left: 210px" OnClick="btnAddUser_Click" />
</div>
<hr />
<div id="addprotein">
<h2>Add protein</h2>
<label for="amount">Amount :</label>
<asp:TextBox ID="tbamount" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Add" style="margin-left: 180px" OnClick="Button2_Click"/>
</div>
<hr />
<div>
<p>Total Consumed :  <asp:Label ID="lblconsumed" runat="server" Text=""></asp:Label></p>
<p>Goal Set :  <asp:Label ID="lblgoal" runat="server" Text=""></asp:Label></p>
</div>
</div>
</form>
</body>
</html>
#user3174595 : I have put in the simple code. its the easiest operation that can be performed.
form1.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text="0" ></asp:Label>
</div>
</form>
form1.aspx.cs
public partial class addition : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
double userValue = 0;
if (double.TryParse(TextBox1.Text,out userValue))
{
Label1.Text = (double.Parse(Label1.Text) + userValue).ToString();
}
}
}
I've absolutely exhausted Google and I can't find a solution to this problem. When click a button on my .aspx page, the corresponding function is not called from .aspx.cs file. I'll just post my code and see if there are any takers.
AddUser.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="AddUser.aspx.cs" Inherits="AddUser" %>
<asp:Content ID="tb_AddUserHeader" ContentPlaceHolderID="tb_HeaderPlaceHolder" Runat="Server">
</asp:Content>
<asp:Content ID="tb_AddUserContent" ContentPlaceHolderID="tb_ContentPlaceHolder" Runat="Server">
<h1>Add User</h1>
<fieldset>
<asp:Label ID="tb_lblAddUser_Authorized" runat="server" Visible="false">
<br />
<table>
<tr>
<td><u>Username:</u></td>
<td><asp:TextBox ID="tb_txtbxUsername" runat="server" Width="200"></asp:TextBox></td>
</tr>
<tr>
<td><u>Password:</u></td>
<td><asp:TextBox ID="tb_txtbxPassword" runat="server" Width="200" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td><u>Account Type:</u></td>
<td><asp:DropDownList ID="tb_ddAccountType" runat="server">
<asp:ListItem Value="v" Text="Viewer"></asp:ListItem>
<asp:ListItem Value="t" Text="Tester"></asp:ListItem>
<asp:ListItem Value="a" Text="Admin"></asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td><asp:Button ID="tb_btnAddUserSubmit" runat="server" Text="Submit" OnClick="tb_btnAddUserSubmit_Click" UseSubmitBehavior="true" /></td>
</tr>
</table>
<br />
</asp:Label>
<asp:Label ID="tb_lblAddUser_Output" runat="server" Visible="false"></asp:Label>
<asp:Label ID="tb_lblAddUser_Unauthorized" runat="server" Visible="true">
<br />Only administrators are authorized to view this page
<br />
<br />
</asp:Label>
</fieldset>
</asp:Content>
and my corresponding codebehind file, AddUser.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using tbBusinessObjects;
using tbWebClientControllers;
public partial class AddUser : System.Web.UI.Page
{
tbWebUserController m_userController;
protected void Page_Load(object sender, EventArgs e)
{
m_userController = new tbWebUserController();
//if (this.IsPostBack)
//{
// String username = tb_txtbxUsername.Text;
// //tb_btnAddUser_Click(sender, e);
//}
//else
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
if (ticket.UserData == "a")
{
tb_lblAddUser_Authorized.Visible = true;
tb_lblAddUser_Unauthorized.Visible = false;
}
}
}
}
protected void tb_btnAddUserSubmit_Click(object sender, EventArgs e)
{
tbUser user = new tbUser();
user.m_username = tb_txtbxUsername.Text;
user.m_password = tb_txtbxPassword.Text;
user.m_type = tb_ddAccountType.SelectedValue.ToCharArray()[0];
if (m_userController.InsertUser(user))
{
tb_lblAddUser_Output.Text = "<br />User was successfully added<br /><br />";
}
else
{
tb_lblAddUser_Output.Text = "<br />There was an error adding user<br /><br />";
}
tb_lblAddUser_Output.Visible = true;
tb_lblAddUser_Authorized.Visible = false;
}
}
I've set multiple breakpoints in my Click function and they're never hit. I tried simply catching my Page_Load function with a Page.IsPostBack, but it none of the data is saved from the textboxes or dropdown.
I've also tried changing the UseSubmitBehavior tag between true and false and removing it completely and it still doesn't work. I've copy/pasted as much code from other pages that use button events that are working and it still won't work. I have absolutely no idea what is going on right now. >_<
EDIT: And just in case it would help in any way, here is my Site.master...
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head id="tb_head" runat="server">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>KersTech Hydraulic/Electric Hybrid Data Recording and Telemetry</title>
<link href="tb_style.css" rel="stylesheet" type="text/css" media="screen" />
<asp:ContentPlaceHolder id="tb_HeaderPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="tb_formBody" runat="server">
<!-- Begin wrapper -->
<div id="tb_wrapper">
<asp:Label ID="tb_lblFormsAuthenticationUserData" runat="server" Text="Nothing" Visible="false"></asp:Label>
<!-- Begin top -->
<div id="tb_top">
<ul id="tb_nav">
<asp:Label ID="tb_lblMasterMenu" runat="server"></asp:Label>
<li><asp:LoginStatus ID="tb_LoginStatus" runat="server" LogoutAction="Redirect" LogoutPageUrl="Logout.aspx" /></li>
</ul>
<div id="tb_Greeting">
<asp:LoginView ID="tb_MasterLoginView" runat="server">
<LoggedInTemplate>
Logged in as <asp:LoginName ID="MasterLoginName" runat="server" />
</LoggedInTemplate>
</asp:LoginView>
</div>
<asp:Label ID="test" runat="server"></asp:Label>
</div>
<!-- Begin content -->
<div id="tb_content">
<asp:ContentPlaceHolder id="tb_ContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
<!-- End content -->
<!-- Begin footer -->
<div id="tb_footer"><div id="something">
<!-- Begin badges -->
<div>
<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /> <!-- HTML validation badge -->
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" /> <!-- CSS validation badge -->
</a>
</div></div>
<!-- End badges -->
</div>
<!-- End footer -->
</div>
<!-- End wrapper -->
</form>
</body>
</html>
EDIT2: I moved my Button outside of my label and manually set the button's visibility along with the label's visibility (that was the only reason I was using those labels in the first place), and it worked just fine. I just spent the last hour and a half trying to figure this out, so in case anyone else ever has the same issue again:
ASP:BUTTONs WILL NOT FIRE FROM INSIDE OF ASP:LABELs!!!!!
try butting this line in the page load
tb_btnAddUserSubmit.Click += new EventHandler(tb_btnAddUserSubmit_Click);
I think the problem may be that the click handler is not register in the click event
Have you copied this method from other page/application ? if yes then it will not work, So you need to delete the event and event name assigned to the button then go to design and go to button event properties go to onClick event double click next to it, it will generate event and it automatically assigns event name to the button. this should work
Try AutoEventWireup="false" in your page headers. I haven't worked in ASP.net in like 5 years but I remember that would sometimes break stuff.
I cannot find your tb_btnAddUserSubmit declaration in .cs code behind, and the handler.
You need to have a AddUser.Designer.cs class. Make sure to check that.
You also need to register the event in OnInit, instead of Page_load.
For those who worked with bootstrap, knows it does not support input type = file, so I hide the asp:fileupload using jQuery (after document ready), and have and for the solution (as you can see from the code
<script type="text/javascript">
$(document).ready(function ()
{
$('#columnSelect').change(function ()
{
getImportColumnOrder();
});
// change file upload style similiar to bootstrap style
//$('#uploader').hide();
$('#uploader').change(function ()
{
var val = $(this).val();
var file = val.split(/[\\/]/);
$('#file').val(file[file.length - 1]);
});
});
function getImportColumnOrder()
{
var order = '';
$('#selectOrer').val('');
$('select', $('#MappingTable')).each(function ()
{
order += $(this).prop('selectedIndex') + ',';
});
$('#selectOrder').val(order.substr(0, order.length - 1));
}
</script>
<div class="span12">
<asp:DropDownList ID="ddl_DBTableList" runat="server" CssClass="combobox" Style="display: inline">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Import_Test">Import_Test</asp:ListItem>
</asp:DropDownList>
<asp:FileUpload ID="uploader" runat="server" CssClass="btn" />
<div class="input-append" style="display: inline;">
<input id="file" class="input-medium" type="text" />
<a class="btn" onclick="$('input[id=uploader]').click();">Select File</a>
</div>
<p />
<div>
<asp:Button ID="btn_uplaod" runat="server" OnClick="doUpload" Text="Upload" CssClass="btn" />
</div>
<p />
<asp:Label ID="result" runat="server" ForeColor="Red"></asp:Label>
<p />
<asp:Label ID="data" runat="server" BackColor="#CCCCCC"></asp:Label>
<p />
<asp:Button ID="btn_import" runat="server" Text="Next" OnClick="doImport" OnClientClick="getImportColumnOrder();return true;" Visible="false" CssClass="btn btn-success" />
</div>
Steps are click on the "Select File", a file chooser popped up for file selection. After double clicked a file, the file location gets displayed in , and also the asp fileupload control.
In Chrome and FF, when I clicked btn_uplaod (button), it runs as supposed. In IE, it will clear the fileupload's content and does nothing (a client side action) and will not do any postback.
If this is a program problem, then maybe Chrome and FF will not run correctly. I'm suspecting is there anything that I need to add to make IE run as suppose to?
image after click upload (IE), (Browse link is cleared)
image after click upload (Chrome), (Run as supposed to)
thanks!
I've made a simple version for those of you who wants to try on Visual studio:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="postback_problem.aspx.cs" Inherits="postback_problem" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="uploader" runat="server" CssClass="btn" /> <div> <input id="file" class="input-medium" type="text" /> <a onclick="$('input[id=uploader]').click();">Select File</a> </div> <p /> <div> <asp:Button ID="btn_uplaod" runat="server" OnClick="doUpload" Text="Upload" CssClass="btn" /> </div> <asp:Label ID="result" runat="server"></asp:Label> </div> </form> </body> </html>
and
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class postback_problem : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void doUpload(object sender, EventArgs e) { result.Text = "no problem!"; } }
I have a button in my asp.net web application. When i clicking the button it will hide the text box in the same webs application .
If is it possible that anyone help me its very useful
Thank you
If the button is an html button then you can use javascript to do this:
onclick of button call following js:
document.getElementById(textBoxId).style.display = "none";
or
document.getElementById(textBoxId).style.visibility = "hidden";
from code behind TextBoxId.Visible = false;
from Javascript document.getElementById('<%=TextBoxId.ClientId%>').style.dispaly="none";
You can do it with JQuery:
<script>
$("#myButton").click(function () {
$("#myTextBox").hide("slow");
});
</script>
This is what you are looking for:
HideTextBox.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="HideTextBox.aspx.cs" Inherits="HideTextFields" %>
<!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>
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="BtnHide" runat="server" onclick="Button1_Click"
Text="Hide TextBox" />
</div>
</form>
</body>
</html>
And the code behind file
HideTextBox.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class HideTextFields : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control c in form1.Controls)
{
if (c is TextBox)
c.Visible = false;
}
}
}