Events in DataList EditItem Template won't fire

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Crazy Cat

    Events in DataList EditItem Template won't fire

    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();
    }
  • Nathan Sokalski

    #2
    Re: Events in DataList EditItem Template won't fire

    The first thing I am going to suggest is that you use a
    System.Text.Str ingBuilder to create the JavaScript rather than a bunch of
    concatenation operators (it is more efficient and the code is easier to
    read). In your EnableCheckNumb erVisibility 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.getEle mentById(elemen tid) 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
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


    "Crazy Cat" <danbredy@hotma il.comwrote in message
    news:bb03db6b-58d5-4c33-86ee-7ac577da7ae2@d4 5g2000hsc.googl egroups.com...
    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();
    }

    Comment

    • Crazy Cat

      #3
      Re: Events in DataList EditItem Template won't fire

      On Oct 13, 9:15 pm, "Nathan Sokalski" <njsokal...@hot mail.comwrote:
      The first thing I am going to suggest is that you use a
      System.Text.Str ingBuilder 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 EnableCheckNumb erVisibility 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.getEle mentById(elemen tid) 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
      njsokal...@hotm ail.comhttp://www.nathansokal ski.com/
      >
      "Crazy Cat" <danbr...@hotma il.comwrote in message
      >
      news:bb03db6b-58d5-4c33-86ee-7ac577da7ae2@d4 5g2000hsc.googl egroups.com...
      >
      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();
         }

      Comment

      • Crazy Cat

        #4
        Re: Events in DataList EditItem Template won't fire

        HI all,

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

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

        OnCancelCommand ="dlPayments_Ca ncelCommand"

        OnUpdateCommand ="dlPayments_Up dateCommand"
        OnEditCommand=" dlPayments_Edit Command"

        OnDeleteCommand ="dlPayments_De leteCommand"

        OnItemDataBound ="dlPayments_It emDataBound"

        OnItemCreated=" dlPayments_Item Created"

        OnItemCommand=" dlPayments_Item Command" >
        <HeaderTemplate >
        <table width="410px" border="1"
        cellpadding="2" cellspacing="2" class="tblCss" style="border-
        color:#0066CC">
        <thead>
        <tr>
        <th class="tblRowMa inCss"
        style="width: 20%;">
        Payment Type
        </th>
        <th class="tblRowMa inCss"
        style="width: 20%;">
        Amount
        </th>
        <th class="tblRowMa inCss"
        style="width: *;">
        Check Number <br />
        (If Applicable)
        </th>
        </tr>
        </thead>
        </HeaderTemplate>
        <FooterTemplate >
        </table>
        </FooterTemplate>
        <ItemTemplate >
        <tr>
        <td class="tblRowMa inCss">
        <asp:Label ID="lblPaymentT ype"
        runat="server" Text='<%# Eval("TypeCode" ) %>'></asp:Label>
        </td>
        <td class="tblRowMa inCss">
        <asp:Label ID="lblAmount"
        runat="server" Text='<%# String.Format(" {0:C}",Eval("Am ount")) %>'></
        asp:Label>
        </td>
        <td class="tblRowMa inCss">
        <asp:Label ID="lblCheckNum ber"
        runat="server" Text='<%# Eval("CheckNumb er") %>'></asp:Label>
        </td>
        <td class="tblRowMa inCss">
        <asp:LinkButt on ID="lbSelect"
        runat="server"
        Text="Edit"
        CommandName="ed it">
        </asp:LinkButton>
        <asp:LinkButt on ID="LinkButton1 "
        runat="server"
        Text="Add"
        CommandName="ad d">
        </asp:LinkButton>
        <asp:LinkButt on ID="LinkButton2 "
        runat="server"
        Text="Delete"
        CommandName="De lete">
        </asp:LinkButton>
        </td>
        </tr>
        </ItemTemplate>
        <EditItemTempla te>
        <tr>
        <td class="tblRowMa inCss">
        <asp:DropDownLi st ID="ddlType"
        DataValueField= '<%#
        Eval("TypeCode" ) %>'
        runat="server">
        </asp:DropDownLis t>
        </td>
        <td class="tblRowMa inCss">
        <asp:TextBox ID="txtAmount"
        runat="server" Text='<%# Eval("Amount") %>'></asp:TextBox>
        </td>
        <td class="tblRowMa inCss">
        <asp:TextBox ID="txtCheckNum ber"
        runat="server" Text='<%# Eval("CheckNumb er") %>'></asp:TextBox>
        </td>
        <td>
        <asp:LinkButt on ID="LinkButton2 "
        runat="server"
        Text="Save Changes"
        CommandName="up date">
        </asp:LinkButton>
        <asp:LinkButt on ID="LinkButton3 "
        runat="server"
        Text="Cancel"
        CommandName="ca ncel">
        </asp:LinkButton>
        </td>
        </tr>
        </EditItemTemplat e>
        </asp:DataList>


        On Oct 13, 10:56 pm, Crazy Cat <danbr...@hotma il.comwrote:
        On Oct 13, 9:15 pm, "Nathan Sokalski" <njsokal...@hot mail.comwrote:
        >
        The first thing I am going to suggest is that you use a
        System.Text.Str ingBuilder 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 EnableCheckNumb erVisibility 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.getEle mentById(elemen tid) 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 postedthe
        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
        njsokal...@hotm ail.comhttp://www.nathansokal ski.com/
        >
        "Crazy Cat" <danbr...@hotma il.comwrote in message
        >
        news:bb03db6b-58d5-4c33-86ee-7ac577da7ae2@d4 5g2000hsc.googl egroups.com....
        >
        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();
           }- Hide quoted text -
        >
        - Show quoted text -

        Comment

        Working...