C
Crazy Cat
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 programmatically 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(object sender, EventArgs e)
{
// Register client script block containing the event handler for the
datalist's dropdownlist control.
ClientScriptManager csm = this.ClientScript;
string script = "function EnableCheckNumberVisibility(Type,
CheckNumber)\n" +
"{ \n" +
"if (Type.value == 'K') { \n" +
"CheckNumber.style.visibility = 'visible'; \n"
+
"} else {\n" +
"CheckNumber.style.visibility = 'hidden';\n" +
"}\n" +
"return true;\n" +
"}";
csm.RegisterClientScriptBlock(this.GetType(),
"EnableCheckNumberVisibility", script,true);
if (!IsPostBack)
{
// Get Custom object collection out of session
Payments = BLL.Payments.getPaymentsByMemberId("XXXXXX");
txtMemberAmount.Text =
Payments.MemberAmount.ToString("c");
txtDependentAmount.Text =
Payments.DependentAmount.ToString("c");
txtLateFee.Text = Payments.LateFee.ToString("c");
txtAdminFee.Text = Payments.AdminFee.ToString("c");
txtTotalDues.Text = Payments.TotalDues.ToString("c");
Payments.PaymentCollection.Add(new BLL.Payment());
PaymentTypes = BLL.Payment.getPaymentTypes();
dlPayments.DataSource = Payments.PaymentCollection;
dlPayments.DataBind();
}
}
protected void dlPayments_ItemCommand(object sender,
DataListCommandEventArgs e)
{
DataList dl = (DataList)sender;
switch (e.CommandName)
{
case "add":
Payments.add(new BLL.Payment());
dlPayments.DataSource = Payments.PaymentCollection;
dlPayments.DataBind();
break;
}
}
protected void dlPayments_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.SelectedItem) ||
(e.Item.ItemType == ListItemType.AlternatingItem)
)
{
BLL.Payment p = (BLL.Payment)e.Item.DataItem;
foreach (ListItem i in PaymentTypes)
{
if (i.Value == p.TypeCode)
{
Label l =
(Label)e.Item.FindControl("lblPaymentType");
l.Text = i.Text;
break;
}
}
}
}
protected void dlPayments_ItemCreated(object sender,
DataListItemEventArgs e)
{
// hook up the event handler here
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList dl =
(DropDownList)e.Item.FindControl("ddlType");
dl.Items.AddRange(PaymentTypes.ToArray());
TextBox tb =
(TextBox)e.Item.FindControl("txtCheckNumber");
dl.Attributes["onchange"] = "EnableCheckNumberVisibility("
+ dl.ClientID + "," +
tb.ClientID + ");";
}
}
protected void dlPayments_DeleteCommand(object source,
DataListCommandEventArgs e)
{
if (e.Item.ItemIndex > 0)
{
Payments.remove(Payments.PaymentCollection[e.Item.ItemIndex]);
((DataList)source).DataSource =
Payments.PaymentCollection;
dlPayments.DataBind();
}
}
protected void dlPayments_EditCommand(object source,
DataListCommandEventArgs e)
{
((DataList)source).EditItemIndex = e.Item.ItemIndex;
((DataList)source).DataSource = Payments.PaymentCollection;
((DataList)source).DataBind();
}
protected void dlPayments_UpdateCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = (DataList)source;
// Payment Type
DropDownList ddlType =
(DropDownList)e.Item.FindControl("ddlType");
Payments.PaymentCollection[e.Item.ItemIndex].TypeCode =
ddlType.SelectedValue;
// Amount
TextBox txtAmount = (TextBox)e.Item.FindControl("txtAmount");
Payments.PaymentCollection[e.Item.ItemIndex].Amount =
Single.Parse(txtAmount.Text);
// Check Number
TextBox txtCheckNumber =
(TextBox)e.Item.FindControl("txtCheckNumber");
Payments.PaymentCollection[e.Item.ItemIndex].CheckNumber =
txtCheckNumber.Text;
dl.DataSource = Payments.PaymentCollection;
dl.EditItemIndex = -1;
dl.DataBind();
}
protected void dlPayments_CancelCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = (DataList)source;
dl.DataSource = Payments.PaymentCollection;
dl.EditItemIndex = -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 programmatically 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(object sender, EventArgs e)
{
// Register client script block containing the event handler for the
datalist's dropdownlist control.
ClientScriptManager csm = this.ClientScript;
string script = "function EnableCheckNumberVisibility(Type,
CheckNumber)\n" +
"{ \n" +
"if (Type.value == 'K') { \n" +
"CheckNumber.style.visibility = 'visible'; \n"
+
"} else {\n" +
"CheckNumber.style.visibility = 'hidden';\n" +
"}\n" +
"return true;\n" +
"}";
csm.RegisterClientScriptBlock(this.GetType(),
"EnableCheckNumberVisibility", script,true);
if (!IsPostBack)
{
// Get Custom object collection out of session
Payments = BLL.Payments.getPaymentsByMemberId("XXXXXX");
txtMemberAmount.Text =
Payments.MemberAmount.ToString("c");
txtDependentAmount.Text =
Payments.DependentAmount.ToString("c");
txtLateFee.Text = Payments.LateFee.ToString("c");
txtAdminFee.Text = Payments.AdminFee.ToString("c");
txtTotalDues.Text = Payments.TotalDues.ToString("c");
Payments.PaymentCollection.Add(new BLL.Payment());
PaymentTypes = BLL.Payment.getPaymentTypes();
dlPayments.DataSource = Payments.PaymentCollection;
dlPayments.DataBind();
}
}
protected void dlPayments_ItemCommand(object sender,
DataListCommandEventArgs e)
{
DataList dl = (DataList)sender;
switch (e.CommandName)
{
case "add":
Payments.add(new BLL.Payment());
dlPayments.DataSource = Payments.PaymentCollection;
dlPayments.DataBind();
break;
}
}
protected void dlPayments_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.SelectedItem) ||
(e.Item.ItemType == ListItemType.AlternatingItem)
)
{
BLL.Payment p = (BLL.Payment)e.Item.DataItem;
foreach (ListItem i in PaymentTypes)
{
if (i.Value == p.TypeCode)
{
Label l =
(Label)e.Item.FindControl("lblPaymentType");
l.Text = i.Text;
break;
}
}
}
}
protected void dlPayments_ItemCreated(object sender,
DataListItemEventArgs e)
{
// hook up the event handler here
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList dl =
(DropDownList)e.Item.FindControl("ddlType");
dl.Items.AddRange(PaymentTypes.ToArray());
TextBox tb =
(TextBox)e.Item.FindControl("txtCheckNumber");
dl.Attributes["onchange"] = "EnableCheckNumberVisibility("
+ dl.ClientID + "," +
tb.ClientID + ");";
}
}
protected void dlPayments_DeleteCommand(object source,
DataListCommandEventArgs e)
{
if (e.Item.ItemIndex > 0)
{
Payments.remove(Payments.PaymentCollection[e.Item.ItemIndex]);
((DataList)source).DataSource =
Payments.PaymentCollection;
dlPayments.DataBind();
}
}
protected void dlPayments_EditCommand(object source,
DataListCommandEventArgs e)
{
((DataList)source).EditItemIndex = e.Item.ItemIndex;
((DataList)source).DataSource = Payments.PaymentCollection;
((DataList)source).DataBind();
}
protected void dlPayments_UpdateCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = (DataList)source;
// Payment Type
DropDownList ddlType =
(DropDownList)e.Item.FindControl("ddlType");
Payments.PaymentCollection[e.Item.ItemIndex].TypeCode =
ddlType.SelectedValue;
// Amount
TextBox txtAmount = (TextBox)e.Item.FindControl("txtAmount");
Payments.PaymentCollection[e.Item.ItemIndex].Amount =
Single.Parse(txtAmount.Text);
// Check Number
TextBox txtCheckNumber =
(TextBox)e.Item.FindControl("txtCheckNumber");
Payments.PaymentCollection[e.Item.ItemIndex].CheckNumber =
txtCheckNumber.Text;
dl.DataSource = Payments.PaymentCollection;
dl.EditItemIndex = -1;
dl.DataBind();
}
protected void dlPayments_CancelCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = (DataList)source;
dl.DataSource = Payments.PaymentCollection;
dl.EditItemIndex = -1;
dl.DataBind();
}