Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling viewstate in a control

Status
Not open for further replies.

ggeorgiou01

Programmer
Apr 11, 2005
60
GB
Hi all,

I have a default.aspx file which contains a user control called contractsearch.ascx. This control contains a dropdownlist with witch, i would like to do an action on the SelectedIndexChanged event.

But when i select an option in my dropdownlist the contents dissapear from the dropdownlist and the event never seems to get run.

Does anybody have an answers as to why this is happening. I have posted my code below. (note: I have enabled ViewState in both my aspx and my ascx files.


DEFAULT.aspx
______________________________________________________

<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>HCC Contract Register</title>
<link rel="stylesheet" href="Main.css" type="text/css">

</head>
<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0
<form id="form1" runat="server">
<div class="pageWrapper">

<div class="contentWrapper">

<div class="columnCenter" id="centerPane" runat="server">

</div>
</div>
</div>
</form>
</body>
</html>


CONTRACTSEARCH.ascx
__________________________________________________________

<%@ Control Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="ContractSearch.ascx.cs" Inherits="Controls_ContractSearch" %>

<asp:dropdownlist id="TypeDdl" runat="server" DataValueField="ID" DataTextField="Description" AutoPostBack="True" Width="450px" onselectedindexchanged="TypeDdl_SelectedIndexChanged" Font-Size="0.75em"></asp:dropdownlist>

<asp:dropdownlist id="CategoryDdl" runat="server" DataValueField="ID" DataTextField="Description" AutoPostBack="True" Width="450px" onselectedindexchanged="CategoryDdl_SelectedIndexChanged" Font-Size="0.75em"></asp:dropdownlist>

<asp:dropdownlist id="SubCategoryDdl" runat="server" DataValueField="ID" DataTextField="Description" AutoPostBack="True" Width="450px" Font-Size="0.75em"></asp:dropdownlist>
 
>>But when i select an option in my dropdownlist the contents dissapear from the dropdownlist and the event never seems to get run.


did you try setting break points in the ASXC control? is it even entering the data population event of the control???

Known is handfull, Unknown is worldfull
 
vbkris thank you for your reply.

yes the dropdownlist is being populated, and i am doing this on load of the control.

(see my code below for the ascx file)
_________________________________________________________

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Hbs.ContractRegister.ContractRegisterBLL;

public partial class Controls_ContractSearch : System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{
BindPage();
}

#region Private methods

private void BindPage()
{
if (!IsPostBack)
{
BindTypes();
CategoryDdl.Enabled = false;
SubCategoryDdl.Enabled = false;
}
}

private void BindTypes()
{
// Type data table
DataTable TypeDt = new DataTable();

// Get Types
TypeBLL Types = new TypeBLL();
TypeDt = Types.GetTypes();

TypeDdl.DataSource = TypeDt;
TypeDdl.DataBind();
TypeDdl.Items.Insert(0, new ListItem("[Select Type]", "0"));
}

private void BindCategorys(int ID)
{
// Category data table
DataTable CategoryDt = new DataTable();

// Get Categorys
CategoryBLL Categorys = new CategoryBLL();
CategoryDt = Categorys.GetCategorys(ID);

CategoryDdl.DataSource = CategoryDt;
CategoryDdl.DataBind();
CategoryDdl.Items.Insert(0, new ListItem("[Select Category]", "0"));
}

private void BindSubCategorys(int ID)
{
// Category data table
DataTable SubCategoryDt = new DataTable();

// Get Categorys
SubCategoryBLL SubCategorys = new SubCategoryBLL();
SubCategoryDt = SubCategorys.GetSubCategorys(ID);

SubCategoryDdl.DataSource = SubCategoryDt;
SubCategoryDdl.DataBind();
SubCategoryDdl.Items.Insert(0, new ListItem("[Select Sub-Category]", "0"));
}

#endregion

#region Event commands

protected void TypeDdl_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(CategoryDdl.SelectedItem.Value) != 0)
{
SubCategoryDdl.SelectedIndex = 0;
}

BindCategorys(Convert.ToInt32(TypeDdl.SelectedValue));
BindSubCategorys(Convert.ToInt32(CategoryDdl.SelectedValue));

SubCategoryDdl.Enabled = true;

if (CategoryDdl.Items.Count <= 1)
{
CategoryDdl.Enabled = false;
}
else
{
CategoryDdl.Enabled = true;
}

if (SubCategoryDdl.Items.Count <= 1)
{
SubCategoryDdl.Enabled = false;
}
else
{
SubCategoryDdl.Enabled = true;
}
}

protected void CategoryDdl_SelectedIndexChanged(object sender, EventArgs e)
{
BindSubCategorys(Convert.ToInt32(CategoryDdl.SelectedValue));

if (SubCategoryDdl.Items.Count <= 1)
{
SubCategoryDdl.Enabled = false;
SubCategoryDdl.SelectedItem.Text = "n/a";
}
else
{
SubCategoryDdl.Enabled = true;
}
}

#endregion

}
 
Could it be that you are disabling them:
Code:
   if (!IsPostBack)
        {
            BindTypes();
            [b]CategoryDdl.Enabled = false;[/b]
            [b]SubCategoryDdl.Enabled = false;[/b]
        }

Try adding an ELSE to the above if and set Enabled = True it it is a postback.
 
hey jbenson,

thats not it. tried taking that away, and that failed to work also.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top