I have two dropdownlists 'PakPeriod' and 'PackageName' in update form, 'Pack period' binding in '.axpx' page only, but 'PackName' binding in 'PackPeriod_SelectedIndexChange' event from database.
problem: when I clicking 'packperiod' then page getting post back, I have to avoid this post back and should work 'PackPeriod_SelectedIndexChange' (for bind 'PackageName' dropdown list and other jobs) and update form on 'update' button click,
public void ddlUpdatepackNameBind()
{
string period = ddlPackPeriodEdit.SelectedValue;
try
{
//open the db connection if it is closed...
if (connection.State == ConnectionState.Closed)
connection.Open();
string packagePeriod = ddlPackPeriodEdit.SelectedValue;
switch (period)
{
case "Monthly":
command = new SqlCommand();
command.CommandText = "sp_Get_PackName";
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("@packagePeriod", packagePeriod);
ddlPackNameEdit.DataSource = command.ExecuteReader();
ddlPackNameEdit.DataTextField = "PackageName";
ddlPackNameEdit.DataValueField = "PackageId";
ddlPackNameEdit.DataBind();
ddlPackNameEdit.Items.Insert(0, new ListItem("--Select Package Name--", "0"));
break;
case "Quarterly":
command = new SqlCommand();
command.CommandText = "sp_Get_PackName";
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("@packagePeriod", packagePeriod);
ddlPackNameEdit.DataSource = command.ExecuteReader();
ddlPackNameEdit.DataTextField = "PackageName";
ddlPackNameEdit.DataValueField = "PackageId";
ddlPackNameEdit.DataBind();
ddlPackNameEdit.Items.Insert(0, new ListItem("--Select Package Name--", "0"));
break;
case "HalfYearly":
command = new SqlCommand();
command.CommandText = "sp_Get_PackName";
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("@packagePeriod", packagePeriod);
ddlPackNameEdit.DataSource = command.ExecuteReader();
ddlPackNameEdit.DataTextField = "PackageName";
ddlPackNameEdit.DataValueField = "PackageId";
ddlPackNameEdit.DataBind();
ddlPackNameEdit.Items.Insert(0, new ListItem("--Select Package Name--", "0"));
break;
case "Yearly":
command = new SqlCommand();
command.CommandText = "sp_Get_PackName";
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("@packagePeriod", packagePeriod);
ddlPackNameEdit.DataSource = command.ExecuteReader();
ddlPackNameEdit.DataTextField = "PackageName";
ddlPackNameEdit.DataValueField = "PackageId";
ddlPackNameEdit.DataBind();
ddlPackNameEdit.Items.Insert(0, new ListItem("--Select Package Name--", "0"));
break;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally //Close db Connection if it is open....
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
public void packUpdate()
{
try
{
//open the db connection if it is closed...
if (connection.State == ConnectionState.Closed)
connection.Open();
int packageId = Convert.ToInt32(ddlPackNameEdit.SelectedValue);
string packNewName = txtPackNameEdit.Text;
string packNewPeriod = ddlPackPeriodEdit.SelectedItem.ToString();
string packNewPrice = txtPackPriceEdit.Text;
command = new SqlCommand();
command.CommandText = "sp_PackageUpdate";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@packageId", packageId);
command.Parameters.AddWithValue("@packNewName", packNewName);
command.Parameters.AddWithValue("@packNewPeriod", packNewPeriod);
command.Parameters.AddWithValue("@packNewPrice", packNewPrice);
command.Connection = connection;
command.ExecuteNonQuery();
lblMessage.Text = "Record Updated successfully";
lblMessage.Visible = true;
ddlPackNameEdit.SelectedIndex = -1;
ddlPackPeriodEdit.SelectedIndex = -1;
txtPackNameEdit.Text = "";
txtPackPriceEdit.Text = "";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally //Close db Connection if it is open....
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
/*Update form*/