Aspnet C# call a particular function in postback - c#

Good afternoon, I'm learning aspnet, and web development, and I'm having a hard time understanding how I can call a certain postback function ... could anyone please answer me how do I do this...
codigo:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnTest.Value = hid.Value;
}
protected void function2()
{
//param other function here
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>test postback</title>
<script type="text/javascript">
function doTest() {
var nome = document.getElementById("nome");
var hid = document.getElementById("hid");
hid.value = nome.value;
return true;
}
</script>
</head>
<body>
<form id="form1" name="form1" runat="server">
<div>
<input type="text" id="nome" value="%%" name="nome" runat="server"/>
<input type="hidden" name="hid" id="hid" value="" runat="server" />
<input type="submit" id="btnTest" runat="server" name="btnTest" value="button" onclick="doTest();" />
</div>
</form>
</body>
</html>

If you want to use html controls you have to use the onserverclick method:
<input runat="server" type="button" onserverclick="ButtonClick"/>
In you code behind:
protected void ButtonClick(object sender, EventArgs e)
{
//Your code goes here
}
You can also use the built in WebControls:
<asp:Button runat="server" ID="btnTest" OnClick="ButtonClick"/>
The code behind would be the same.

Related

C# asp.net WebForm add JS and run it from code behind

I have this WebForm Html:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="GetLink.aspx.cs" Inherits="GetLink" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" runat="server" id="hdnVal" value="55"/>
</div>
</form>
</body>
</html>
And i want to add to this code a JavaScript function and run it with this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!ClientScript.IsStartupScriptRegistered("key1"))
{
ClientScript.RegisterStartupScript(GetType(), "key1", #"<script type=""text/javascript"">function callMyJSFunction() { document.getElementById(""hdnVal"").value='5'; }</script>");
}
ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction();</script>");
string resutOfExecuteJavaScript = hdnVal.Value;
}
When i run it the value of hdnVal keep the 55 value and not change. Any idea what is the problem?
Your code in Page_Load event should call ClientScript.RegisterClientScriptBlock when registering the JavaScript function of callMyJSFunction, whereas in your code you are registering this function as a startup script. This is the only mistake in your code.
So, if you change your server-side code to as below, then it will work according to your expectations.
protected void Page_Load(object sender, EventArgs e)
{
if (!ClientScript.IsClientScriptBlockRegistered("key1"))
{
//register your javascript function
ClientScript.RegisterClientScriptBlock(GetType(), "key1", #"<script type=""text/javascript"">function callMyJSFunction() { document.getElementById(""hdnVal"").value='5'; }</script>");
}
ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction();</script>");
string resutOfExecuteJavaScript = hdnVal.Value;
}
The first problem is you are creating function in Clientscript while you can simply put the function in javascript and then just do the calling part.Second problem is that the time your function is calling that hiddenfield for view its not available on document simply means stop putting your code on page load and use a button click event instead.Third problem is you are using multiple inverted commas at so many places which aren't required.
This worked for me
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('document').ready()
{
function callMyJSFunction()
{
debugger;
document.getElementById('hdnVal').value = '5';
alert(document.getElementById('hdnVal').value);
}
// - including fonts, images, etc.
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" runat="server" id="hdnVal" value="55"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
and on cs page
protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction()</script>",false);
string resutOfExecuteJavaScript = hdnVal.Value;
}

Dynamically Create a specific div with its children in C#

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);

One step process file upload

When a user wants to upload a file (currently there are 4 places in the form that allow for this), they first have to “Choose File” and then they have to click on “Upload”. If they miss the 2nd "Upload" step, there is no indication to them or us.
Is there a way to combine the “two-step” process to a single step (select and upload).
Use this link to know more about it
http://www.c-sharpcorner.com/UploadFile/2b481f/uploading-a-file-in-Asp-Net-web-api/
And you can also use this code
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.9.1.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileupload1" runat="server" />
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="upload" style="display:none" />
</div>
<script type="text/javascript">
var isfirst = true;
$(function () {
$('#<%= fileupload1.ClientID %>').on('change', function (e) {
console.log('change triggered');
$('#<%= btn.ClientID%>').trigger('click'); // trigger the btn button click which i have hidden using style='display:none'
});
});
</script>
</form>
</body>
Code behind
protected void btn_Click(object sender, EventArgs e)
{
//TODO
}

How to find pagenumber of current page of PDF inside web form using object tag?

I have an application where a PDF file is opened inside the web form as shown in the below code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
<div id="divPDF" runat="server">
<object data="<%=pdfPath%>" type="application/pdf" style="height:500px; width: "500px" >
</object>
</div>
</div>
</form>
</body>
</html>
protected string pdfPath;
protected void Page_Load(object sender, EventArgs e)
{
divPDF.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.FileName != "")
{
FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
pdfPath = FileUpload1.FileName;
divPDF.Visible = true;
}
}
It is working fine and I can view and read the pdf. Now what I need is when I scroll through the pages the current pagenumber should appear in a text box.

Session variable lost when click sign in

In first visit, Page_Load is running and session store correctly. When I click on Sign In button, Page_Load is call again, and then call btnSignIn_Click function, but Session is empty!
public partial class LoginPage : System.Web.UI.Page
{
UserItem userItem = null;
protected void Page_Load(object sender, EventArgs e)
{
//Initialize and validate post
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
protected void btnSignIn_Click(object sender, EventArgs e)
{
if (Session["post"] != null)
{
RequestObj post = Session["post"] as RequestObj;
userItem = Functions.LogIn(post);
}
Response.Redirect("LogIn.aspx");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%# Page Language="C#" AutoEventWireup="true" CodeBehind=" LoginPage.aspx.cs"
Inherits="myNameSpace.LoginPage " %>
<!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">
<meta name="viewport" content="width=320"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%; text-align:center;">
<%if (userItem == null)
{%>
Username:
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
Password:
<asp:TextBox ID="txtPass" runat="server" TextMode="Password">
</asp:TextBox>
<br />
<asp:Button ID="btnSignIn" runat="server" Text="Sign In"
onclick="btnSignIn_Click" />
<%}else{%>
<%=userItem.LoginName%>
<br />
<%=userItem.LoginTime.ToString()%>
<br />
<asp:Button ID="btnSignOut" runat="server" Text="Sign Out"
onclick="btnSignOut_Click" />
<%}%>
</div>
</form>
</body>
</html>
Did you mean to only set session when the page renderes initially?
Try wrapping the assignment like this
if(!IsPostBack)
{
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
This will prevent this from being reset during a postback
Otherwise I would check the value of post in a debugger

Categories