ASP.NET - How to set Selected Item in Dropdownlist in an Usercontrol

Asked By matt cupryk on 10-May-09 06:28 PM

I built the following behind code for a dropdown user control. The problem lies when I want to set the value of the dropdown on web form. That means I am getting the current value for the dropdown.

Here is my behind code:

<code>


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using KORE.SIDWebClient.BLL;

using System.ComponentModel;

namespace KORE.SIDWebClient.UI.Controls

{

public partial class uxLookupFieldTicketStatus : System.Web.UI.UserControl

{

private string _keyField;

private string _valueField;

private string _defaultValue;

public uxLookupFieldTicketStatus()

{

}

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();

this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));

ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);

this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);

}

}

///////////////////////////////////////////////////////////////////////////////////////////

/// <summary>

///

/// </summary>

///

[Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]

public int SelectedIndex

{

get

{

return LookupFieldTicketStatusDropDown.SelectedIndex;

}

}

[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]

public string SelectedText

{

get

{

return LookupFieldTicketStatusDropDown.SelectedItem.Text;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedItem.Text = value;

}

}

[Category("Data"), Description("Selected Valueof the DropDownList"), Browsable(true)]

public string SelectedValue

{

get

{

return LookupFieldTicketStatusDropDown.SelectedValue;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedValue = value;

}

}

/// <summary>

/// ////////////////////////////////////////////////////////////////////////////////////////

/// </summary>

[Category("Data"), Description("The dataset column that will be display field in the dropdown"), Browsable(true)]

public string KeyField

{

get

{

return _keyField;

}

set

{

_keyField = value;

}

}

[Category("Data"), Description("The dataset column that will be value field in the dropdown"), Browsable(true)]

public string ValueField

{

get

{

return _valueField;

}

set

{

_valueField = value;

}

}

public string DefaultValue

{

get

{

return _defaultValue;

}

set

{

_defaultValue = value;

}

}

//////////////////////////////////////////////////////////////////////////////////////////

}

}

and the ui is

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uxLookupFieldTicketStatus.ascx.cs" Inherits="KORE.SIDWebClient.UI.Controls.uxLookupFieldTicketStatus" %>

<asp:DropDownList ID="LookupFieldTicketStatusDropDown" AppendDataBoundItems="true" Width="125px" CssClass="inputBox" runat="server">

<asp:ListItem Value=""></asp:ListItem>

</asp:DropDownList><br />

<asp:RequiredFieldValidator id="CVLookupFieldTicketStatusDropDown"

Text="(Required)"

InitialValue=""

ControlToValidate="LookupFieldTicketStatusDropDown"

Runat="server" />

</code>



uxLookupFieldTicketStatus1.KeyField = TicketDS.Tables[0].Rows[0]["TicketStatus"].ToString(); ??? I am not sure how to make this work.

 

Ravenet Rasaiyah replied to matt cupryk on 10-May-09 07:27 PM
Hi

make you code like this way
this.LookupFieldTicketStatusDropDown.ClearSelecion();
this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue).selected=true;

thank you

Where should I do this? - matt cupryk replied to Ravenet Rasaiyah on 10-May-09 08:23 PM

What I am doing is the following:

DataSet TicketDS = BLL.TicketLogic.GetTicket(ticketID);

if (TicketDS != null && TicketDS.Tables.Count != 0 && TicketDS.Tables[0].Rows.Count > 0)

{

txtActionTaken.Text = TicketDS.Tables[0].Rows[0]["TicketNumber"].ToString();

txtClosedBy.Text = TicketDS.Tables[0].Rows[0]["AgentClosed"].ToString();

txtCreatedBy.Text = TicketDS.Tables[0].Rows[0]["AgentCreated"].ToString();

txtDateAssigned.Text = TicketDS.Tables[0].Rows[0]["DateAssigned"].ToString();

txtDateClosed.Text = TicketDS.Tables[0].Rows[0]["DateClosed"].ToString();

txtDateCreated.Text = TicketDS.Tables[0].Rows[0]["DateCreated"].ToString();

txtDateFollowup.Text = TicketDS.Tables[0].Rows[0]["FollowupBy"].ToString();

txtExternalTicket.Text = TicketDS.Tables[0].Rows[0]["ExternalTicket"].ToString();

txtFollowupNotes.Text = TicketDS.Tables[0].Rows[0]["FollowupNote"].ToString();

txtLastUpdate.Text = TicketDS.Tables[0].Rows[0]["DateLastModified"].ToString();

txtProblem.Text = TicketDS.Tables[0].Rows[0]["ProblemNote"].ToString();

txtShortDescription.Text = TicketDS.Tables[0].Rows[0]["ShortDescription"].ToString();

txtSolution.Text = TicketDS.Tables[0].Rows[0]["SolutionNote"].ToString();

txtSubmitVia.Text = TicketDS.Tables[0].Rows[0]["TicketApplicationType"].ToString();

txtTrackIt.Text = TicketDS.Tables[0].Rows[0]["TrackIt"].ToString();

txtUserID.Text = TicketDS.Tables[0].Rows[0]["UserID"].ToString();

uxLookupFieldTicketStatus1.SetValue = TicketDS.Tables[0].Rows[0]["TicketStatus"].ToString(); ===> my problem

 

<KORE:uxLookupFieldTicketStatus runat="server" ID="uxLookupFieldTicketStatus1"

KeyField="TicketStatus" ValueField="ID" SetValue='<%# Bind("TicketStatus") %>' />

This is my code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using KORE.SIDWebClient.BLL;

using System.ComponentModel;

namespace KORE.SIDWebClient.UI.Controls

{

public partial class uxLookupFieldTicketStatus : System.Web.UI.UserControl

{

private string _keyField;

private string _valueField;

private string _defaultValue;

public uxLookupFieldTicketStatus()

{

}

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();

this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));

ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);

this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);

LookupFieldTicketStatusDropDown.DataBind();

}

}


public string SetValue

{

get

{

return LookupFieldTicketStatusDropDown.SelectedItem.Text;

}

set

{

foreach (ListItem li in LookupFieldTicketStatusDropDown.Items)

{

if (li.Value.Equals(LookupFieldTicketStatusDropDown.Items.FindByText(value)))

{

LookupFieldTicketStatusDropDown.ClearSelection();

li.Selected = true;

}

}

//if (li != null)

// li.Selected = true;

}

}


///////////////////////////////////////////////////////////////////////////////////////////

/// <summary>

///

/// </summary>

///

[Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]

public int SelectedIndex

{

get

{

return LookupFieldTicketStatusDropDown.SelectedIndex;

}

}

[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]

public string SelectedText

{

get

{

return LookupFieldTicketStatusDropDown.SelectedItem.Text;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedItem.Text = value;

}

}

[Category("Data"), Description("Selected Valueof the DropDownList"), Browsable(true)]

public string SelectedValue

{

get

{

return LookupFieldTicketStatusDropDown.SelectedValue;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedValue = value;

}

}

/// <summary>

/// ////////////////////////////////////////////////////////////////////////////////////////

/// </summary>

[Category("Data"), Description("The dataset column that will be display field in the dropdown"), Browsable(true)]

public string KeyField

{

get

{

return _keyField;

}

set

{

_keyField = value;

}

}

[Category("Data"), Description("The dataset column that will be value field in the dropdown"), Browsable(true)]

public string ValueField

{

get

{

return _valueField;

}

set

{

_valueField = value;

}

}

public string DefaultValue

{

get

{

return _defaultValue;

}

set

{

_defaultValue = value;

}

}

//////////////////////////////////////////////////////////////////////////////////////////

}

}

It is failing. - matt cupryk replied to matt cupryk on 10-May-09 09:11 PM

f (!Page.IsPostBack)

{

LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();

this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));

ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);

this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);

this.LookupFieldTicketStatusDropDown.ClearSelection();

this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue).Selected = true;


}

iti is failing. this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue).Selected = true;

re - Web Star replied to matt cupryk on 11-May-09 12:20 AM

u need to just find the dropdownlist from usercontrol as

bjFISearch = (MEDNET.ReportModule.Controls.RMSearch) m_lstFilter.FindControl("FISearch1");

ddllist= (DropDownList)(objFISearch.FindControl("ddllist"));

then u set to vlaue to dropdownlist

ddllist.SelectedIndex = @selectedIndex;

I am lost. - matt cupryk replied to Web Star on 11-May-09 09:27 AM

public partial class uxLookupFieldTicketStatus : System.Web.UI.UserControl

{

private string _keyField;

private string _valueField;

private string _defaultValue;

public uxLookupFieldTicketStatus()

{

}

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();

this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));

ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);

this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);

}

}

public string SetValue

{

get

{

return LookupFieldTicketStatusDropDown.SelectedItem.Text;

}

set

{

foreach (ListItem li in LookupFieldTicketStatusDropDown.Items)

{

if (li.Value.Equals(LookupFieldTicketStatusDropDown.Items.FindByText(value)))

{

LookupFieldTicketStatusDropDown.ClearSelection();

li.Selected = true;

}

}

}

}



///////////////////////////////////////////////////////////////////////////////////////////

/// <summary>

///

/// </summary>

///

[Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]

public int SelectedIndex

{

get

{

return LookupFieldTicketStatusDropDown.SelectedIndex;

}

}

[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]

public string SelectedText

{

get

{

return LookupFieldTicketStatusDropDown.SelectedItem.Text;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedItem.Text = value;

}


}


[Category("Data"), Description("Selected Valueof the DropDownList"), Browsable(true)]

public string SelectedValue

{

get

{

return LookupFieldTicketStatusDropDown.SelectedValue;

}

set

{

this.LookupFieldTicketStatusDropDown.SelectedValue = value;

}

}


/// <summary>

/// ////////////////////////////////////////////////////////////////////////////////////////

/// </summary>

[Category("Data"), Description("The dataset column that will be display field in the dropdown"), Browsable(true)]

public string KeyField

{

get

{

return _keyField;

}

set

{

_keyField = value;

}

}

[Category("Data"), Description("The dataset column that will be value field in the dropdown"), Browsable(true)]

public string ValueField

{

get

{

return _valueField;

}

set

{

_valueField = value;

}

}

public string DefaultValue

{

get

{

return _defaultValue;

}

set

{

_defaultValue = value;

}

}

//////////////////////////////////////////////////////////////////////////////////////////

}

}