DropdownList from webservice reset after postback - c#

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>

Related

change visibility of table tr on dropdown value

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)
{
}
}

Asp.Net Dropdownlist in UpdatePanel Doesn't Postback When Selected Index Changed

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>

ASP.NET was the checkbox was checked in repeater?

I have a repeater with html table inside. In the html table I have a table cell with a check box.
I am trying to get the checked rows from the user after clicking a button but the result is always null.
asp.net markup:
<table id="tbl1" class="table">
<tr>
<th>test 1</th>
<th>test 2</th>
<th>test 3</th>
<th>test 4</th>
<th>Select</th>
</tr>
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<tr id="tr1" runat="server">
<td>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("test1") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl2" runat="server" Text='<%#Eval("test2") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl3" runat="server" Text='<%#Eval("test3") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl4" runat="server" Text='<%#Eval("test4") %>'>' ></asp:Label>
</td>
<td id="td1" runat="server">
<asp:CheckBox ID="Select" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="btnSelect" runat="server" Text="Go" OnClick="btnSelect_Click" />
C# code:
protected void btnSelect_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rpt in rep.Items)
{
CheckBox ckb = (CheckBox)rpt.FindControl("Select");
if (ckb.Checked) // Always Null
{
//
}
else
{
//
}
}
}
The problem is that you have another server control inside the repeater item. The Checkbox is not directly in the repeater item it is in the table row. You can extract the checkbox like this
CheckBox ckb = (CheckBox)rpt.FindControl("tr1").FindControl("Select");
if (ckb.Checked)
...
Of course this is bad since changing the layout will break your code. To remedy this you can write a recursive FindControl but it requires some more work.
Are you rebinding the DataSource for the Repeater on PostBack? This would cause the state of all the controls in the Repeater to be reset.
The problem might be that rpt.FindControl("Select") only searches only in the children of rpt. You can try this:
/// <summary>
/// Iterates throug all children and returns all of Type T.
/// </summary>
public static List<T> FindChildrenOfType<T>(Control control) where T : class
{
List<T> controls = new List<T>();
foreach (Control childControl in control.Controls)
{
if (childControl.Controls.Count > 0)
{
controls.AddRange(FindChildrenOfType<T>(childControl, comp));
}
if (childControl is T)
{
controls.Add(childControl as T);
}
}
return controls;
}
Use it like this:
var checkboxes = FindChildrenOfType<CheckBox>(rpt);
You can try this....
Aspx Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("val") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Button Text="Click" OnClick="Button2_Click" runat="server" />
</form>
</body>
</html>
CS Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("val", typeof(string));
for (int i = 0; i < 10; i++)
dt.Rows.Add("testing" + i.ToString());
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < Repeater1.Items.Count; i++)
{
CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
if (chk.Checked)
{
Rpt += (chk.Text + "<br />");
}
}
Response.Write(Rpt);
}
Refrenced By: http://www.codeproject.com/Questions/534719/GetplusSelectedplusCheckboxesplusinplusASPplusRepe

Getting values from the source page using "PreviousPage"

I’ve tried to post values between 2 web forms using the PreviousPage technique.
I have followed the MSDN article (http://msdn.microsoft.com/en-us/library/ms178139.aspx ) about the PreviousPage and I have referred this (http://www.deliciousdotnet.com/2011/03/getting-values-from-source-page-using.html#.Uw7jCvmSz3Q ) as well. All seem to be in order but I am seeing the following "Unknown member 'Designation' of System.Web.UI.Page" about my public methods in the destination page.
What have I done wrong?? Please help. Thank you.
this is my source pages html code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="adduser.aspx.cs" Inherits="adduser" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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>
<link href="../Styles/main_style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body style="min-height: 600px; background-image: none !important;">
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<div class="lightboxContainer">
<div class="lightboxContainerSection">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table cellpadding="5" width="600px" class="style1" style="font-size: 12px; margin: 5px;
font-weight: bold;">
<tr>
<td>
Find User
</td>
<td>
<telerik:RadTextBox ID="RadTextBox1" runat="server" ValidationGroup="textbox1" OnTextChanged="TextBox1_TextChanged">
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadTextBox1"
ErrorMessage="*" Style="color: #FF0000" ValidationGroup="textbox1"></asp:RequiredFieldValidator>
</td>
<td>
<telerik:RadButton ID="RadButton1" runat="server" Text="Find User" OnClick="RadButton1_Click"
ValidationGroup="textbox1">
</telerik:RadButton>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="4">
<telerik:RadGrid ID="RadGrid_User" runat="server" OnNeedDataSource="RadGrid_User_NeedDataSource"
OnSelectedIndexChanged="RadGrid_User_SelectedIndexChanged" AllowPaging="True"
CellSpacing="0" GridLines="None">
<ClientSettings EnablePostBackOnRowClick="True">
<Selecting AllowRowSelect="True" />
</ClientSettings>
</telerik:RadGrid>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
User name
</td>
<td>
<asp:Label ID="LabelUser" runat="server" Text="Label"></asp:Label>
</td>
<td>
Company
</td>
<td>
<asp:Label ID="LabelCompany" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
Designation
</td>
<td>
<asp:Label ID="LabelDesignation" runat="server" Text="Label"></asp:Label>
</td>
<td>
Department
</td>
<td>
<asp:Label ID="LabelDepartment" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
Mobile
</td>
<td>
<asp:Label ID="LabelMobile" runat="server" Text="Label"></asp:Label>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Label ID="LabelConfirmation" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<telerik:RadButton ID="RadButton2" runat="server" onclick="RadButton2_Click"
Text="Yes">
</telerik:RadButton>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<hr />
</div>
</form>
</body>
</html>
And this is my source page code behind file
using System;
using System.Linq;
using Telerik.Web.UI;
public partial class adduser : System.Web.UI.Page
{
private ActiveDirectory ad = new ActiveDirectory();
private DatabaseConnect db = new DatabaseConnect();
public void RadGrid_User_SelectedIndexChanged(object sender, EventArgs e)
{
GridDataItem selectedItem = (GridDataItem)RadGrid_User.SelectedItems[0];
string user = selectedItem["Email"].Text;
Session["userID"] = user.Split('#')[0];
RadTextBox1.Text = (string)Session["userID"];
string detail = ad.GetUserDetails(RadTextBox1.Text.Trim());
string[] details = detail.Split('/');
LabelUser.Text = details[0];
LabelCompany.Text = details[1];
LabelDepartment.Text = details[3];
LabelDesignation.Text = details[4];
LabelMobile.Text = details[5];
LabelConfirmation.Text = "Do you want to grant "+details[0]+" permission to access the CRI ?";
LabelUser.Visible = true;
LabelCompany.Visible = true;
LabelDepartment.Visible = true;
LabelDesignation.Visible = true;
LabelMobile.Visible = true;
LabelConfirmation.Visible = true;
RadButton2.Visible = true;
}
public void RadGrid_User_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
string username = this.RadTextBox1.Text.Trim();
if (username.Length >= 1)
{
this.RadGrid_User.DataSource = this.ad.GetUserDetails_WildCard(this.RadTextBox1.Text.Trim());
}
else
{
this.RadGrid_User.Visible = false;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.db.IsAllowedAdministrator(this.User.Identity.Name))
{
if (!this.IsPostBack)
{
LabelCompany.Visible = false;
LabelDepartment.Visible = false;
LabelDesignation.Visible = false;
LabelMobile.Visible = false;
LabelUser.Visible = false;
LabelConfirmation.Visible = false;
RadButton2.Visible = false;
}
}
else
{
this.Response.Redirect("~/ZCRI_RestrictedAdmin.aspx");
}
}
protected void RadButton1_Click(object sender, EventArgs e)
{
this.RadGrid_User.Rebind();
this.RadGrid_User.Visible = true;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
LabelCompany.Visible = false;
LabelDepartment.Visible = false;
LabelDesignation.Visible = false;
LabelMobile.Visible = false;
LabelUser.Visible = false;
LabelConfirmation.Visible = false;
RadButton2.Visible = false;
}
public string Designation
{
get
{
return LabelDesignation.Text;
}
}
public string Mobile
{
get
{
return LabelMobile.Text;
}
}
public string Company
{
get
{
return LabelCompany.Text;
}
}
public string Department
{
get
{
return LabelDepartment.Text;
}
}
protected void RadButton2_Click(object sender, EventArgs e)
{
Response.Redirect(#"~/AdminInterfaces\adduserpermission.aspx");
}
}
This is my destination page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="adduserpermission.aspx.cs"
Inherits="adduserpermission" %>
<%# PreviousPageType VirtualPath="~/AdminInterfaces/adduser.aspx" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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></title>
<link href="../Styles/main_style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body style="min-height: 600px; background-image: none !important;">
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<div class="lightboxContainer">
<div class="lightboxContainerSection">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:Label ID="Label2"
runat="server" Text="Label"></asp:Label><asp:Label ID="Label3"
runat="server" Text="Label"></asp:Label><asp:Label ID="Label4"
runat="server" Text="Label"></asp:Label>
</div>
</div>
</form>
</body>
</html>
My destination page code behind file
using System;
using System.Linq;
public partial class adduserpermission : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = PreviousPage.Designation;
Label2.Text = PreviousPage.Department;
Label3.Text = PreviousPage.Company;
Label4.Text = PreviousPage.Mobile;
}
}
Now this will work as i confused labels with your properties.
On previous Page you have a label with name LabelDesignation but here you are using wrong name to access it. try to use this.
You have an issue with this code in source page code behind file
this.Response.Redirect("~/ZCRI_RestrictedAdmin.aspx");
The PreviousPage property returns the page that sent control to this page using Server.Transfer.
If the current page is being rendered as a result of a direct request (not a transfer or cross-post from another page), the PreviousPage property contains null.
else
{
this.Server.Transfer("~/ZCRI_RestrictedAdmin.aspx");
}
using System;
using System.Linq;
public partial class adduserpermission : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
if(Page.PreviousPage.IsCrossPagePostBack == true)
{
Label1.Text = PreviousPage.Designation;
Label2.Text = PreviousPage.Department;
Label3.Text = PreviousPage.Company;
Label4.Text = PreviousPage.Mobile;
}
}
}
}
You need to cast it to right type:
public partial class adduserpermission : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
adduser prevPage = PreviousPage as adduser;
if (prevPage != null)
{
Label1.Text = prevPage.Designation;
Label2.Text = prevPage.Department;
Label3.Text = prevPage.Company;
Label4.Text = prevPage.Mobile;
}
}
}

Repeater control checkbox event not fired

Am having three check box in repeater control. I need the concept like radio button list.When i click the first row first checkbox, other two are checked property is set to false. suppose i check the second one means first and third are unchecked.
My Design code is here :
<asp:Repeater ID="repeaterItems" runat="server"
onitemdatabound="repeaterItems_ItemDataBound" >
<ItemTemplate>
<table id="mytable" cellspacing="0" width="100%" align="center">
<tr>
<td style="width: 18px;">
<asp:Label ID="id" runat="server" Text='<%#Bind("fld_id")%>'></asp:Label>
</td>
<td style="width: 301px;">
<asp:Label ID="Label1" runat="server" Text='<%#Bind("fld_Question")%>'></asp:Label>
</td>
<td style="width: 10px;">
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</td>
<td style="width: 30px;">
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged"/>
</td>
<td style="width: 33px;">
<asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox3_CheckedChanged"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
code is :
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk1 = sender as CheckBox;
RepeaterItem item = chk1.Parent as RepeaterItem;
CheckBox chk2 = item.FindControl("CheckBox2") as CheckBox;
chk2.Checked = false;
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
}
protected void repeaterItems_ItemCreated1(object sender, RepeaterItemEventArgs e)
{
RepeaterItem ri = (RepeaterItem)e.Item;
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
{
CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox1");
chk.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
}
}
CheckBox1_CheckedChanged event not fire. Please help me to fix this error.
I spent more time to solve this issue but i can't..
Just make sure that you are not binding the repeater on post back, and that viewstate is enabled on the repeater.
if(!Page.IsPostBack)
{
Repeater.Datasource = dataset(what ever source);
Repeater.Databind;
}
There is no reason why you should bind to the itemcreated event on the repeater, just to attach to the oncheckedchanged event, as this is pointless.
you can achieve by also JQuery
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('.chkGroup').change(function () {
var parentTr = $(this).parent().parent();
var currentCheckID = $(this).find('input').attr("id");
$(parentTr).find('.chkGroup input').each(function () {
if (currentCheckID != $(this).attr("id")) {
$(this).removeAttr("checked");
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="repeaterItems" runat="server" onitemdatabound="repeaterItems_ItemDataBound">
<ItemTemplate>
<table id="mytable" cellspacing="0" width="100%" align="center">
<tr>
<td style="width: 18px;">
<asp:Label ID="id" runat="server" Text='<%#Bind("ID")%>'></asp:Label>
</td>
<td style="width: 301px;">
<asp:Label ID="Label1" runat="server" Text='<%#Bind("Name")%>'></asp:Label>
</td>
<td style="width: 10px;">
<asp:CheckBox ID="CheckBox4" runat="server" CssClass="chkGroup" />
</td>
<td style="width: 30px;">
<asp:CheckBox ID="CheckBox5" runat="server" CssClass="chkGroup" />
</td>
<td style="width: 33px;">
<asp:CheckBox ID="CheckBox6" runat="server" CssClass="chkGroup" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
You can use the same event CheckBox1_CheckedChanged handler for all the three check-boxes OnCheckedChanged.
By using the sender object you could know which checkbox raised the event and then set the other two check-boxes checked property to false.

Categories