Hi all,
I am developing an asp.net 2.0 application in Visual Studio 2005. On
my page I have a simple datalist that is bound programmaticall y to a
collection of simple objects. On this page I need to control the
visibility of a textbox based on a dropdown selection in the
datalist's edititem template. To do this, I registered a client script
block function and attached a client side handler for the
dropdownlist's onchange event in the datalist's OnItemCreated event.
The client script works fine, but I find that the update and cancel
events won't fire in the EditItem template. My edit, delete, and add
event fire fine in the ItemTemplate. I also find that if I remove the
client side onchange event handler the EditItem commands work fine.
Here is the code --
protected void Page_Load(objec t sender, EventArgs e)
{
// Register client script block containing the event handler for the
datalist's dropdownlist control.
ClientScriptMan ager csm = this.ClientScri pt;
string script = "function EnableCheckNumb erVisibility(Ty pe,
CheckNumber)\n" +
"{ \n" +
"if (Type.value == 'K') { \n" +
"CheckNumber.st yle.visibility = 'visible'; \n"
+
"} else {\n" +
"CheckNumber.st yle.visibility = 'hidden';\n" +
"}\n" +
"return true;\n" +
"}";
csm.RegisterCli entScriptBlock( this.GetType(),
"EnableCheckNum berVisibility", script,true);
if (!IsPostBack)
{
// Get Custom object collection out of session
Payments = BLL.Payments.ge tPaymentsByMemb erId("XXXXXX");
txtMemberAmount .Text =
Payments.Member Amount.ToString ("c");
txtDependentAmo unt.Text =
Payments.Depend entAmount.ToStr ing("c");
txtLateFee.Text = Payments.LateFe e.ToString("c") ;
txtAdminFee.Tex t = Payments.AdminF ee.ToString("c" );
txtTotalDues.Te xt = Payments.TotalD ues.ToString("c ");
Payments.Paymen tCollection.Add (new BLL.Payment());
PaymentTypes = BLL.Payment.get PaymentTypes();
dlPayments.Data Source = Payments.Paymen tCollection;
dlPayments.Data Bind();
}
}
protected void dlPayments_Item Command(object sender,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sende r;
switch (e.CommandName)
{
case "add":
Payments.add(ne w BLL.Payment());
dlPayments.Data Source = Payments.Paymen tCollection;
dlPayments.Data Bind();
break;
}
}
protected void dlPayments_Item DataBound(objec t sender,
DataListItemEve ntArgs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) ||
(e.Item.ItemTyp e == ListItemType.Se lectedItem) ||
(e.Item.ItemTyp e == ListItemType.Al ternatingItem)
)
{
BLL.Payment p = (BLL.Payment)e. Item.DataItem;
foreach (ListItem i in PaymentTypes)
{
if (i.Value == p.TypeCode)
{
Label l =
(Label)e.Item.F indControl("lbl PaymentType");
l.Text = i.Text;
break;
}
}
}
}
protected void dlPayments_Item Created(object sender,
DataListItemEve ntArgs e)
{
// hook up the event handler here
if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList dl =
(DropDownList)e .Item.FindContr ol("ddlType");
dl.Items.AddRan ge(PaymentTypes .ToArray());
TextBox tb =
(TextBox)e.Item .FindControl("t xtCheckNumber") ;
dl.Attributes["onchange"] = "EnableCheckNum berVisibility("
+ dl.ClientID + "," +
tb.ClientID + ");";
}
}
protected void dlPayments_Dele teCommand(objec t source,
DataListCommand EventArgs e)
{
if (e.Item.ItemInd ex 0)
{
Payments.remove (Payments.Payme ntCollection[e.Item.ItemInde x]);
((DataList)sour ce).DataSource =
Payments.Paymen tCollection;
dlPayments.Data Bind();
}
}
protected void dlPayments_Edit Command(object source,
DataListCommand EventArgs e)
{
((DataList)sour ce).EditItemInd ex = e.Item.ItemInde x;
((DataList)sour ce).DataSource = Payments.Paymen tCollection;
((DataList)sour ce).DataBind();
}
protected void dlPayments_Upda teCommand(objec t source,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sourc e;
// Payment Type
DropDownList ddlType =
(DropDownList)e .Item.FindContr ol("ddlType");
Payments.Paymen tCollection[e.Item.ItemInde x].TypeCode =
ddlType.Selecte dValue;
// Amount
TextBox txtAmount = (TextBox)e.Item .FindControl("t xtAmount");
Payments.Paymen tCollection[e.Item.ItemInde x].Amount =
Single.Parse(tx tAmount.Text);
// Check Number
TextBox txtCheckNumber =
(TextBox)e.Item .FindControl("t xtCheckNumber") ;
Payments.Paymen tCollection[e.Item.ItemInde x].CheckNumber =
txtCheckNumber. Text;
dl.DataSource = Payments.Paymen tCollection;
dl.EditItemInde x = -1;
dl.DataBind();
}
protected void dlPayments_Canc elCommand(objec t source,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sourc e;
dl.DataSource = Payments.Paymen tCollection;
dl.EditItemInde x = -1;
dl.DataBind();
}
I am developing an asp.net 2.0 application in Visual Studio 2005. On
my page I have a simple datalist that is bound programmaticall y to a
collection of simple objects. On this page I need to control the
visibility of a textbox based on a dropdown selection in the
datalist's edititem template. To do this, I registered a client script
block function and attached a client side handler for the
dropdownlist's onchange event in the datalist's OnItemCreated event.
The client script works fine, but I find that the update and cancel
events won't fire in the EditItem template. My edit, delete, and add
event fire fine in the ItemTemplate. I also find that if I remove the
client side onchange event handler the EditItem commands work fine.
Here is the code --
protected void Page_Load(objec t sender, EventArgs e)
{
// Register client script block containing the event handler for the
datalist's dropdownlist control.
ClientScriptMan ager csm = this.ClientScri pt;
string script = "function EnableCheckNumb erVisibility(Ty pe,
CheckNumber)\n" +
"{ \n" +
"if (Type.value == 'K') { \n" +
"CheckNumber.st yle.visibility = 'visible'; \n"
+
"} else {\n" +
"CheckNumber.st yle.visibility = 'hidden';\n" +
"}\n" +
"return true;\n" +
"}";
csm.RegisterCli entScriptBlock( this.GetType(),
"EnableCheckNum berVisibility", script,true);
if (!IsPostBack)
{
// Get Custom object collection out of session
Payments = BLL.Payments.ge tPaymentsByMemb erId("XXXXXX");
txtMemberAmount .Text =
Payments.Member Amount.ToString ("c");
txtDependentAmo unt.Text =
Payments.Depend entAmount.ToStr ing("c");
txtLateFee.Text = Payments.LateFe e.ToString("c") ;
txtAdminFee.Tex t = Payments.AdminF ee.ToString("c" );
txtTotalDues.Te xt = Payments.TotalD ues.ToString("c ");
Payments.Paymen tCollection.Add (new BLL.Payment());
PaymentTypes = BLL.Payment.get PaymentTypes();
dlPayments.Data Source = Payments.Paymen tCollection;
dlPayments.Data Bind();
}
}
protected void dlPayments_Item Command(object sender,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sende r;
switch (e.CommandName)
{
case "add":
Payments.add(ne w BLL.Payment());
dlPayments.Data Source = Payments.Paymen tCollection;
dlPayments.Data Bind();
break;
}
}
protected void dlPayments_Item DataBound(objec t sender,
DataListItemEve ntArgs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) ||
(e.Item.ItemTyp e == ListItemType.Se lectedItem) ||
(e.Item.ItemTyp e == ListItemType.Al ternatingItem)
)
{
BLL.Payment p = (BLL.Payment)e. Item.DataItem;
foreach (ListItem i in PaymentTypes)
{
if (i.Value == p.TypeCode)
{
Label l =
(Label)e.Item.F indControl("lbl PaymentType");
l.Text = i.Text;
break;
}
}
}
}
protected void dlPayments_Item Created(object sender,
DataListItemEve ntArgs e)
{
// hook up the event handler here
if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList dl =
(DropDownList)e .Item.FindContr ol("ddlType");
dl.Items.AddRan ge(PaymentTypes .ToArray());
TextBox tb =
(TextBox)e.Item .FindControl("t xtCheckNumber") ;
dl.Attributes["onchange"] = "EnableCheckNum berVisibility("
+ dl.ClientID + "," +
tb.ClientID + ");";
}
}
protected void dlPayments_Dele teCommand(objec t source,
DataListCommand EventArgs e)
{
if (e.Item.ItemInd ex 0)
{
Payments.remove (Payments.Payme ntCollection[e.Item.ItemInde x]);
((DataList)sour ce).DataSource =
Payments.Paymen tCollection;
dlPayments.Data Bind();
}
}
protected void dlPayments_Edit Command(object source,
DataListCommand EventArgs e)
{
((DataList)sour ce).EditItemInd ex = e.Item.ItemInde x;
((DataList)sour ce).DataSource = Payments.Paymen tCollection;
((DataList)sour ce).DataBind();
}
protected void dlPayments_Upda teCommand(objec t source,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sourc e;
// Payment Type
DropDownList ddlType =
(DropDownList)e .Item.FindContr ol("ddlType");
Payments.Paymen tCollection[e.Item.ItemInde x].TypeCode =
ddlType.Selecte dValue;
// Amount
TextBox txtAmount = (TextBox)e.Item .FindControl("t xtAmount");
Payments.Paymen tCollection[e.Item.ItemInde x].Amount =
Single.Parse(tx tAmount.Text);
// Check Number
TextBox txtCheckNumber =
(TextBox)e.Item .FindControl("t xtCheckNumber") ;
Payments.Paymen tCollection[e.Item.ItemInde x].CheckNumber =
txtCheckNumber. Text;
dl.DataSource = Payments.Paymen tCollection;
dl.EditItemInde x = -1;
dl.DataBind();
}
protected void dlPayments_Canc elCommand(objec t source,
DataListCommand EventArgs e)
{
DataList dl = (DataList)sourc e;
dl.DataSource = Payments.Paymen tCollection;
dl.EditItemInde x = -1;
dl.DataBind();
}
Comment