Wednesday, July 28, 2010

Bind the DropDownList On Selected IndexChanged based on the ID


Output :
Bind the DropDownList On Selected IndexChanged based on the ID





Table Design :
MASTERSITE
SELECT SITE_SITEID_ID, SITE_SITECODE_ID, STATE_STATEID_ID,
FROM MASTERSITE

MASTERSTATE
SELECT STATE_STATEID_ID, STATE_STATENAME_NM, STATE_SHORTNAME_CD
FROM MASTERSTATE



Stored Procedure :
Alter Procedure SelectSite
(
@STATE_STATEID_ID int
)
As
Select * from MASTERSITE where STATE_STATEID_ID = @STATE_STATEID_ID



CODE:
Default.aspx

StateName:
SiteCode:
::Select


Default.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default3 : System.Web.UI.Page
{

#region Declare
BOMaster objbomaster;
BLLMaster objbllmaster;
CommonBinding objcmnbinding;
DataSet ds;
#endregion

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bindddl();
// BindGV();
}
}

public void Bindddl()
{
objcmnbinding = new CommonBinding();


ddlState = objcmnbinding.CommonBindingDDL("SelectState", "STATE_STATENAME_NM", "STATE_STATEID_ID", ddlState);

}
//ddlState_SelectedIndexChanged
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
objbomaster = new BOMaster();
objbllmaster = new BLLMaster();
ds = new DataSet();
objbomaster.StateID = Convert.ToInt32(ddlState.SelectedItem.Value);
ds = objbllmaster.SelectSiteCode(objbomaster);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
ddlSiteCode.DataTextField = "SITE_SITEID_ID";
ddlSiteCode.DataValueField = "SITE_SITECODE_ID";
ddlSiteCode.DataSource = ds.Tables[0];
ddlSiteCode.DataBind();
ListItem itm = new ListItem("::Select::", "0");
ddlSiteCode.Items.Insert(0, itm);
//return ddlSiteCode;
}
else
{
ListItem itm = new ListItem("::Select::", "0");
ddlSiteCode.Items.Insert(0, itm);
}

}
catch (Exception ex)
{
lblmsg.ForeColor = System.Drawing.Color.Red;
lblmsg.Text = ex.Message.ToString();
}
}
}


DAL

#region SelectSiteCode
public DataSet SelectSiteCode(object ObjBOClass)
{
try
{
objbomaster = ((BOMaster)(ObjBOClass));
objsqlhelper = new sqlhelper();
objsqlhelper.ClearParameters();
objsqlhelper.CreateParameter("@STATE_STATEID_ID", objbomaster.StateID);
return objsqlhelper.ExecuteQueryWP("SelectSite");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
#endregion


BLL


#region SelectSiteCode
public DataSet SelectSiteCode(object ObjBOClass)
{
try
{
return (DataSet)new DALMaster().SelectSiteCode(ObjBOClass);
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
#endregion


AppCode/CommonBinding.cs

#region DropDownListBinding
public DropDownList CommonBindingDDL(string SPName, string DataTextField, string DataValueField, DropDownList ddlName)
{
try
{
objbllmaster = new BLLMaster();
ds = new DataSet();
ds = objbllmaster.GetDataFromDatabase(SPName);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
ddlName.DataTextField = DataTextField;
ddlName.DataValueField = DataValueField;
ddlName.DataSource = ds.Tables[0];
ddlName.DataBind();
ListItem itm = new ListItem("::Select::", "0");
ddlName.Items.Insert(0, itm);
return ddlName;
}
else
{
return null;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
#endregion

0 comments:

Post a Comment