Dawood Abbas

Dawood Abbas

  • NA
  • 264
  • 98.4k

Avoid postback operation for asp dropdown onselected index

Mar 20 2015 3:33 AM
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,
I used update panel also but its  not working , so what is the solution ?
 
/*packPeriod Bind*/ 
 
<asp:dropdownlist ID="ddlPackPeriod" runat="server" AutoPostBack="True" >
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="Monthly">Monthly</asp:ListItem>
<asp:ListItem Value="Quarterly">Quarterly</asp:ListItem>
<asp:ListItem Value="HalfYearly">HalfYearly</asp:ListItem>
<asp:ListItem Value="Yearly">Yearly</asp:ListItem>
</asp:dropdownlist>
 
 
/*Pckname bindind*/ 
 
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();
}
}
 
/*updating form*/ 
 
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*/ 
protected void btnPackageUpdate_Click(object sender, EventArgs e)
{
packUpdate();
}
 
 

Answers (4)