Events in DataList EditItem Template won't fire

Discussion in 'ASP .Net' started by Crazy Cat, Oct 13, 2008.

  1. Crazy Cat

    Crazy Cat Guest

    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();
    }
    Crazy Cat, Oct 13, 2008
    #1
    1. Advertising

  2. The first thing I am going to suggest is that you use a
    System.Text.StringBuilder to create the JavaScript rather than a bunch of
    concatenation operators (it is more efficient and the code is easier to
    read). In your EnableCheckNumberVisibility function, what is CheckNumber? If
    it is one of the controls, it will not work because the ID used on the
    client is not the one you enter server-side (you need to use the ClientId
    property to get the generated ID, do a View Source in your browser to see
    what I'm talking about). Also, when referencing an element in JavaScript,
    you should always use the document.getElementById(elementid) method. Also, I
    think it would be helpful to everyone in these newsgroups if you posted the
    DataList code in your *.aspx file as well.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Crazy Cat" <> wrote in message
    news:...
    > 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();
    > }
    Nathan Sokalski, Oct 14, 2008
    #2
    1. Advertising

  3. Crazy Cat

    Crazy Cat Guest

    On Oct 13, 9:15 pm, "Nathan Sokalski" <> wrote:
    > The first thing I am going to suggest is that you use a
    > System.Text.StringBuilder to create the JavaScript rather than a bunch of
    > concatenation operators (it is more efficient and the code is easier to
    > read).


    Good tip. Thanks.

    In your EnableCheckNumberVisibility function, what is CheckNumber? If
    > it is one of the controls, it will not work because the ID used on the
    > client is not the one you enter server-side (you need to use the ClientId
    > property to get the generated ID, do a View Source in your browser to see
    > what I'm talking about).


    CheckNumber is a textbox control and I am using ClientId (see the
    ItemCreated
    method where I attach the client side handler).

    Also, when referencing an element in JavaScript,
    > you should always use the document.getElementById(elementid) method.


    Initially, I used the getElementById method, but that actually caused
    an error. Apparently the
    javascript resolved the object name I passed it into a DOM element,
    and when I attempted to
    call getElementById with what it saw as an object and not a string, it
    threw an exception.

    Also, I
    > think it would be helpful to everyone in these newsgroups if you posted the
    > DataList code in your *.aspx file as well.


    I'm not at the office now, but I will post the *.aspx file tomorrow.

    Thanks for your suggestions.

    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > "Crazy Cat" <> wrote in message
    >
    > news:...
    >
    > > 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();
    > >    }
    Crazy Cat, Oct 14, 2008
    #3
  4. Crazy Cat

    Crazy Cat Guest

    HI all,

    I have additional info -- here is the markup for my datalist --

    <asp:DataList ID="dlPayments"
    runat="server"

    OnCancelCommand="dlPayments_CancelCommand"

    OnUpdateCommand="dlPayments_UpdateCommand"
    OnEditCommand="dlPayments_EditCommand"

    OnDeleteCommand="dlPayments_DeleteCommand"

    OnItemDataBound="dlPayments_ItemDataBound"

    OnItemCreated="dlPayments_ItemCreated"

    OnItemCommand="dlPayments_ItemCommand" >
    <HeaderTemplate>
    <table width="410px" border="1"
    cellpadding="2" cellspacing="2" class="tblCss" style="border-
    color:#0066CC">
    <thead>
    <tr>
    <th class="tblRowMainCss"
    style="width: 20%;">
    Payment Type
    </th>
    <th class="tblRowMainCss"
    style="width: 20%;">
    Amount
    </th>
    <th class="tblRowMainCss"
    style="width: *;">
    Check Number <br />
    (If Applicable)
    </th>
    </tr>
    </thead>
    </HeaderTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    <ItemTemplate>
    <tr>
    <td class="tblRowMainCss">
    <asp:Label ID="lblPaymentType"
    runat="server" Text='<%# Eval("TypeCode") %>'></asp:Label>
    </td>
    <td class="tblRowMainCss">
    <asp:Label ID="lblAmount"
    runat="server" Text='<%# String.Format("{0:C}",Eval("Amount")) %>'></
    asp:Label>
    </td>
    <td class="tblRowMainCss">
    <asp:Label ID="lblCheckNumber"
    runat="server" Text='<%# Eval("CheckNumber") %>'></asp:Label>
    </td>
    <td class="tblRowMainCss">
    <asp:LinkButton ID="lbSelect"
    runat="server"
    Text="Edit"
    CommandName="edit">
    </asp:LinkButton>
    <asp:LinkButton ID="LinkButton1"
    runat="server"
    Text="Add"
    CommandName="add">
    </asp:LinkButton>
    <asp:LinkButton ID="LinkButton2"
    runat="server"
    Text="Delete"
    CommandName="Delete">
    </asp:LinkButton>
    </td>
    </tr>
    </ItemTemplate>
    <EditItemTemplate>
    <tr>
    <td class="tblRowMainCss">
    <asp:DropDownList ID="ddlType"
    DataValueField='<%#
    Eval("TypeCode") %>'
    runat="server">
    </asp:DropDownList>
    </td>
    <td class="tblRowMainCss">
    <asp:TextBox ID="txtAmount"
    runat="server" Text='<%# Eval("Amount") %>'></asp:TextBox>
    </td>
    <td class="tblRowMainCss">
    <asp:TextBox ID="txtCheckNumber"
    runat="server" Text='<%# Eval("CheckNumber") %>'></asp:TextBox>
    </td>
    <td>
    <asp:LinkButton ID="LinkButton2"
    runat="server"
    Text="Save Changes"
    CommandName="update">
    </asp:LinkButton>
    <asp:LinkButton ID="LinkButton3"
    runat="server"
    Text="Cancel"
    CommandName="cancel">
    </asp:LinkButton>
    </td>
    </tr>
    </EditItemTemplate>
    </asp:DataList>


    On Oct 13, 10:56 pm, Crazy Cat <> wrote:
    > On Oct 13, 9:15 pm, "Nathan Sokalski" <> wrote:
    >
    > > The first thing I am going to suggest is that you use a
    > > System.Text.StringBuilder to create the JavaScript rather than a bunch of
    > > concatenation operators (it is more efficient and the code is easier to
    > > read).

    >
    > Good tip. Thanks.
    >
    >  In your EnableCheckNumberVisibility function, what is CheckNumber? If
    >
    > > it is one of the controls, it will not work because the ID used on the
    > > client is not the one you enter server-side (you need to use the ClientId
    > > property to get the generated ID, do a View Source in your browser to see
    > > what I'm talking about).

    >
    > CheckNumber is a textbox control and I am using ClientId (see the
    > ItemCreated
    > method where I attach the client side handler).
    >
    > Also, when referencing an element in JavaScript,
    >
    > > you should always use the document.getElementById(elementid) method.

    >
    > Initially, I used the getElementById method, but that actually caused
    > an error. Apparently the
    > javascript resolved the object name I passed it into a DOM element,
    > and when I attempted to
    > call getElementById with what it saw as an object and not a string, it
    > threw an exception.
    >
    > Also, I
    >
    > > think it would be helpful to everyone in these newsgroups if you posted the
    > > DataList code in your *.aspx file as well.

    >
    > I'm not at the office now, but I will post the *.aspx file tomorrow.
    >
    > Thanks for your suggestions.
    >
    >
    >
    > > --
    > > Nathan Sokalski
    > > ://www.nathansokalski.com/

    >
    > > "Crazy Cat" <> wrote in message

    >
    > >news:....

    >
    > > > 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();
    > > >    }- Hide quoted text -

    >
    > - Show quoted text -
    Crazy Cat, Oct 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Hancock

    Inherited Page Won't Fire Postback Events

    James Hancock, Apr 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    448
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 8, 2004
  2. =?Utf-8?B?TWFydHkgVS4=?=

    DataList-EditItem-Required Validator

    =?Utf-8?B?TWFydHkgVS4=?=, Sep 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    457
    =?Utf-8?B?TWFydHkgVS4=?=
    Sep 28, 2004
  3. Chad
    Replies:
    4
    Views:
    8,324
  4. fix
    Replies:
    0
    Views:
    367
  5. James Cooke
    Replies:
    0
    Views:
    226
    James Cooke
    Sep 11, 2008
Loading...

Share This Page