Issue with dropdownlist in a user control

G

Gunjan Garg

The issue here is -

When the postback event happens the selected index is always returing the
first value.
Appreciate your help in advance.

Thanks

Here is the code
----------------------------------------------------------------------------
--------------------------------------
<%@ Control Language="C#" %>
<%@ import Namespace="goondemand.framework" %>
<%@ import Namespace="goondemand.framework.types" %>
<%@ import Namespace="goondemand.ui.util" %>
<script runat="server">

// public access to class data elements
private String fieldlabel = "";
private String fieldvalue = "";
private String fieldname = "";
private int fieldlength = 0;
private int spacesfromleft = GlobalConfig.SPACESFROMLEFT;
private bool hidelabel = false;
private String align = "left";
private bool colonenabled = false;
private bool required = false;
private String classname = "lookup-default";
private ListData listData;

private String label = "";


public String FIELDNAME{
get{
return fieldname;
}

set{
fieldname=value;
}
}

public String FIELDLABEL{
get{
return fieldlabel;
}

set{
fieldlabel=value;
}
}


public String FIELDVALUE{
get{
return fieldvalue;
}

set{
fieldvalue=value;
}
}


public int FIELDLENGTH{
get{
return fieldlength;
}

set{
fieldlength=value;
}
}



public int SPACESFROMLEFT{
get{
return spacesfromleft;
}

set{
spacesfromleft=value;
}
}


public bool HIDELABEL{
get{
return hidelabel;
}

set{
hidelabel=value;
}
}

public bool COLONENABLED{
get{
return colonenabled;
}

set{
colonenabled=value;
}
}

public String ALIGN{
get{
return align;
}

set{
align=value;
}
}

public bool REQUIRED{
get{
return required;
}

set{
required=value;
}
}

public String CLASSNAME{
get{
return classname;
}

set{
classname=value;
}
}

public ListData LISTDATA{
get{
return listData;
}

set{
listData=value;
}
}

void Page_Load(Object obj, EventArgs args){


localDropDown.ID = fieldname;

if (hidelabel){
label = "";
}
else {
if (required) {
label = GlobalConfig.REQUIRED + "&nbsp;" + fieldlabel;
}
else {
label = fieldlabel;
}

if (colonenabled){
label = label + ":";
}
}

int selectIndex=1;

if (!Page.IsPostBack) {

if (listData != null ){

String field1 = "";
String field2 = "";
ArrayList fields = null;
int i = listData.getCount();

if (i >= 1){
fields =
((RowData)listData.getRowData(1)).getOrderedFields();
field1 = (String)fields[0];
field2 = (String)fields[1];
}

bool firstRow = false;
int index=0;
ListItemCollection listItem = new ListItemCollection();

for(index = 1;index < i;index++){

RowData singleRow =
(RowData)listData.getRowData(index);

String tempValue1 =
(String)singleRow.getFieldValue(field1);
String tempValue2 =
(String)singleRow.getFieldValue(field2);
if (null == tempValue1){
tempValue1 = "";
}
ListItem oneItem = new
ListItem(tempValue2,tempValue1);
listItem.Add(oneItem);

if ( tempValue1.Equals(FIELDVALUE) ){
selectIndex = index;
}
}

localDropDown.DataSource = listItem;
localDropDown.DataTextField = "text";
localDropDown.DataValueField = "value";
localDropDown.DataBind();
} // end of null != data

} // end of postback

if (Page.IsPostBack){
Log.Instance.write(Log.INFO,this.ToString(),"The selected index
in
the drop down is "+ localDropDown.SelectedValue);
}
} // end of method page_load


public String getStartTag() {

String tag = "<td width=\"100%\" class=\""+ classname + "\">";
tag = tag + System.Environment.NewLine + "<table border=\"0\"
width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
tag = tag + System.Environment.NewLine + "<tr>";
tag = tag + System.Environment.NewLine + "<td class=\"" +
classname
+ "\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">" +
Misc.Instance.getSpaces(spacesfromleft) + label + "</td>";
tag = tag + System.Environment.NewLine + "</tr>";
tag = tag + System.Environment.NewLine + "<tr>";
tag = tag + System.Environment.NewLine + "<td class=\"" +
classname
+ "\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">" +
Misc.Instance.getSpaces(spacesfromleft);
return tag;
}

public String getEndTag() {
String tag = "</td>";
tag = tag + System.Environment.NewLine + "</tr>";
tag = tag + System.Environment.NewLine + "</table>";
tag = tag + System.Environment.NewLine + "</td>";
return tag;
}

</script>
<%=getStartTag()%>
<asp:DropDownList id="localDropDown" runat="server"></asp:DropDownList>
<%=getEndTag()%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top