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);
}
}
}
Related
I'm building a simple page with ASPX.
In this page I display a file component. With this component, the user can select a local file:
<div class="row">
<form class="form-horizontal">
<div class="form-group">
<input type="file" id="selectFile" >
</div>
</form>
</div>
Now, I want to set this file programmatically. So from my Default.aspx.cs code I have this:
protected void Page_Load(object sender, EventArgs e)
{
String s = Request.QueryString["idEsame"];
//RECUPERO IL FILE ED IL PATH DEL FILE
string[] fileEntries = Directory.GetFiles("C:\\Users\\michele.castriotta\\Desktop\\deflate_tests");
foreach (string fileName in fileEntries)
{
// here i need to compare , i mean i want to get only these files which are having these type of filenames `abc-19870908.Zip`
if(fileName == "file")
{
}
}
}
Now if the filename is "file" then I want to load automatically this file on the page.
how can I do this?
STEP1: IN THE PAGE ( sample.aspx)
INSERT THE FOLLOWING CODE:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="sample.aspx.cs" Inherits="sample" %>
<!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>
Select File:
<asp:FileUpload ID="FileUploader" runat="server" />
<br />
<br />
<asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
STEP2:
in the code page say for example ( sample.aspx.cs)
INSERT THE FOLLOWING CODE:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class sample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploader.HasFile)
try
{
FileUploader.SaveAs(Server.MapPath("confirm//") +
FileUploader.FileName);
Label1.Text = "File name: " +
FileUploader.PostedFile.FileName + "<br>" +
FileUploader.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
}
}
If your pattern is "{3 letter}-{8 numbers}.{Zip}", you can simply filter the files using .Where(f => myRegex.IsMatch(f)):
RegexOptions options = RegexOptions.IgnoreCase;
string pattern = #"^\w{3}-\d{8}\.zip$";
string directoryPath = "C:\\Users\\michele.castriotta\\Desktop\\deflate_tests";
var fileEntries = Directory.GetFiles(directoryPath).Where(f => myRegex.IsMatch(f));
foreach (string fileName in fileEntries)
{
// Process
}
Explain : I want to display data from database on google map but is shows that google API key is needed, I already put it inside script tag but still error is occured.I attached code of default.apsx page and default.aspx.cs page.
plaese help me our this issue.
Error : You need to set the Google Maps API Key
my code is:
//Default.aspx
<%# Register Assembly="GMaps" Namespace="Subgurim.Controles" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:GMap ID="GMap1" runat="server" Width="500px" Height="500px" />
</div>
</form>
</body>
</html>
//default.aspx.cs
using Subgurim.Controles;
using Subgurim.Controles.GoogleChartIconMaker;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GLatLng mainLocation = new GLatLng();
GMap1.setCenter(mainLocation, 14);
XPinLetter xpinLetter = new XPinLetter(PinShapes.pin_star, "H", Color.Blue, Color.White, Color.Chocolate);
GMap1.Add(new GMarker(mainLocation, new GMarkerOptions(new GIcon(xpinLetter.ToString(), xpinLetter.Shadow()))));
List<lOCATION> tblobj = new List<lOCATION>();
using (MapDATAEntities dbcontext =new MapDATAEntities())
{
tblobj = dbcontext.lOCATIONs.Where(i => i.country.Equals("india")).ToList();
}
PinIcon p;
GMarker gm;
GInfoWindow win;
foreach (var i in tblobj)
{
p = new PinIcon(PinIcons.truck, Color.Cyan);
gm = new GMarker(new GLatLng(Convert.ToDouble(i.lat), Convert.ToDouble(i.lng)),
new GMarkerOptions(new GIcon(p.ToString(), p.Shadow())));
win = new GInfoWindow(gm, i.city + " <a href='" + i.empImg + "'>Read more...</a>", false, GListener.Event.mouseover);
GMap1.Add(win);
}
}
}
}
Im getting the following error when I click on any item in my ListBox:
Invalid postback or callback argument. Event validation is enabled
using in configuration or <%#
Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback
events originate from the server control that originally rendered
them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
However, I've set AutoPostBack="True" in my ListBox and also in my DropDownList:
<%# Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Form1.aspx.cs" Inherits="WebApplication1.Form1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="dropDown" runat="server" AutoPostBack="True" Height="19px" Width="226px">
</asp:DropDownList>
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="349px" Width="501px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" ></asp:ListBox>
<br />
</form>
</body>
</html>
and I've set if (!Page.IsPostBack) in my .cs code:
using System;
using System.Collections.Generic;
using System.Web.UI;
namespace WebApplication1
{
public partial class Form1 : System.Web.UI.Page
{
public ServiceReference1.WebService1SoapClient client= new ServiceReference1.WebService1SoapClient();
public 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();
List<string> klubi = new List<string>();
klubi.Add("Vsi klubi");
foreach (localhost.Klub ig in asd.vrniVseKlube())
{
klubi.Add(ig.naziv);
}
dropDown.DataSource = klubi;
dropDown.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
}
}
Anyone know how to get rid of the error?
The error it self says you need to add "<%# Page EnableEventValidation="true" %> " in page, like below :-
" <%# Page Language="C#" AutoEventWireup="false" EnableEventValidation="false" CodeBehind="Form1.aspx.cs" Inherits="WebApplication1.Form1" %>"
and the error gets sorted out.
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/>");
}
}
}
}
I have created a TextBox dynamically, and i am getting the value of the textbox when i click the button. But the value entered in the dynamic textbox gets empty when i click the button.
Below is my ASPX Code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Reports.WebForm1" %>
<!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>
</div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack = "true">
<asp:ListItem>Text</asp:ListItem>
<asp:ListItem>Check</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="GetTextBoxValue" />
</form>
</body>
</html>
CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Reports
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox TB = new TextBox();
TB.ID = "abc";
form1.Controls.Add(TB);
Response.Write(Request.Form["abc"]);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
createcontrol();
}
protected void createcontrol()
{
if (DropDownList1.SelectedValue.ToLower().Trim() == "text")
{
TextBox TB = new TextBox();
TB.ID = "abc";
form1.Controls.Add(TB);
}
}
}
}
When you use dynamic controls in .NET and want their values to be accessible after postback you need to create them within the Page_Init method of the page. In essence its not working because ViewState has already been set by the time you created the controls. See this guide for detailed info on this topic https://web.archive.org/web/20211020131055/https://www.4guysfromrolla.com/articles/081402-1.aspx . To fix the problem elevate your Textbox instantiation code to the Page_Init method and all should be well.