Passing Textbox Input as a Parameter in Code Behind OdbcCommand - javascript

The goal is that as a user I can enter a string into a text box. That string is passed to the codebehind as a parameter to a stored procedure call and returns an alert in the window.
The following code is not executing the stored procedure or returning an alert.
Markup:
<script type="text/javascript">
$(function PageLoad() {
document.getElementById('<%= ValueHiddenField.ClientID%>.value' = '<%= RemoveTote_TextBox.ClientID%>')
});
</script>
<div id="RemoveToteForm" runat="server" class="col text-center">
<div class="row">
<div class="col text-center">
<h2><%: Title %></h2>
</div>
</div>
<asp:TextBox ID="RemoveTote_TextBox" runat="server" />
<br/>
<input type="submit" name="SubmitButton" value="Submit" onclick="PageLoad()" />
<asp:hiddenfield id="ValueHiddenField"
onvaluechanged="ValueHiddenField_ValueChanged"
value=""
runat="server"/>
</div>
</asp:Content>
Code Behind:
protected void ValueHiddenField_ValueChanged(object sender, EventArgs e)
{
string ntsh = ValueHiddenField.Value;
using (OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnectToClient"].ConnectionString))
{
OdbcCommand cmd = new OdbcCommand("{call usp_Remove_Tote (?)}", con);
OdbcParameter Tote_number = cmd.Parameters.Add("#Tote_number", OdbcType.Char);
Tote_number.Value = ntsh;
con.Open();
String result = cmd.ExecuteScalar().ToString();
con.Close();
if (result != "Success+")
{
Response.Write("<script>alert('" + result + "'); </script>");
}
else
{
Response.Write("<script>alert('" + result + "'); </script>");
}
}
}

Related

Display Number Progress Bar ASPNet

first
aspx:
<div class="row">
<div class="col-sm-2">
<asp:TextBox TextMode="Number" runat="server" ID="FromNum" placeholder="From"/>
</div>
<div class="col-sm-4">
<asp:TextBox TextMode="Number" runat="server" ID="ToNum" placeholder="To"/>
</div>
<div class="col-sm-2">
<asp:Button ID="BtnRec" Text="Go" runat="server" OnClick="Btn_Click" />
</div>
</div>
<div class="col-sm-2">
<asp:Label runat="server" ID="Indication" />
</div>
second
aspx.cs
protected void Btn_Click(object sender, EventArgs e)
{
DataTable dataTable = SqlHelper.Client.GetDataTable(sql);;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string conMun = ((int)dataTable.Rows[i]["ConfirmNum"]).ToString();
string subject = "No: " + conMun;
this.Indication.Text = (i + 1).ToString() + " of" + dataTable.Rows.Count;
this.SendEmail(subject, html, email); //mail
}
}
I need
Indication
to be constantly updated/refreshed by the number.
How can this be done?

Unable to get values from individual check boxes in JavaScript

I am working on an asp.net webforms page. I have three html checkboxes and if any of them are checked I have to append the corresponding values to a JavaScript string. When I execute the btnSave, the saveOrderTypes() is called and goes up to alert("D"); and then exits. I don't see any JavaScript error in the console. I am not sure why this is happening. I tried to get the values using jQuery and similar issue happens.
<asp:Content ID="scriptArea" runat="server" ContentPlaceHolderID="headerScript">
<script>
function saveOrderTypes() {
alert("In saveOrderTypes");
var orderTypes = '';
if (document.getElementById('chkDelivery').checked) {
orderTypes = 'D';
alert("D");
}
if (document.getElementById('chkUPS').checked) {
orderTypes = orderTypes + 'U';
alert("U");
}
if (document.getElementById('chkCustomer').checked) {
orderTypes = orderTypes + 'C';
alert("C");
}
alert("orderTypes: " + orderTypes);
//more code here
}
$(document)
.ready(function () {
});
</script>
</asp:Content>
<asp:Content ID="mainDivision" ContentPlaceHolderID="mainContent" runat="server">
<div class="row" style="width: 100%">
<div class="col-sm-4">
<input type="checkbox" id="chkDelivery">
<label >Delivery</label><br>
<input type="checkbox" id="chkUPS">
<label >UPS</label><br>
<input type="checkbox" id="chkCustomer">
<label >Customer</label><br>
</div>
</div>
<button type="button" id="btnSave" onclick="javascript:saveOrderTypes();return false;"
>Save</button>
</asp:Content>

I can't get values of Listbox in asp.net after it was daynamically added in Javascript

I daynamically create options of select in javascript, and click a asp:Button to submit the added options. but i can't get that from ListBox. pls help me!!
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Hello.aspx.cs" Inherits="BeyondInfo.Hello" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to My First ASP.NET</title>
<script type="text/javascript">
function add() {
var count = document.getElementById("count");
if (count.value == "") {
count.value = 1;
}
count.value = parseInt(count.value) + 1;
var num = count.value;
var listBox2 = document.getElementById("ListBox2");
var optn = document.createElement("OPTION");
optn.value = num;
optn.text = num + "_add";
listBox2.options.add(optn);
}
</script>
</head>
<link href="Common.css" rel="stylesheet" />
<body>
<form id="form1" runat="server">
<div style="height: 277px">
<asp:ListBox ID="ListBox2" runat="server">
<asp:ListItem>b1222222222222222222222222222</asp:ListItem>
<asp:ListItem>b2</asp:ListItem>
<asp:ListItem>b3</asp:ListItem>
<asp:ListItem>b4</asp:ListItem>
</asp:ListBox>
<input id="count" type="hidden" />
<input id="btnAdd" type="button" value="ADD" onclick="add()"/>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
</body>
</html>
</p>
Hello.aspx.cs
public partial class Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string sel = "";
foreach (ListItem item in ListBox2.Items) {
sel +=item.Text + ",";
}
Label1.Text = sel;
}
}
Try looking at this post answer by NC01. You may be able to achieve what you want by doing something similar.
Basically they store the added fields in a hidden field so they are accessible server side.
I believe that your problem is that when you add an item to a DropDownList client-side (using JavaScript) that when a PostBack occurs, the item will disappear. The only way around this is to add a hidden server-control to the page and when adding the item client-side, add it to the hidden server-control also, so that when a PostBack occurs you can also add it server-side.
Here's an example.
https://forums.asp.net/t/1287204.aspx?How+to+add+the+values+to+listbox+control+using+javascript
aspx file:
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem value="1">Item 1</asp:listitem>
<asp:listitem value="2">Item 2</asp:listitem>
<asp:listitem value="3">Item 3</asp:listitem>
<asp:listitem value="4">Item 4</asp:listitem>
<asp:listitem value="5">Item 5</asp:listitem>
<asp:listitem value="6">Item 6</asp:listitem>
</asp:DropDownList>
<input id="DropDownList1NewItems" type="hidden" name="DropDownList1NewItems" runat="server">
<input type="button" onclick="addItem();" value="Add Item">
</form>
<script type="text/javascript">
<!--
function addItem()
{
// Add the item here...
var ddlRef = document.getElementById('<%= DropDownList1.ClientID %>');
var newOption = window.document.createElement('OPTION');
newOption.text = 'SomeNewItem';
newOption.value = 'SomeNewItem';
ddlRef.options.add(newOption);
var hiddenElementRef = document.getElementById('<%= DropDownList1NewItems.ClientID %>');
if ( hiddenElementRef.value.length > 0 )
hiddenElementRef.value += ';';
hiddenElementRef.value += 'SomeNewItem';
}
// -->
</script>
aspx.cs file:
private void Page_Load(object sender, System.EventArgs e)
{
if ( this.IsPostBack )
{
if ( DropDownList1NewItems.Value.Length > 0 )
{
string [] newItemsArray = DropDownList1NewItems.Value.Split(';');
for (int i=0; i<newItemsArray.Length; i++)
{
string newItem = newItemsArray[i];
DropDownList1.Items.Add(new ListItem(newItem, newItem));
}
DropDownList1NewItems.Value = string.Empty;
}
}
}

Picking up changed values of ASP textbox

I'm trying to create a dynamic textbox that I can use to allow the entry of multiple values. I employed javascript for this purpose, thinking that this would allow me to grab the text value and place it in a list, before passing it to my web app. However, the code document.getElementById('<%=sidemgr.ClientID%>').value only picks up the initial value of sidemgr.txt from it's creation.
How do I get the updated value of this textbox? Do I have to use a javascript textbox?
Here is the javascript I have
<script type="text/javascript">
$(document).ready(function () {
$("#btnAdd").click(function () {
var value = document.getElementById('<%=sidemgr.ClientID%>').value;
var field = $("#field").val();
var input = "<div id='listDiv' />";
var newRow = "<tr><td>" + field + "</td><td>" + value + "</td></tr>";
$('#controls').append(newRow);
});
});
</script>
and the html
<div>
<asp:TextBox ID="sidemgr" runat="server"></asp:TextBox>
<br />
<input id="btnAdd" type="url" value="Add" />
<asp:Button ID="btnProcess" runat="server" Text="Process" OnClick="Process" />
<table id="controls" cellpadding="10" cellspacing="10">
</table>
</div>
C# code behind
protected void Process(object sender, EventArgs e)
{
var parameters = Request.Form["parameters"];
if (parameters != null && parameters.Count() > 0)
{
parameters.Split(',').ToList().
ForEach(p =>
{
Response.Write(p + "<br />");
});
}
}
Note that some of this is from a solution I found on SO a while ago.
Create a hiddenfield to hold the values you want in the server, the way you are doing server doesnt the value of Request.Form["parameters"]
$(document).ready(function () {
$("#btnAdd").click(function () {
var value = document.getElementById('<%=sidemgr.ClientID%>').value;
var field = $("#field").val();
var input = "<div id='listDiv' />";
var newRow = "<tr><td>" + field + "</td><td>" + value + "</td></tr>";
$("#parameters").val($("#parameters").val()+value+",")
$('#controls').append(newRow);
});
});
<div>
<asp:TextBox ID="sidemgr" runat="server"></asp:TextBox>
<input type="hidden" id="parameters" name="parameters"/>
<br />
<input id="btnAdd" type="url" value="Add" />
<asp:Button ID="btnProcess" runat="server" Text="Process" OnClick="Process" />
<table id="controls" cellpadding="10" cellspacing="10">
</table>
</div>
With this hiddenfield the server will receive the Request.Form["parameters"] with something like "value1,value2,"
Or you coud use multiple hiddenfields with name like "parameters[]", insert in the table, and work with Request.Form["parameters"] as a list

JS function is not called inside a form

I am new to JS. can u find me the problem and suggest thereby?? Js client side validation is not called. I have server side validation too. by clicking the button only the server side validation is being called. can u guys help?
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Users.aspx.cs" Inherits="JSApp.Users" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JsApp</title>
<script type="text/javascript">
function ValidatingForm() {
var b = true;
if (document.getElementById("FnTextBox").valueOf == "") {
document.getElementById("Label4").innerText = "beshi Required";
b = false;
} else {
document.getElementById("Label4").innerText = "";
}
if (document.getElementById("LnTextBox").valueOf == "") {
document.getElementById("Label5").innerText = "koravabe required";
b = false;
} else {
document.getElementById("Label5").innerText = "";
}
if (document.getElementById("EmailTextBox").valueOf == "") {
document.getElementById("Label6").innerText = "ajeeb vabe required";
b = false;
} else {
document.getElementById("Label6").innerText = "";
}
return b;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="FN"></asp:Label>
<asp:TextBox ID="FnTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="LN"></asp:Label>
<asp:TextBox ID="LnTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="EmailTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label6" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm()" Text="Send" />
</div>
</form>
</body>
</html>
aspx.cs:server side
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JSApp
{
public partial class Users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendButton_Click(object sender, EventArgs e)
{
if (ValidateForm())
{
SaveData();
}
}
private void SaveData()
{
string cs = ConfigurationManager.ConnectionStrings["SampleConnection"].ConnectionString;
SqlConnection connection=new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spInsertUsers",connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter fnParameter=new SqlParameter("#fn",FnTextBox.Text);
SqlParameter lnParameter=new SqlParameter("#ln",LnTextBox.Text);
SqlParameter emailParameter=new SqlParameter("#email",EmailTextBox.Text);
cmd.Parameters.Add(fnParameter);
cmd.Parameters.Add(lnParameter);
cmd.Parameters.Add(emailParameter);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
private bool ValidateForm()
{
bool b = true;
if (string.IsNullOrEmpty(FnTextBox.Text))
{
b = false;
Label4.Text = "Required";
}
else
{
Label4.Text = string.Empty;
}
if (string.IsNullOrEmpty(LnTextBox.Text))
{
b = false;
Label5.Text = "Required";
}
else
{
Label5.Text = "";
}
if (string.IsNullOrEmpty(EmailTextBox.Text))
{
b = false;
Label6.Text = "Required";
}
else
{
Label6.Text = "";
}
return b;
}
}
}
Try change your button click from:
<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm()" Text="Send" />
to:
<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm" Text="Send" />
See same issue here:
http://forums.asp.net/t/1069601.aspx?I+want+to+use+onsubmit+asp+net+wont+let+me+
That looks more or less correct. I would try the following:
Add a semicolon to your OnClientClick handler - i.e. "OnClientClick="return ValidatingForm();"
Put a javascript alert inside ValidatingForm (to ensure that it's actually called)
Check your comparison logic, maybe instead of: document.getElementById("FnTextBox").valueOf == ""
how about trying:
document.getElementById("FnTextBox").value.length == 0
innerscript attribute does not have cross browser compatibility. using textContent had solved the issue. when i use innerscript it runs ok in IE and Chrome but does not run in Firefox. that matter i posted as the problem.

Categories