Here i am going to explain how to do cascading drop down list in asp.net webform.code is copied from my project so not able to formatted properly due to lack of time.but you see the code and understand the implementation.
.aspx code
OnRowCommand="gvCity_RowCommand" OnRowEditing="gvCity_RowEditing" OnRowCancelingEdit="gvCity_RowCancelingEdit"OnRowUpdating="gvCity_RowUpdating" OnRowDeleting="gvCity_RowDeleting" ShowFooter="True"
PageSize="20" Width="100%">
runat="server">
AutoPostBack="true" OnSelectedIndexChanged="ddlNewCountrygvCity_SelectedIndexChanged"
Visible="false" runat="server">
runat="server">
Visible="false" Enabled="false" runat="server">
Visible="false">
OnClientClick="return CityUpdateValidate();" ImageUrl="~/Image/update.png" ToolTip="Update"
ToolTip="Cancel" ImageUrl="~/Image/cancel.png" />
CommandName="AddNew" ToolTip="Add New" />
OnClick="ibtnNewCityInsert_Click" ImageUrl="~/Image/add.png" Visible="false"
ToolTip="Insert" />
ToolTip="Cancel" ImageUrl="~/Image/cancel.png" Visible="false" />
ImageUrl="~/Image/img_edit.png" ToolTip="Edit" />
ImageUrl="~/Image/img_delete.png" OnClientClick="return ConfirmDelete();" ToolTip="Delete"
.cs code
public void gvCityBind()
{
dsCity = SqlHelper.ExecuteDataset(connectionString, "genCityMaster_SelectDelete", 0, "NULL", "S");
if (dsCity.Tables[0].Rows.Count > 0)
{
gvCity.DataSource = dsCity;
gvCity.DataBind();
}
else
{
NoDataFound(dsCity, gvCity);
SrNoCity = 1;
}
}
protected void gvCity_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCity.PageIndex = e.NewPageIndex;
gvCityBind();
}
protected void gvCity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");
lblSrNo.Text = Convert.ToString(gvCity.PageIndex * gvCity.PageSize + SrNoCity);
SrNoCity++;
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
DropDownList ddlState = (DropDownList)e.Row.FindControl("ddlState");
if (ddlCountry != null)
{
ddlCountry.DataSource = objCountryBLL.SelectCounrty();
ddlCountry.DataBind();
ddlCountry.SelectedValue = dsCity.Tables[0].Rows[e.Row.RowIndex]["CountryId"].ToString();
}
if (ddlState != null)
{
ddlState.DataSource = objStateBLL.SelectState(Convert.ToInt32(ddlCountry.SelectedValue));
ddlState.DataBind();
ddlState.SelectedValue = dsCity.Tables[0].Rows[e.Row.RowIndex]["StateId"].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlNewCountry = (DropDownList)e.Row.FindControl("ddlNewCountry");
DropDownList ddlNewState = (DropDownList)e.Row.FindControl("ddlNewState");
if (ddlNewCountry != null)
{
ddlNewCountry.DataSource = objCountryBLL.SelectCounrty();
ddlNewCountry.DataBind();
}
if (ddlNewState != null)
{
ddlNewState.DataSource = objStateBLL.SelectState(Convert.ToInt32(ddlNewCountry.SelectedValue));
ddlNewState.DataBind();
}
}
}
protected void gvCity_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("AddNew"))
{
gvCity.EditIndex = -1;
gvCityBind();
gvCity.FooterRow.CssClass = objCommonDAL.GetGrigViewFooterStyle();
Label lblNewSrNo = (Label)gvCity.FooterRow.FindControl("lblNewSrNo");
lblNewSrNo.Text = SrNoCity.ToString();
ImageButton ibtnNewCityInsert = (ImageButton)gvCity.FooterRow.FindControl("ibtnNewCityInsert");
ImageButton imgbtnNewCancel = (ImageButton)gvCity.FooterRow.FindControl("imgbtnNewCancel");
ImageButton imgbtnNewAdd = (ImageButton)gvCity.FooterRow.FindControl("imgbtnNewAdd");
ibtnNewCityInsert.Visible = true;
imgbtnNewCancel.Visible = true;
imgbtnNewAdd.Visible = false;
DropDownList ddlNewCountry = (DropDownList)gvCity.FooterRow.FindControl("ddlNewCountry");
ddlNewCountry.Visible = true;
ddlNewCountry.Focus();
DropDownList ddlNewState = (DropDownList)gvCity.FooterRow.FindControl("ddlNewState");
ddlNewState.Visible = true;
TextBox txtNewCity = (TextBox)gvCity.FooterRow.FindControl("txtNewCity");
txtNewCity.Visible = true;
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void ibtnNewCityInsert_Click(object sender, ImageClickEventArgs e)
{
try
{
objCity.StateId = Convert.ToInt32(((DropDownList)gvCity.FooterRow.FindControl("ddlNewState")).SelectedValue);
objCity.CityName = Convert.ToString(((TextBox)gvCity.FooterRow.FindControl("txtNewCity")).Text);
objCity.CreatedBy = Convert.ToInt32(UserId);
objCity.ModifiedBy = Convert.ToInt32(UserId);
objCity.CreatedOn = System.DateTime.Now;
objCity.ModifiedOn = System.DateTime.Now;
try
{
int intResultInsert = objCityBLL.InsertCity(objCity);
if (intResultInsert > 0)
{
exceptionMessage.Text = "Record Added Successfully !";
gvCity.EditIndex = -1;
gvCityBind();
}
else
{
exceptionMessage.Text = "This record already exists !";
}
}
catch (Exception ex)
{
exceptionMessage.Text = ex.Message.ToString();
}
finally
{
objCityBLL = null;
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void gvCity_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCity.EditIndex = e.NewEditIndex;
gvCityBind();
}
protected void gvCity_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCity.EditIndex = -1;
gvCityBind();
}
protected void gvCity_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
objCity.CityId = Convert.ToInt32(((Label)gvCity.Rows[e.RowIndex].FindControl("lblCityId")).Text);
objCity.StateId = Convert.ToInt32(((DropDownList)gvCity.Rows[e.RowIndex].FindControl("ddlState")).SelectedValue);
objCity.CityName = Convert.ToString(((TextBox)gvCity.Rows[e.RowIndex].FindControl("txtCity")).Text);
objCity.CreatedBy = Convert.ToInt32(UserId);
objCity.ModifiedBy = Convert.ToInt32(UserId);
objCity.CreatedOn = System.DateTime.Now;
objCity.ModifiedOn = System.DateTime.Now;
int intResultUpdate = objCityBLL.UpdateCity(objCity);
if (intResultUpdate > 0)
{
exceptionMessage.Text = "Record Updated Successfully !";
gvCity.EditIndex = -1;
gvCityBind();
}
}
catch (Exception ex)
{
exceptionMessage.Text = ex.Message.ToString();
}
finally
{
objCityBLL = null;
}
}
protected void gvCity_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
objCity.CityId = (int)gvCity.DataKeys[e.RowIndex].Value;
int result = objCityBLL.DeleteCity(objCity);
if (result > 0)
{
exceptionMessage.Text = "Row Deleted Successfully...!";
gvCity.EditIndex = -1;
gvCityBind();
}
}
catch (Exception ex)
{
exceptionMessage.Text = ex.Message.ToString();
}
}
protected void ddlNewCountrygvCity_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlNewCountry = (DropDownList)gvCity.FooterRow.FindControl("ddlNewCountry");
DropDownList ddlNewState = (DropDownList)gvCity.FooterRow.FindControl("ddlNewState");
if (Convert.ToInt32(ddlNewCountry.SelectedValue) > 0)
{
ddlNewState.Enabled = true;
ddlNewState.DataSource = objStateBLL.SelectState(Convert.ToInt32(ddlNewCountry.SelectedValue));
ddlNewState.DataBind();
}
else
{
ddlNewState.Enabled = false;
}
}
0 comments:
Post a Comment