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>
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.
I have MVC4 Web application and i am rendering a partial view(.ascx) inside a .cshtml page. I have added a dropdownlist which has OnSelectedIndexChanged and it never fires. please see the code below
Dashboard.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.ascx.cs" Inherits="iSPYCMS.Views.Dashboard" EnableViewState="true" %>
<!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>Charts Example</title>
</head>
<body style="padding:50px;background-color:white">
<form id="form1" runat="server">
<asp:DropDownList runat="server" ID="cmbTypeSeletion" Height="16px" Width="190px" EnableViewState="true" Enabled="true" OnSelectedIndexChanged="cmbTypeSeletion_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Downloads" Value="Downloads"> </asp:ListItem>
<asp:ListItem Enabled="true" Text="Plays" Value="Plays"></asp:ListItem>
<asp:ListItem Enabled="true" Text="Completion" Value="Completion"></asp:ListItem>
</asp:DropDownList>
<div>
<script type="text/javascript" src="https://www.google.com/jsapi"> </script>
<asp:Literal ID="ltScripts" runat="server"></asp:Literal>
<div id="chart_div">
</div>
</div>
</form>
</body>
</html>
Dashboard.ascx.cs
namespace iSPYCMS.Views
{
public partial class Dashboard : ViewUserControl
{
protected void cmbTypeSeletion_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Index.cshtml
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
#Html.Partial("~/Views/Dashboard.ascx")
</div>
Set AutoPostBack="True" for the dropdown list
I did search other similar questions, most of which had to do with jscript. I am having a difficult enough time trying to learn C#, so hopefully there is answer. I seems like I always get close to the answer only to find I have hit another hidden exception. What I am attempting to do is a work around for what must exist a more elegant way, but here it is: I have a check box, that when checked, changes Label1 text to "1" and unchecked changes Label1 text to "0". When the button on the page is clicked it sends data fields to a stored procedure. Even though I can see that the label value is either 1 or 0, the send to SP function always sends 'NULL' for Label1, which I assume is the default value for a blank label. So how do I get page to read the current value of the label when the button is clicked?
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-/W3C//DTD/ XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">
<script runat="server">
public void InsertCard (object source, EventArgs e)
{
SqlDataSource1.Insert();
}
public void CheckBox1_CheckChanged (object source, EventArgs e)
{
Response.Write("");
if (CheckBox1.Checked == true)
{
Label1.Text = "1";
}
else
{
Label1.Text = "0";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:DevTestConnectionString %>"
insertcommand="AddMember" InsertCommandType="StoredProcedure">
<insertparameters>
<asp:FormParameter Name="CardNumber" formfield="CardNumberBox" Type="int32" />
<asp:FormParameter Name="NameFirst" formfield="NameBox" Type="String" />
<asp:FormParameter Name="Valid" formfield="Label1" Type="string" />
</insertparameters>
</asp:sqldatasource>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<hr />
Last Name:<asp:TextBox id="NameBox" runat="server" Width="150px"></asp:TextBox>
<br>
Card Number:<asp:textbox id="CardNumberBox" runat="server" />
<br>
<asp:CheckBox ID="CheckBox1" runat="server" Text=" Valid" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
<br>
<asp:button id="Button1" runat="server" text="Add Card" onclick="InsertCard" />
</div>
</form>
</body>
</html>
Why used the label? Use the checkbox directly and set the datatype on the table column to bit. Replace the label to the below.
<asp:ControlParameter ControlID="CheckBox1" Name="Valid" PropertyName="Checked" Type="Boolean"/>
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>
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test"%>
<%# 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></title>
</head>
<body style="height: 162px">
<form id="form1" runat="server">
<div>
<asp:RadioButton ID="MCA" runat="server" Text="MCA" AutoPostBack="True"
oncheckedchanged="MCA_CheckedChanged" />
<br />
</div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="Sem1"></asp:ListItem>
<asp:ListItem Value="Sem2"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged"
ViewStateMode="Enabled">
<asp:ListItem Value="MCA101"></asp:ListItem>
<asp:ListItem Value="MCA103">MCA103</asp:ListItem>
</asp:DropDownList>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnUploadComplete="upload"/>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Event Code..
protected void upload(Object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string s = DropDownList1.SelectedValue;
string t = DropDownList2.SelectedValue;
string path= string path = Server.MapPath("~/MCA/" + s + "/" +t+ "/")+e.FileName
}
//Both s and t get the first value of Dropdownlist even if some other value selected and that's uploading is not done as per directort structure.
Both Dropdownlist have several values and postback property is true for both the list.
How to get the exact selected value of list ?
Issue is Request.Form["__VIEWSTATE"] = null when AjaxFileUpload OnUploadComplete event is called.
Fix for this issue (C# Code):
Set dropdown selection in session at page load.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__VIEWSTATE"] != null)
Session["Path"] = "//" + DropDownList1.SelectedValue + "//" + DropDownList2.SelectedValue + "//";
}
Use session value for the creation of filepath:
protected void upload(Object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string path = string.Empty;
if (Session["Path"] != null)
path = Server.MapPath("~//MCA" + (string)Session["Path"]) + e.FileName;
}
I believe you will need to add the drop down lists to the same update panel as the upload control.