DropDownList databinding twice (C#)

Don
Joined
5 Oct 2005
Posts
11,239
Location
Liverpool
Hi All,

I have a DDL which seems to be databinding twice and I can't seem to find out the reason why, the offending code is this...

DropDownList soft = (DropDownList)e.Row.FindControl("DropDownListSoft");

DataSet DropInfo = SQLQueries.AvailSoftware();


soft.AppendDataBoundItems = true;
soft.Items.Insert(0, new ListItem("Select to add software"));
soft.DataSource = DropInfo;
soft.DataTextField = "SoftwareName";
soft.DataValueField = "ID";
soft.DataBind();

When I remove the command AppendDataBoundItems it works fine but with it in it databinds it twice :( The rest of the code is...

public partial class CompSoft : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int PCID = Convert.ToInt32(Request.QueryString["pcid"]);

if (!Page.IsPostBack)
{
DataSet ds = SQLQueries.SoftwareUsedBindData(PCID);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

SoftList.DataSource = ds;
SoftList.DataBind();
}
else
{
SoftList.DataSource = ds;
SoftList.DataBind();
}


}
}

protected void SoftList_RowCommand(object sender, GridViewCommandEventArgs e)
{
int PCID = Convert.ToInt32(Request.QueryString["pcid"]);

if (e.CommandName.ToString() == "expand")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
string value = Convert.ToString(SoftList.DataKeys[row.DataItemIndex].Value);
//string value = SoftList.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Text;
string sScript = "<script language=javascript>window.showModalDialog('softdetails.aspx?pcid=" + value + "','_blank','dialogWidth:290px;dialogHeight:290px;');</script>";
Page.RegisterStartupScript("javascript", sScript);
}

else if (e.CommandName.ToString() == "add")
{
DropDownList Software = (DropDownList)SoftList.FooterRow.FindControl("DropDownListSoft");

string SoftwareName = string.Empty;

if (Software.SelectedItem != null)
SoftwareName = Software.SelectedItem.Value;

int id = Convert.ToInt32(SoftwareName);

SQLQueries.SoftwareAddToComp(PCID, id);

SoftList.DataSource = SQLQueries.SoftwareUsedBindData(PCID);
SoftList.DataBind();

//SQLQueries.GetSoftID(SoftwareName);

}

else if (e.CommandName.ToString() == "remove")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
int id = Convert.ToInt32(SoftList.DataKeys[row.DataItemIndex].Value);

SQLQueries.SoftwareRemoveFromComp(id);

int PC_ID = Convert.ToInt32(Request.QueryString["pcid"]);

DataSet ds = SQLQueries.SoftwareUsedBindData(PCID);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

SoftList.DataSource = ds;
SoftList.DataBind();
}
else
{
SoftList.DataSource = ds;
SoftList.DataBind();
}


}
}

protected void SoftList_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button expand = e.Row.FindControl("expand") as Button;
expand.CommandArgument = e.Row.RowIndex.ToString();
}

else if (e.Row.RowType == DataControlRowType.Footer)
{

DropDownList soft = (DropDownList)e.Row.FindControl("DropDownListSoft");

DataSet DropInfo = SQLQueries.AvailSoftware();


soft.AppendDataBoundItems = true;
soft.Items.Insert(0, new ListItem("Select to add software"));
soft.DataSource = DropInfo;
soft.DataTextField = "SoftwareName";
soft.DataValueField = "ID";
soft.DataBind();

}
//Response.Write("["++"]");
}

protected void DropDownListSoft_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DDL_Soft = (DropDownList)SoftList.FooterRow.FindControl("DropDownListSoft");
Label MediaNo = (Label)SoftList.FooterRow.FindControl("MedNo");
Label Maker2 = (Label)SoftList.FooterRow.FindControl("Maker");
Label Version = (Label)SoftList.FooterRow.FindControl("Version");

int ID = Convert.ToInt32(DDL_Soft.SelectedItem.Value);
DataSet PopSoft = SQLQueries.PopulateSoft(ID);

if (null != PopSoft && 0 < PopSoft.Tables[0].Rows.Count)
{
DataRow Soft = PopSoft.Tables[0].Rows[0];

MediaNo.Text = Soft["MediaNo"].ToString();
Maker2.Text = Soft["Manufacturer"].ToString();
Version.Text = Soft["Verson"].ToString();
}

}
}

I really appriciate any help I can get as I have been working on this problem for ages :(

Stelly
 
I'm not too good at reading C# only knowing VB really but I'd start by putting breakpoints on the handful of DataBind() calls you've used and see which ones are fired in which order.

If you get really stuck, create a dummy class that implements the DropDownList control and overloads DataBind() and view the call stack each time it gets called.

Could loadviewstate be calling it to refill before running through page_load?

Sorry if that is no help at all.
 
Back
Top Bottom