Click here to Skip to main content
15,891,253 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Inserting Empty Row in Grid View in ASP Dot net + vb dot net Pin
Abhishek Sur28-Dec-09 20:52
professionalAbhishek Sur28-Dec-09 20:52 
AnswerRe: Inserting Empty Row in Grid View in ASP Dot net + vb dot net Pin
dongavipul28-Dec-09 20:57
dongavipul28-Dec-09 20:57 
AnswerRe: Inserting Empty Row in Grid View in ASP Dot net + vb dot net Pin
satalaj29-Dec-09 1:19
satalaj29-Dec-09 1:19 
QuestionHow the Filter by tag feature on Code Project is done ? Pin
Pradeep_Nair28-Dec-09 14:33
Pradeep_Nair28-Dec-09 14:33 
QuestionImporting .cer into .pfx Pin
siva.lankada28-Dec-09 13:32
siva.lankada28-Dec-09 13:32 
QuestionTreeview indentation not proper for last node Pin
pranilrao28-Dec-09 11:02
pranilrao28-Dec-09 11:02 
AnswerRe: Treeview indentation not proper for last node Pin
pranilrao29-Dec-09 11:47
pranilrao29-Dec-09 11:47 
QuestionHow to write user control ? Pin
paper6728-Dec-09 5:56
paper6728-Dec-09 5:56 
Hi,


I have tried to construct a user control named EventCategory, that holds 2 dropdownlists
to select an event category. Depending on the selection in the first dropdownlist, the second
dropdownlist must be repopulated with items corresponding with the first dropdownlist selection.
Here is my control :

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

<table border="0">
<tr>
<td style="width: 65px"><asp:Label ID="CategoryLabel" runat="server" Text="Category:" AssociatedControlID="CategoryList" /></td>
<td>
<asp:DropDownList ID="CategoryList" runat="server" Width="130px" AutoPostBack="True"
OnSelectedIndexChanged="CategoryList_SelectedIndexChanged" />
</td>
<td style="width: 20px" />
<td style="width: 90px"><asp:Label ID="SubCategoryLabel" runat="server" Text="SubCategory:" AssociatedControlID="SubCategoryList" /></td>
<td>
<asp:DropDownList ID="SubCategoryList" runat="server" Width="130px" />
</td>
</tr>
</table>

When i enter the control in a web page, I can wite something like this :
<!-- The user control gets registered in web.config -->
....
<UC:EventCategory ID="EventCategory" runat="server" SelectText="«Select»" Category="Parties" />

Here I want the "Parties" item to be initial selected in the CategoryList control when the page comes up.

therefore I wrote the following in code behind :

public partial class EventCategory : System.Web.UI.UserControl
{
#region Private data members

private string m_strCategory = string.Empty;
private string m_strSubCategory = string.Empty;

#endregion

#region Public properties

[Category("Appearance"),
DefaultValue(""),
Description("The event category associated with the user control.")]
public string Category
{
get { return ((CategoryList.SelectedItem != null) && (CategoryList.SelectedItem.Value != "0") ? CategoryList.SelectedItem.Text : string.Empty).Trim(); }
set { m_strCategory = value.Trim(); }
}

[Category("Appearance"),
DefaultValue(""),
Description("The event subcategory associated with the user control.")]
public string SubCategory
{
get { return ((SubCategoryList.SelectedItem != null) && (SubCategoryList.SelectedItem.Value != "0") ? SubCategoryList.SelectedItem.Text : string.Empty).Trim(); }
set { m_strSubCategory = value.Trim(); }
}

[Category("Appearance"),
DefaultValue(""),
Description("The selection text shown as first item in the user control's lists.")]
public string SelectText
{
get
{
object obj = ViewState["EventCat_SelectText"];
if (obj == null)
return string.Empty;
else
return (string)obj;
}

set { ViewState["EventCat_SelectText"] = value; }
}
#endregion

#region Public functions

public void Initialize()
{
PopulateCategoryList();

SubCategoryLabel.Enabled = false;
SubCategoryList.Enabled = false;
}

#endregion

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Initialize();
}

protected void CategoryList_SelectedIndexChanged(object sender, EventArgs e)
{
int nCatId = Int32.Parse(CategoryList.SelectedValue);

if (nCatId > 0)
PopulateSubCategoryList(nCatId);
else
{
SubCategoryList.Items.Clear();
SubCategoryLabel.Enabled = false;
SubCategoryList.Enabled = false;
}
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

if (!IsPostBack)
{
if (m_strCategory != string.Empty)
ListControlHelper.SelectListText(CategoryList, m_strCategory);

int nCatId = Int32.Parse(CategoryList.SelectedValue);

if (m_strSubCategory != string.Empty)
{
PopulateSubCategoryListOnEmpty(nCatId);
ListControlHelper.SelectListText(SubCategoryList, m_strSubCategory);
}
}
}

#region Private helpers

private int PopulateCategoryList()
{
EventCatsTableAdapter Adapter = new EventCatsTableAdapter();

CategoryList.DataSource = Adapter.GetAllEventCats();
CategoryList.DataTextField = "CatName";
CategoryList.DataValueField = "CatId";

CategoryList.DataBind();

if (CategoryList.Items.Count > 0)
ListControlHelper.AddListItem(CategoryList, SelectText, "0");
else
throw new InvalidOperationException(string.Format("{0} in user control {1} is empty.", CategoryList.ID, ID));

return CategoryList.Items.Count;
}

private int PopulateSubCategoryListOnEmpty(int nCatId)
{
if (SubCategoryList.Items.Count > 0)
return SubCategoryList.Items.Count;

return PopulateSubCategoryList(nCatId);
}

private int PopulateSubCategoryList(int nCatId)
{
if (!SubCategoryLabel.Enabled)
SubCategoryLabel.Enabled = true;
if (!SubCategoryList.Enabled)
SubCategoryList.Enabled = true;

EventSubCatsTableAdapter Adapter = new EventSubCatsTableAdapter();

SubCategoryList.DataSource = Adapter.GetEventSubCatsByCatId(nCatId);
SubCategoryList.DataTextField = "SubCatName";
SubCategoryList.DataValueField = "SubCatId";

SubCategoryList.DataBind();

if (SubCategoryList.Items.Count > 0)
ListControlHelper.AddListItem(SubCategoryList, SelectText, "0");
else
throw new InvalidOperationException(string.Format("{0} in user control {1} is empty.", SubCategoryList.ID, ID));

return SubCategoryList.Items.Count;
}

#endregion
}

This works fine for a simple web page, but when i place the user control in a InsertItemTemplate of a FormView and I change the
mode of the FormView to FormViewMode.Insert by an ImageButton in the page where the FormView is located I can not initial select
a dropdownlist item through the Category property of the user control. I know it is because the formview is switched to insert mode during
a postback so the IsPostBack block of the OnPreRender override gets skipped. Is there a better way to wtite the user control so that I can
select an initail value in a dropdownlist that works also in a template of a FormView.

TIA
QuestionUsing FaceBook in ASP Pin
Anil Kumar.Arvapalli28-Dec-09 3:36
Anil Kumar.Arvapalli28-Dec-09 3:36 
AnswerRe: Using FaceBook in ASP Pin
sashidhar28-Dec-09 6:13
sashidhar28-Dec-09 6:13 
QuestionDatabase searching Pin
sv_vasja28-Dec-09 1:17
sv_vasja28-Dec-09 1:17 
AnswerRe: Database searching Pin
ProtoBytes28-Dec-09 12:33
ProtoBytes28-Dec-09 12:33 
GeneralRe: Database searching Pin
sv_vasja28-Dec-09 21:19
sv_vasja28-Dec-09 21:19 
GeneralRe: Database searching [Null Values] Pin
ProtoBytes29-Dec-09 3:27
ProtoBytes29-Dec-09 3:27 
GeneralRe: Database searching [Null Values] Pin
sv_vasja29-Dec-09 21:35
sv_vasja29-Dec-09 21:35 
GeneralRe: Database searching [Null Values] Pin
ProtoBytes30-Dec-09 3:30
ProtoBytes30-Dec-09 3:30 
QuestionOnline Update Issue! Pin
Sr...Frank27-Dec-09 23:54
Sr...Frank27-Dec-09 23:54 
AnswerRe: Online Update Issue! Pin
Sr...Frank28-Dec-09 4:53
Sr...Frank28-Dec-09 4:53 
QuestionRow should be bold in SSRS Pin
siddisagar27-Dec-09 20:27
siddisagar27-Dec-09 20:27 
AnswerRe: Row should be bold in SSRS Pin
sashidhar27-Dec-09 21:27
sashidhar27-Dec-09 21:27 
GeneralRe: Row should be bold in SSRS Pin
siddisagar27-Dec-09 23:58
siddisagar27-Dec-09 23:58 
GeneralRe: Row should be bold in SSRS Pin
sashidhar28-Dec-09 0:13
sashidhar28-Dec-09 0:13 
QuestionHow to check cookies are enable or disable in borwser Pin
Samarjeet Singh@india27-Dec-09 20:12
Samarjeet Singh@india27-Dec-09 20:12 
AnswerRe: How to check cookies are enable or disable in borwser Pin
Abhishek Sur27-Dec-09 20:23
professionalAbhishek Sur27-Dec-09 20:23 
GeneralRe: How to check cookies are enable or disable in borwser Pin
Samarjeet Singh@india27-Dec-09 20:57
Samarjeet Singh@india27-Dec-09 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.