How to get selected dropdownvalue in AjaxFileUpload onuploadComplete event? - c#

<%# 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.

Related

Google Recaptcha throwing System.Web.HttpRuntime.WebObjectActivator error

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.

checkbox autopostback without refreshing page asp net

I have this checkbox that I need to be AutoPostBack="True" so that I can trigger OnCheckedChanged="chkCompany_OnCheckedChanged". The problem is that I dont want the page to be refreshed and redirected, I want the user to stay put exactly where they are.
ASPX:
<asp:CheckBox OnCheckedChanged="chkCompany_OnCheckedChanged" AutoPostBack="True" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />
C#:
protected void chkCompany_OnCheckedChanged(object sender, EventArgs e)
{
if (chkCompany.Checked)
{
txtName.Visible = false;
}
else
{
txtName.Visible = true;
}
}
You should use UpdatePanel control to do this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:CheckBox OnCheckedChanged="chkCompany_OnCheckedChanged" AutoPostBack="True" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Keep your code inside update pannel.
you can use javascript to do this,if Update panel does not work.
<!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">
<script type=text/javascript>
function CheckedChanged()
{
if((document.getElementById('chkCompany').checked))
`{`
document.getElementById('txtname').Visible=false;
}
`else`
{
document.getElementById('txtname').Visible=false;
`}`
}
</script>
</head>
<body>
<asp:CheckBox OnCheckedChanged="CheckedChanged" AutoPostBack="false" CssClass="chkCompany" ClientIDMode="Static" ID="chkCompany" runat="server" />
<asp:TextBox ID="txtname" runat="server"/>
</body>
</html>
-----------------------------------------------------------------------------
Here is another solution but for DropDownCheckList.
The same works for CheckBox.
<asp:UpdatePanel runat="server">
<ContentTemplate>
<cwc:DropDownCheckList runat="server" ID="myId" AutoPostBack="True" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="myId" />
</Triggers>
</asp:UpdatePanel>

DropDownList OnSelectedIndexChanged does not fire

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

How to use CalenderExtender in ContentPlaceHolder? [duplicate]

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>

Using Select Command With a DDL

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>

Categories

Resources