Problem with DDL and Gridview

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

I have a problem... I have a DDL which I want to populate a GridView
dependant on what is chosen in the DDL... so far I have done below... but when I try to go to edit it postbacks the page and the information vanishes... any ideas what I should do??

protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlSelect.SelectedItem.Value == "1")
{
string[] datakey = { "PC_ID" };
gdvSelect.DataKeyNames = datakey;
gdvSelect.DataSourceID = "SqlSource";
gdvSelect.AutoGenerateEditButton = true;
gdvSelect.AutoGenerateColumns = true;
gdvSelect.DataBind();

SqlSource.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
SqlSource.SelectCommand = "SELECT * FROM Computer";
SqlSource.DeleteCommand = "DELETE FROM Computer WHERE PC_ID = @PC_ID";
SqlSource.DeleteParameters.Add(new Parameter("PC_ID", TypeCode.Int32));

SqlSource.InsertCommand = "INSERT INTO Computer (PC_ID, PcModel, CCCSR, EPSR, PoNo, ShipDate, OnSite, UserID, OS, UserShared, Available) VALUES (@PC_ID, @PcModel, @CCCSR, @EPSR, @PoNo, @ShipDate, @OnSite, @UserID, @OS, @UserShared, @available)";
SqlSource.InsertParameters.Add(new Parameter("PC_ID", TypeCode.Int32));
SqlSource.InsertParameters.Add(new Parameter("PcModel", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("CCCSR", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("EPSR", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("PoNo", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("UserID", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("OS", TypeCode.String));
SqlSource.InsertParameters.Add(new Parameter("UserShared", TypeCode.Int32));
SqlSource.InsertParameters.Add(new Parameter("Available", TypeCode.Int32));

SqlSource.UpdateParameters.Add(new Parameter("PC_ID", TypeCode.Int32));
SqlSource.UpdateParameters.Add(new Parameter("PcModel", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("CCCSR", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("EPSR", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("PoNo", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("UserID", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("OS", TypeCode.String));
SqlSource.UpdateParameters.Add(new Parameter("UserShared", TypeCode.Int32));
SqlSource.UpdateParameters.Add(new Parameter("Available", TypeCode.Int32));
}

else if (ddlSelect.SelectedItem.Value == "2")
{
gdvSelect.DataSource = SQLQueries.UserListBindData();
gdvSelect.DataBind();
}
}

Cheers,

Stelly
 
When you populate your DropDownList, have you wrapped it in a 'IsPostBack' check in your Page_Load() event?

Like:

Code:
if(!IsPostBack)
{
    // populate drop down list here
}

Because if you have, when the page posts back after selecting an item from the list, the Drop Down List won't be populated again before it gets to your SelectedIndexChanged method..
 
Back
Top Bottom