I already read Stack Overflow question How to bind a List to a gridview?.
But I need to bind a List<DateTime> to a gridview, but MAINLY I also want be able to access the DateTime value from within a ItemTemplate. How do I do that? When I was working with a DataTable, I used to do it like that:
<%# Eval("SDIndex") %>
How would I do it linking it directly to the List<DateTime>?
You may try the following:
<%# Page Language="C#" %>
<%# Import Namespace="System.Linq" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var dates = Enumerable.Range(1, 20).Select(x => new DateTime(2011, 4, x)).ToList();
grd.DataSource = dates;
grd.DataBind();
}
</script>
<!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">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# ((DateTime)GetDataItem()).ToString("dd/MM/yyyy") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
This should also work:
<%# string.Format("{0:dd/MM/yyyy}", GetDataItem()) %>
Related
I am trying to use Google Recaptcha on on my web pages. I keep getting an error saying
GoogleReCaptcha.GoogleReCaptcha)(System.Web.HttpRuntime.WebObjectActivator.GetService(typeof(GoogleReCaptcha.GoogleReCaptcha))));
Below is the screen shot:
Below is my aspx page code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TryAgain.WebForm1" %>
<%# Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<h1>Google ReCaptcha Form</h1>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="XXXX" PrivateKey="XXX" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
</p>
</div>
</form>
</body>
</html>
Below is my aspx.cs file code:
protected void btn_Click(object sender, EventArgs e)
{
if (ctrlGoogleReCaptcha.Validate())
{
//submit form
lblStatus.Text = "Success";
}
else
{
lblStatus.Text = "Captcha Failed!! Please try again!!";
}
}
GoogleRecaptcha that I downloaded has properties like so:
I am struggling with this error for hours. Any help will be highly appreciated.
when i am using javascript code in that format like
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) {
var first = document.getElementById('<%=txtfirst.ClientID %>');
var second = document.getElementById('<%=txtText.ClientID %>');
//Condition to check whether user enters text in two textboxes or not
if ((sender.value.length >= 1 && first.value.length >= 1) && (sender.value.length >= 1 && second.value.length >= 1))
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />
</div>
</form>
</body>
</html>
its runnung perfect and javascript work fine,
but when i am using this javascript code in another page like
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) {
var first = document.getElementById('<%=txtfirst.ClientID %>');
var second = document.getElementById('<%=txtText.ClientID %>');
//Condition to check whether user enters text in two textboxes or not
if ((sender.value.length >= 1 && first.value.length >= 1) && (sender.value.length >= 1 && second.value.length >= 1))
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false"/>
</asp:Content>
javascript not working and no error issue.
i am using visual studio 2010
asp.net c#.
Try to pass actual ID of button you get after rendering the page. (you can copy from view source)
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'ContentPlaceHolder1_btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'ContentPlaceHolder1_btnButton')"></asp:TextBox>
it will work.
Thanks
This question already has an answer here:
Ajax in UserControl
(1 answer)
Closed 8 years ago.
I am using CalenderExtender in my project inside in content place holder and calender extender is in a UserControl. This control is working in normal aspx-page but when i am dragging this control in ContentPlaceHolder then it is not working. Actually the Calender is not appearing in textBox below is my code which i used in my project.
ASPX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Calender1.ascx.cs" Inherits="Facultymanagement.Calender1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:TextBox ID="txtcalender" runat="server">
</asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="MM/dd/yyyy"
TargetControlID="txtcalender" PopupButtonID="txtcalender"></asp:CalendarExtender>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (CalendarExtender1.SelectedDate.ToString() != "")
{
txtcalender.Text = CalendarExtender1.SelectedDate.ToString();
}
}
public string TextBox1Value
{
get
{
return txtcalender.Text;
// return Convert.ToString(Calendar.SelectedDate);
}
set { txtcalender.Text = value; }
}
}
This is the place where i am trying to access the value:
protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = calender1.TextBox1Value.ToString();
}
You need to find control first
ContentPlaceHolder mpContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("ContentPlacename");
if (mpContentPlaceHolder1 != null)
{
Button btn_searsh;
btn_searsh = (Button)mpContentPlaceHolder1.FindControl("main_search");
btn_searsh.CssClass += " " + "btn-primary";//to pass the rentpage class
}
I think you must doo something wrong.
The following code works:
WebUserControl1.ascx:
<%# Control Language="C#" ClassName="WebUserControl" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<script runat="server">
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
</cc1:CalendarExtender>
Default.aspx:
<%# Page Language="C#" %>
<%# Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<uc1:WebUserControl ID="WebUserControl2" runat="server" />
<uc1:WebUserControl ID="WebUserControl3" runat="server" />
<uc1:WebUserControl ID="WebUserControl4" runat="server" />
</form>
</body>
</html>
I'm having some problems with my code. Basically I want a DDL that has a list of values from a table sorted by the primary key. When an item is selected and a button is clicked, a Grid View shows the rest of the information about the respective record. I'll post the code I have now. For some reason, nothing appears in the DDL at all and I can't seem to figure out why.
<%# Page Language="C#" %>
<%# Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load()
{
if (!Page.IsPostBack)
{
grv1.DataSource = srcCSC;
grv1.DataBind();
}
}
void btn1_click(object sender, EventArgs e)
{
DataSourceSelectArguments objSelectArg = new DataSourceSelectArguments();
DataView objView = (DataView)srcCSC.Select(objSelectArg);
srcCSC.Select(objSelectArg);
//rebind
grv1.DataSource = srcCSC;
grv1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
City:
<asp:DropDownList ID="ddlCity" runat="server" DataSourceID="srcCSC" DataTextField="City" DataValueField="City" /><br />
<asp:SqlDataSource
ID="srcCSC"
runat="server"
ConnectionString="<%$ ConnectionStrings:xyz1%>"
SelectCommand="Select City, CompanyName, ContactName, Relationship From CustomerSupplierCity Where City=#City">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCity" Name="City" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Button ID="btn1" Text="Select" runat="server"
OnClick="btn1_click" />
<asp:GridView ID="grv1" runat="server" AllowPaging="true" DataKeyNames="City,CompanyName,Relationship" />
</div>
</form>
</body>
</html>
In my main page i have a label and a button ..on button click i want to open a new popup window using showModalDialog ..In my popup window i have a textbox and a close button ..i want to put something in textbox and click the button . popup need to be close ..Textbox value should show in main page lebel .....
My Main page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ModalPopupValuePass._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>
<script type="text/javascript">
function Abc()
{
debugger;
//window.showModalDialog("FIRE/Popup.aspx",null,"resizable: yes");
var style = 'dialogWidth:350px;dialogHeight:100px;dialogleft:200px;dialogtop:200px;status:no;help:no;';
var respond = window.showModalDialog("FIRE/Popup.aspx", this, '', style);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:HiddenField ID="hf1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Abc();"/>
</div>
</form>
</body>
</html>
In cs page of main page
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = hf1.Value;
}
My popup page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Popup.aspx.cs" Inherits="ModalPopupValuePass.FIRE.Popup" %>
<!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>
<script type="text/javascript">
function Def()
{
debugger;
window.opener.document.getElementById('<%=hf1.ClientID %>').value = document.getElementById('<%=TextBox1.ClientID %>').value;
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Close" OnClientClick="Def();" />
</div>
</form>
</body>
</html>
But i am not getting the mainpage control in popup ...need help..!!