Calling a class from a button on an aspx page - c#

My page just seems to refresh. I cannot call the LottoTest() class onclick.
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Draw.aspx.cs" Inherits="Lotto.Draw" %>
<!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>
<h1>Welcome to the Lotto draw</h1>
<asp:Button ID="button1" runat="server" Text="DRAW" OnClientClick="LottoTest()" />
</div>
</form>
</body>
</html>
Draw.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lotto
{
public partial class Draw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void LottoTest()
{
Dictionary<int, int> numbers = new Dictionary<int, int>();
Random generator = new Random();
while (numbers.Count < 6)
{
numbers[generator.Next(1, 49)] = 1;
}
string[] lotto = numbers.Keys.OrderBy(n => n).Select(s => s.ToString()).ToArray();
foreach (String _str in lotto)
{
Response.Write(_str);
Response.Write("<br/>");
}
}
}
}

The issue is that you are making a client request. Change OnClientClick to OnClick (without the brackets).
Then change your method signature to:
public void LottoTest(object sender, EventArgs e) instead of public void LottoTest()

Remove OnClientClick from markup and use OnClick
Markup
<!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>
<h1>Welcome to the Lotto draw</h1>
<asp:Button ID="button1" runat="server" Text="DRAW" OnClick="LottoTest" />
</div>
</form>
</body>
</html>
Add sender and eventargs as parameters to your method
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lotto
{
public partial class Draw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void LottoTest(object sender, EventArgs e)
{
Dictionary<int, int> numbers = new Dictionary<int, int>();
Random generator = new Random();
while (numbers.Count < 6)
{
numbers[generator.Next(1, 49)] = 1;
}
string[] lotto = numbers.Keys.OrderBy(n => n).Select(s => s.ToString()).ToArray();
foreach (String _str in lotto)
{
Response.Write(_str);
Response.Write("<br/>");
}
}
}
}

Related

Asp.net ListBox OnSelectedIndexChanged not firing

I created ListBox, set AutoPostBack="true" and enableEventValidation="false". When I click on any item in my ListBox, nothing changes despite OnSelectedIndexChange event. I set the message "Hello" must pop out OnSelectedIndexChanged but that does not happend.
WebForm1.aspx
<%# Page Language="C#" enableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="235px" Width="436px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
</form>
</body>
</html>
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 WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();
localhost.WebService1 asd = new localhost.WebService1();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> igralci = new List<string>();
foreach (localhost.Igralec ig in asd.vrniVseIgralce())
{
igralci.Add(ig.id + ", " + ig.ime + ", " + ig.priimek + ", " + ig.letoRojstva + "\n");
}
ListBox1.DataSource = igralci;
ListBox1.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
}
}

display json data in grid view from server using .net

I have been created simple shopify app to retrieve product details.
When i access code from gihub.
It run successfully and display product details in text box.
I need to simple change to show the product details in grid view.
Here is dafault.aspx:
existing code;
default.aspx:
<%# Page Language="C#" AutoEventWireup="true" codefile="Default.aspx.cs" Inherits="SampleWebApplication._Default" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="APIOutput" TextMode="MultiLine" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Here is my output screenshot http://s22.postimg.org/xj9zacxa9/untitled.jpg
I need to display product details in gridview,
default.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopifyAPIAdapterLibrary;
using System.Configuration;
using System.Web.Services;
namespace SampleWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState;
ShopifyAPIClient client
= new ShopifyAPIClient(state);
APIOutput.Text = (string)client.Get("/admin/products.json");
}
}
}
but I just confused with codefile, Can anyone help me get the product details in gridview?
Any help would be highly appreciated.
Thanks in advance.
<%# Page Language="C#" AutoEventWireup="true" codefile="Default.aspx.cs" Inherits="SampleWebApplication._Default" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
and default.cs code as follows:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopifyAPIAdapterLibrary;
using System.Configuration;
using System.Web.Services;
namespace SampleWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState;
ShopifyAPIClient client
= new ShopifyAPIClient(state);
string shopData = (string)client.Get("/admin/products.json");
JavascriptSerializer serializer = new JavascriptSerializer();
// Here Product is a object class which contains all of the attribute that JSON has.
List<Product> lstProduct = serializer.Deserialize<Product>(shopData);
GridView1.DataSource = lstProduct;
GridView1.DataBind();
}
}
}
and the product class look like this :
public Class Product
{
// Here is all the properties you want to discribed.
// This property is same as the property available in your JSON file.
}
Hopefully this will help.

asp.net show string from a class on html page

First I have index.aspx and below that there is a c# class called index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index : Page
{
public string test = "hello";
protected void Page_Load(object sender, EventArgs e)
{
test = "Hello lesley";
}
}
But how can I show the value of the 'test' string in the html page?
In your markup use asp:Label as
<asp:Label runat="server" ID="MyLabel"></asp:Label>
And in your backend code use Text property accordingly
public string test = "hello";
if(!IsPostBack)
{
MyLabel.Text = test;
}
Try this
protected void Page_Load(object sender, EventArgs e)
{
test = "Hello lesley";
Label label = new Label();
label.Text = test;
Page.Controls.Add(label);
}
Alternatively, if you already have that property in your index.aspx.cs, you can do this in index.aspx html page.
<!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">
</head>
<body>
<form id="form1" runat="server">
Hi <b><%=test%></b>
</form>
</body>

Dynamic IDs on asp:TextBox?

This is my code, on a .ascx page :
<% for (int i = 1; i <= 10; i++)
{ %>
<asp:TextBox ID="myTextBox_<%=i %>" runat="server" Width="100%" CssClass="focus_out reset_content"></asp:TextBox>
<% } %>
but I get myTextBox_<%=i %> is not a valid identificator. So, how can I put "Dynamic IDs"?
You need to create a container for the textboxes, such as a Panel control, and then use the Page_Load in the code behind to loop through and add the text boxes to the panel.
Example:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlContainer" runat="server" />
</div>
</form>
</body>
</html>
Code behind:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++) {
TextBox txtNewTextBox = new TextBox();
txtNewTextBox.ID = "myTextBox_" + i;
pnlContainer.Controls.Add(txtNewTextBox);
}
}
}
Here is the link for Dynamically adding textbox control in ASP.Net. Hope it works for you.

Ajax autocomple extender from a database

I want to create a textbox (for entering names) in my aspx application which suggests names of Employees from database.
I am pasting my code below. I am using Mysql
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
ServiceMethod="GetCompletionList2" TargetControlID="TextBox1"
UseContextKey="True">
</asp:AutoCompleteExtender>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
Default.aspx.cs:
// here we use a readymade class for database operations to Mysql server
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Gbs.DAL; // for ..redymade class..
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
{
myDb db = new myDb();// its class in Dal.cs
string sql = "Select * from empdata Where empname like #prefixText";
DataTable dt = db.getDataTable(sql); // method in redymade class
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["empname"].ToString(), i);
i++;
}
return items;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
// there is no any error, the program runs successfully
AutocompleteExtender needs an .asmx web service which can be called from javascript.
It won't work from your Default.aspx.

Categories

Resources