I have a problem with a custom control I have built in 'listing.ascx'. The control is basically a dropdownlist. I am building a search form which has normal and my own controls in it. Before doing a postback for example I will select an item from the normal
control dropdownlist and then from my own custom control. Upon postback the normal control has persisted correctly but the custom control does not and always selects the first item as default.
I have included the search page code not sure if it is important to see the control's code or not but have added it anyway.
Also on another note please check out the bolded text in the controls code behind page I'm not sure how to get the posted value back when used as shown below. I'm not going to be using it like this, however, if I post it to another page I will.
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace NETJOBS.Search
{
public class Search : System.Web.UI.Page
{
protected Listing ddlListing1;
protected System.Web.UI.WebControls.DropDownList ddlNormalControl;
protected System.Web.UI.WebControls.Button searchButton;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new System.EventHandler(this.Page_PreRender);
}
#endregion
namespace NETJOBS
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NETJOBS.BusinessLogicLayer;
public class Listing : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList ddlMainListing;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private ITProjectStatusesCollection _DataSource;
public ITProjectStatusesCollection DataSource
{
get { return _DataSource; }
set { _DataSource = value; }
}
if (Page.IsPostBack)
{ //If it is a postback how can I get the value of the selected list item?
//for example:
//Response.Write(Request.Form[ddlMainListing.ClientID]); }
}
Upon postback the normal control has persisted correctly but the custom control does not and always selects the first item as default.
One of reasons is that you are refilling the DDL items in every PostBack which will make the first item active by default. Also make sure EnableViewState=True
Have done what you said and everything is working perfectly, thanks appreciate your help smiling4ever! I guess I was thinking of the classic ASP way of doing things. This is now resolved.
Have done what you said and everything is working perfectly, thanks appreciate your help smiling4ever! I guess I was thinking of the classic ASP way of doing things. This is now resolved.
You are welcome
Classical ASP is totaly different from ASP.NET. You should read more about that mate :)
Haydes
Member
30 Points
6 Posts
Help- custom dropdownlist control does not persist on postback
Dec 05, 2005 10:52 AM|LINK
Hi all,
I have a problem with a custom control I have built in 'listing.ascx'. The control is basically a dropdownlist. I am building a search form which has normal and my own controls in it. Before doing a postback for example I will select an item from the normal control dropdownlist and then from my own custom control. Upon postback the normal control has persisted correctly but the custom control does not and always selects the first item as default.
I have included the search page code not sure if it is important to see the control's code or not but have added it anyway.
Also on another note please check out the bolded text in the controls code behind page I'm not sure how to get the posted value back when used as shown below. I'm not going to be using it like this, however, if I post it to another page I will.
Here is the search page search.aspx
<%@ Register TagPrefix="it" TagName="Listing" Src="~/UserControls/Listing.ascx" %>
<form id="frmProjectSearchCriteria" runat="server" method="post">
<table>
<tr>
<td colspan="3" class="heading1">
Search Criteria
</td>
</tr>
<tr>
<td>
<table style="WIDTH:100%">
<tr>
<td nowrap>
<u>J</u>ob Reference
</td>
<td>
</td>
<td>
<asp:TextBox id="txtJobReference" tabindex="2" accesskey="P" maxlength="30" runat="Server" Width="200" />
</td>
</tr>
<tr>
<td nowrap>
<u>T</u>ask Status Trial
</td>
<td>
</td>
<td>
<it:Listing id="ddlListing1" Runat="Server" />
</td>
</tr>
<tr>
<td nowrap>
Normal Control
</td>
<td>
</td>
<td>
<asp:DropDownList id="ddlNormalControl" style="width:200px;" Runat="Server">
<asp:ListItem Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Button class="smallbtn" Text="Search" runat="server" id="searchButton" />
</td>
</tr>
</table>
</form>
The following is the code behind search.aspx.cs
using
System;using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace NETJOBS.Search
{
public class Search : System.Web.UI.Page
{
protected Listing ddlListing1;
protected System.Web.UI.WebControls.DropDownList ddlNormalControl;
protected System.Web.UI.WebControls.Button searchButton;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new System.EventHandler(this.Page_PreRender);
}
#endregion
//*********************************************************************
//
// search.aspx
//
//*********************************************************************
void Page_Load(Object s, EventArgs e)
{
}
void Page_PreRender(object sender, System.EventArgs e)
{
if (!User.IsInRole("SEARCH"))
{
Response.Redirect("../login.aspx");
}
}
}
}
Here is my custom control 'listing.aspx'
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Listing.ascx.cs" Inherits="NETJOBS.Listing" %>
<asp:DropDownList ID="ddlMainListing" style="width:200px;" Runat="Server"/>
Here is the code behind 'listing.aspx.cs
namespace NETJOBS
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NETJOBS.BusinessLogicLayer;
public class Listing : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList ddlMainListing;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private ITProjectStatusesCollection _DataSource;
public ITProjectStatusesCollection DataSource
{
get { return _DataSource; }
set { _DataSource = value; }
}
private void Page_Load(object sender, System.EventArgs e)
{
_DataSource = ITProjectStatuses.projectStatusesGetAll();
BindOptions();
if (Page.IsPostBack)
{
//If it is a postback how can I get the value of the selected list item?
//for example:
//Response.Write(Request.Form[ddlMainListing.ClientID]);
}
}
void BindOptions()
{
ddlMainListing.Items.Clear();
ddlMainListing.DataSource = _DataSource;
ddlMainListing.DataTextField = "Status_Name";
ddlMainListing.DataValueField = "Status_Id";
ddlMainListing.DataBind();
}
}
}
smiling4ever
All-Star
15825 Points
3123 Posts
Re: Help- custom dropdownlist control does not persist on postback
Dec 05, 2005 01:40 PM|LINK
One of reasons is that you are refilling the DDL items in every PostBack which will make the first item active by default. Also make sure EnableViewState=True
To know the selected value upon postback
DDL.SelectedItem.Text to get the Displayed text
DDL.SelectedItem.Value to get the Displayed Value
HTH
Regards
Haydes
Member
30 Points
6 Posts
Re: Help- custom dropdownlist control does not persist on postback
Dec 05, 2005 07:29 PM|LINK
Have done what you said and everything is working perfectly, thanks appreciate your help smiling4ever! I guess I was thinking of the classic ASP way of doing things. This is now resolved.
smiling4ever
All-Star
15825 Points
3123 Posts
Re: Help- custom dropdownlist control does not persist on postback
Dec 05, 2005 11:21 PM|LINK
You are welcome
Classical ASP is totaly different from ASP.NET. You should read more about that mate :)
regards