I am working in an asp.net web application using c#. Following is design view
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication18.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
<div>
<table id="tblid" runat="server">
<tr id="tr11" runat="server">
<td>
<asp:Label ID="lblselect" runat="server" Text="Select List Type">
</asp:Label>
</td>
<td>
<asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddllistType" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="ddllistType_SelectedIndexChanged">
<asp:ListItem Text="select" Value="0"></asp:ListItem>
<asp:ListItem Text="Agent" Value="1"></asp:ListItem>
<asp:ListItem Text="Customer" Value="2"></asp:ListItem>
<asp:ListItem Text="Branch" Value="3"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr id="trlic" runat="server" style="display:none;">
<td>
<asp:Label ID="lblvalue" runat="server" Text="Item Types"></asp:Label>
</td>
<td>
<asp:CheckBoxList ID="cblItemType" runat="server">
<asp:ListItem Text="Premium" Value="1"></asp:ListItem>
<asp:ListItem Text="Budget" Value="2"></asp:ListItem>
<asp:ListItem Text="Normal" Value="3"></asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Below is my code behind
protected void ddllistType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ddllistType.SelectedValue.ToString().Equals("1"))
{
trlic.Style.Add("display", "none");
}
else
{
trlic.Style.Add("display", "block");
}
}
catch (Exception ex)
{
}
}
when user will select agent from ddllistType then trlist should get hide otherwise it should gets displayed. But the above code is not working. Please help me here.
You are using Style.Add("display", "none") to hide and show the element, but I think you should use Visible property instead.
protected void ddllistType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ddllistType.SelectedValue.ToString().Equals("1"))
{
trlic.Visible = false;
}
else
{
trlic.Visible = true;
}
sc1.Update();
}
catch (Exception ex)
{
}
}
Related
I have a Submit button and on submit button click I want to check if my dropdown's selected index has changed or not. If yes, it should call a function.
I don't know how to do it in asp.net C#, need help.
<asp:DropDownList ID="ddlIncidentStatus" runat="server"
Enabled="false"
Display="Dynamic"
AppendDataBoundItems="True"
AutoPostBack="true"
CssClass="form-control">
<asp:ListItem Value="0">- Select Incident Status -</asp:ListItem>
</asp:DropDownList>
protected void btnSave_Click(object sender, System.EventArgs e)
{
if ((SaveToMemory() > 0))
{
//here i want to check if ddlIncidentStatus has change the value or not
Response.Redirect(("IncidentReport_New.aspx?OHSIncidentID=" +
Encryption.EncryptParameter(_incident.OHSIncidentID.ToString())));
}
}
}
Example.aspx
<%# Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="DropDownListExample._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<p>Select a City of Your Choice</p>
<div>
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem Value="">Please Select</asp:ListItem>
<asp:ListItem>New Delhi </asp:ListItem>
<asp:ListItem>Greater Noida</asp:ListItem>
<asp:ListItem>NewYork</asp:ListItem>
<asp:ListItem>Paris</asp:ListItem>
<asp:ListItem>London</asp:ListItem>
</asp:DropDownList>
</div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
</form>
</body>
</html>
Example.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "")
{
Label1.Text = "Please Select a City";
}
else
Label1.Text = "Your Choice is: " + DropDownList1.SelectedValue;
}
I have few textboxes in an update panel. On button click the values of textboxes is inserted into the database. I want the values to get cleared once the data has been inserted into the database.
PS: I tried to clear the values using a function after calling it once the data has been inserted. It doesn't work.
ASP
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="CalenderTest.aspx.cs" Inherits="DynamicCalender.CalenderTest" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 363px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1"runat="server"></asp:ToolkitScriptManager>
<div>
<div>
<asp:Label ID="lbl_year" runat="server" Text="Year"></asp:Label>
<asp:DropDownList ID="ddl_year" runat="server"></asp:DropDownList>
<asp:Label ID="lbl_train" runat="server" Text="Training"></asp:Label>
<asp:DropDownList ID="dd1_training" runat="server"></asp:DropDownList>
<asp:Button ID="btnsave" runat="server" Text="Create Batches" OnClick="btnsave_Click"/>
<br />
<br />
<asp:UpdatePanel ID="pnl1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="ss" runat="server" Text="session"></asp:Label>
<asp:Label runat="server" Text="Venue"></asp:Label>
<asp:TextBox ID="venue" runat="server" CausesValidation="false"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Time"></asp:Label>
<asp:DropDownList ID="ddltime" runat="server" ></asp:DropDownList>
<asp:Label ID="Label2" runat="server" Text="Faculty"></asp:Label>
<asp:TextBox ID="faculty" runat="server" CausesValidation="false"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Module Details"></asp:Label>
<asp:TextBox TextMode="MultiLine" id="module_det" runat="server" CausesValidation="false"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
<asp:Button id="Button2" runat="server" Text="Next" OnClick="Button2_Click"/>
<asp:TextBox ID="caldt" runat="server" CausesValidation="false"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="caldt"></asp:CalendarExtender>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
<asp:HiddenField ID="hd01" runat="server" />
<asp:HiddenField ID="hd02" runat="server" />
</div>
</div>
C#
protected void Button1_Click(object sender, EventArgs e)
{
sess_datetime = caldt.Text + " " + ddltime.SelectedItem.Value.ToString();
str1 = "Insert into sessDetail (tid,bid,sid,dt,faculty,venue,status) values(#tid,#bid,#sid,#dt,#faculty,#venue,#status)";
cmd = new SqlCommand(str1, con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#tid", dd1_training.SelectedItem.Value);
cmd.Parameters.AddWithValue("#bid",Session["bid"]);
cmd.Parameters.AddWithValue("#sid",Session["sid"]);
cmd.Parameters.AddWithValue("#dt", sess_datetime);
cmd.Parameters.AddWithValue("#faculty", faculty.Text);
cmd.Parameters.AddWithValue("#venue", venue.Text);
cmd.Parameters.AddWithValue("#status", "0");
cmd.ExecuteNonQuery();
con.Close();
clear();
}
protected void clear
{
try
{
foreach (var pnl in pnl1.Controls)
{
var tb = pnl as TextBox;
if (tb != null)
{
tb.Text = "";
}
}
ddltime.ClearSelection();
}
catch(Exception x)
{
Response.Write(x.Message);
}
}
Wrap the Controls inside the UpdatePanel with a PlaceHolder and use that for the foreach loop.
<asp:UpdatePanel ID="pnl1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Code behind
foreach (var pnl in PlaceHolder1.Controls)
{
//clear controls
}
in my project i have placed two dropdownlist in an updatepanel. but event of dropdownlist selectedindexchanged not working.
it's my design code in aspx file :
<%# Page Title="" Language="C#" MasterPageFile="~/Page.master" AutoEventWireup="true" CodeFile="NewJob.aspx.cs" Inherits="NewJob" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="panel-body">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpCategory" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<table>
<tr class="centered">
<td class="table-field" colspan="4">
<br />
</td>
</tr>
<tr>
<td class="table-title">
Category :
</td>
<td class="table-field">
<asp:DropDownList ID="drpCategory" CssClass="piran-control" runat="server" Width="300px" AutoPostBack="True" ViewStateMode="Enabled" EnableViewState="True" OnSelectedIndexChanged="drpCategory_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="padding-right:40px;" >
SubCategory :
</td>
<td class="table-field" colspan="4">
<asp:DropDownList ID="drpSubCategory" CssClass="piran-control" runat="server" Width="300px" ViewStateMode="Enabled" EnableViewState="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnInsert" runat="server" Text=" Insert " class="submit_btn btn btn-success" OnClick="Button1_Click" ValidationGroup="Validation" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
And this is my code behind :
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
cData.loadCategory(myDs);
drpCategory.DataValueField = "ID";
drpCategory.DataTextField = "Name";
drpCategory.DataSource = myDs.tblCategory;
drpCategory.DataBind();
}
}
protected void drpCategory_SelectedIndexChanged(object sender, EventArgs e) {
userData.catID = Int32.Parse(drpCategory.SelectedValue);
cData.loadSubCategory(myDs);
drpCategory.DataValueField = "ID";
drpCategory.DataTextField = "Name";
drpCategory.DataSource = myDs.tblSubCategory;
drpCategory.DataBind();
}
i want when chande item or index of drpCategory postback in ajax and show sub category in drpSubCategory.
what's your solution ???
don't forget to set your AsyncPostbackTrigger within your update panel
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpCategory" EventName="SelectedIndexChanged" />
</Triggers>
My list is populated from a webservice call.
If i populate the list manually line by line its ok. I can select the item and return the selectem item with no problem e.g.
List<ListItem> oList = new List<ListItem>();
oList.Add(new ListItem("User", "0"));
oList.Add(new ListItem("Manager", "1"));
cboUsers.DataSource = oList
Thats not practical as the list I want to bind to is Dynamic, e.g.
cboUsers.DataSource = MyWebService.GetUsers() // returns List<ListItem>
No matter what I do in code I cannot get the the selected item from the list and the list ALWAYS resets itself.
Both items of code are enclosed within
if (!IsPostBack)
But when the list is bound to a web service no matter what I do (ViewState, Session anything) the list is ALWAYS reset after postback and I can NEVER get the selected item correctly.
I have tried every combination of properties on pages, controls in code and in the mark up and nothing works. I have looked at loads of articles on here and other websites and none of their examples work.
Any help would be appreciated.
[EDIT]
The full code is below.
(cboAccess, manually filled work just fine as expected)
(cboDept will fill and display but after that I can get no selection from it)
You make a selection from the dropdown (cboDept) then click the Add button (cmdAdd) which adds the text selection from the list to my database using my web service. The selection is ALWAYS shown to be the first item no matter what I select.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
List<ListItem> oList = new List<ListItem>();
oList.Add(new ListItem("Normal User", "0"));
oList.Add(new ListItem("Manager", "10"));
cboAccess.DataTextField = "Text";
cboAccess.DataValueField = "Value";
cboAccess.DataSource = oList;
cboAccess.DataBind();
cboDept.DataSource = oServices.GetTeamGroups().ToList();
cboDept.DataValueField = "Value";
cboDept.DataTextField = "Text";
cboDept.DataBind();
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
string sDept = ((ListItem)cboDept.SelectedItem).Text;
oServices.AddNewUser(sDept);
}
}
[EDIT2 - HTML]
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="adduser.ascx.cs" Inherits="AttendanceWeb2.adduser" %>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
<p>
</p>
<asp:scriptmanager runat="server" id="scm1">
</asp:scriptmanager>
<table class="style1">
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department</td>
<td>
<asp:updatepanel runat="server">
<ContentTemplate>
<asp:DropDownList ID="cboDept" runat="server">
</asp:DropDownList>
</ContentTemplate>
<triggers>
<asp:AsyncPostBackTrigger ControlID="cmdAdd" />
</triggers>
</asp:updatepanel>
</td>
</tr>
<tr>
<td>
Employee ID</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Access Level</td>
<td>
<asp:dropdownlist runat="server" id="cboAccess"></asp:dropdownlist>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="cmdAdd" runat="server" onclick="cmdAdd_Click" Text="Add"
Width="100px" />
<asp:Button ID="cmdDelete" runat="server" onclick="cmdDelete_Click" Text="Delete"
Width="100px" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
[EDIT 3] - Simplified Version (Still does not work)
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="AttendanceWeb2._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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="cboDept" runat="server"></asp:DropDownList>
<asp:Button ID="cmdAdd" runat="server" Text="Add" Width="100px"
onclick="cmdAdd_Click" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
cboDept.DataSource = oServices.GetTeamGroups().ToList();
cboDept.DataValueField = "Value";
cboDept.DataTextField = "Text";
cboDept.DataBind();
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
ListItem oItem = cboDept.SelectedItem;
string sText = oItem.Text;
string sValue = oItem.Value;
}
Make sure that you have EnableViewState set to True for your cboDept control.
EDIT
Put your table inside a form. Example:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button type="submit" ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
i use from ajax tabcontainer .and i want to when value of hiddenfield is not 1(value of hiddenfield change when i click in gridview,i dont have problem with set value for hiddenfield) and user click in email tab it alerts and stays in first tab.but in my code it alert and changes tab.i want to stay in cuurent tab.
i write this code but it dosent work.
please help me.
<%# Page Language="C#" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
<script type="text/javascript">
function SetActiveTab() {
var hfd = $get('<%=HiddenField1.ClientID%>');
if (hfd.value != "1") {
alert("hitttttttt");
var ctrl = $find('TabContainer1');
ctrl.set_activeTab(ctrl.get_tabs()[0]);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ajax Control - Tabs </title>
</head>
<body>
<form id="form1" runat="server">
<b>Tabs Demonstration</b> <br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:HiddenField ID="HiddenField1" runat="server" />
<br />
<asp:TabContainer runat="server" ID="TabContainer1" Height="138px" ActiveTabIndex="0"
Width="402px">
<asp:TabPanel runat="server" ID="Panel1" HeaderText="Address" >
<ContentTemplate>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<table>
<tr><td>First Name:</td><td><asp:TextBox ID="txtName" runat="server" /></td></tr>
<tr><td>Address:</td><td><asp:TextBox ID="txtAddress" runat="server" /></td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel runat="server" ID="Panel3" HeaderText="Email" OnClientClick="SetActiveTab" >
<ContentTemplate>
Email: <asp:TextBox ID="txtEmail" runat="server" />
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel runat="server" ID="Panel2" HeaderText="Login Details" >
<ContentTemplate>
<table>
<tr> <td>User Name:</td><td><asp:TextBox ID="txtUser" runat="server" /></td></tr>
<tr> <td>Password:</td><td><asp:TextBox ID="txtPass" runat="server" /></td></tr>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</form>
The one solution I found is to remove OnClientClick handler and use the javascript below:
function pageLoad() {
var tabContainer = $find('<%= TabContainer1.ClientID %>');
var hfd = $get('<%= HiddenField1.ClientID %>');
var oldSetActiveTab = Function.createDelegate(tabContainer, tabContainer.set_activeTab);
tabContainer.set_activeTab = function (value) {
if (value.get_id() == '<%= Panel3.ClientID %>' && hfd.value != "-1") {
alert("oops");
}
else {
oldSetActiveTab(value);
}
};
}