Tuesday, June 29, 2010

How to Identify a changed row from a dropdownlist in a datagrid iew

protected void ddlPramotionType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList test = (DropDownList)sender;


foreach (GridViewRow row in gvInvoiceItemList.Rows)
{
if (row.RowType ==DataControlRowType.DataRow)
{

DropDownList ddlPramotionType = (DropDownList)row.FindControl("ddlPramotionType");

if (test.ClientID == ddlPramotionType.ClientID)
{
TextBox txtNoOfPacks = ((TextBox)row.FindControl("txtNoOfPacks"));
TextBox txtDiscount = ((TextBox)row.FindControl("txtDiscount"));

if (ddlPramotionType.SelectedItem.Value == "Discount")
{
txtNoOfPacks.Enabled = false;
txtDiscount.Enabled = true;
}
else if (ddlPramotionType.SelectedItem.Value == "FreePack")
{
txtNoOfPacks.Enabled = true;
txtDiscount.Enabled = false;
}
else
{
txtNoOfPacks.Enabled = false;
txtDiscount.Enabled = false;
}
}
}

}
}

Binding a java script when adding rows to data grid view

protected void gvInvoiceItemList_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlPromoType = (DropDownList)e.Row.FindControl("ddlPramotionType");
TextBox txtNoofPacks = (TextBox)e.Row.FindControl("txtNoOfPacks");
TextBox txtDiscounts = (TextBox)e.Row.FindControl("txtDiscount");

if (ddlPromoType != null && txtNoofPacks != null && txtDiscounts != null)
{
string function = "DisableTextboxes('" + txtNoofPacks.ClientID + "','" + txtDiscounts.ClientID + "','" + ddlPromoType.ClientID + "')";

//add a java script to drop down list

ddlPromoType.Attributes.Add("onchange", function);
ClientScript.RegisterStartupScript(this.GetType(), ddlPromoType.ClientID, "");

}
}
}
catch (Exception ex)
{

}
}


// java script