Detailsview - how to reference a field in Java/VB Script

Discussion in 'ASP .Net Web Controls' started by LehrSJ, Sep 13, 2006.

  1. LehrSJ

    LehrSJ Guest

    I have a detailsview control. I need to know how to refererence the form
    fields in the detailsview with VBScript. If I have a text field where the
    user enters data and then I in turn want to convert all of the text to upper
    case with VB or Java script I found I can do this by using
    Attributes.Add("onchange", "doUpperCase(this)"). I can then create a script
    doUpperCase that capitalizes the field. I don't need the actual field name
    because it is passed as (this).

    However I don't know what to do if I have say, a checkbox and a text box.
    If the user checks the checkbox I want to have VBScript that changes the
    value of the text box. But I don't know what name to use for the text box.
    I found I could look directly at the HTML code that is generated and pull out
    the field name. But that doesn't seem the correct way to do it.

    Thanks for any help

    --
    LehrSJ
     
    LehrSJ, Sep 13, 2006
    #1
    1. Advertising

  2. Hello LehrSJ,

    In ASP.NET web page, the server control's ID will vary from their actual
    client id(of the html element) when render to client browser. Therefore,
    when we want to inject some script code which will manipulate the ASP.NET
    webcontrol's client-side element, we should use the "ClientID" property of
    the control, also this property is only useful after the control has been
    added into a parent control collection in the page's control tree. A
    general way to view an ASP.NET page's control tree is turn on the trace on
    page through the @page directive, like:

    <%@ Page Trace="true" ....................... %>

    For your scenario, are you using TemplateField for the column you put the
    checkbox and TextBox which need to interact with each other on client-side?
    If not, I would suggest you use TemplateField so that we can have more
    flexible control over those controls. For example:

    =======================
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    DataKeyNames="id"
    DataSourceID="SqlDataSource1" Height="50px" Width="125px"
    AllowPaging="True" OnItemCreated="DetailsView1_ItemCreated">
    <Fields>
    ....................................
    <asp:TemplateField HeaderText="Operate">
    <EditItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" />
    <asp:TextBox ID="TextBox1"
    runat="server"></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text="name:
    "></asp:Label>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Eval("name") %>' ></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
    </Fields>
    </asp:DetailsView>
    ============================================

    And we can define some client scripts which accept control's ClientID as
    parameter and manipulate them. e.g

    ==============================
    <head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function chk_onchange(chkid, txtid)
    {
    var chk = document.getElementById(chkid);
    var txt = document.getElementById(txtid);

    txt.value = chk.checked;

    }

    </script>
    ===============================

    In code behind, we can use the DetailsView control's "ItemCreated" event to
    bind script function to control's attributes as below:

    ===============================
    protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    {
    if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
    {

    CheckBox chk = DetailsView1.FindControl("CheckBox1") as
    CheckBox;
    TextBox txt = DetailsView1.FindControl("TextBox1") as TextBox;

    chk.Attributes["onclick"] = string.Format("chk_onchange( '{0}',
    '{1}');", chk.ClientID, txt.ClientID);

    }
    }

    ============================

    Hope this helps some. If you have anything unclear or any further questions
    on this, please feel free to let me know.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Sep 14, 2006
    #2
    1. Advertising

  3. LehrSJ

    LehrSJ Guest

    Thanks. That works. And yes I am using template fields.
    --
    LehrSJ


    "Steven Cheng[MSFT]" wrote:

    > Hello LehrSJ,
    >
    > In ASP.NET web page, the server control's ID will vary from their actual
    > client id(of the html element) when render to client browser. Therefore,
    > when we want to inject some script code which will manipulate the ASP.NET
    > webcontrol's client-side element, we should use the "ClientID" property of
    > the control, also this property is only useful after the control has been
    > added into a parent control collection in the page's control tree. A
    > general way to view an ASP.NET page's control tree is turn on the trace on
    > page through the @page directive, like:
    >
    > <%@ Page Trace="true" ....................... %>
    >
    > For your scenario, are you using TemplateField for the column you put the
    > checkbox and TextBox which need to interact with each other on client-side?
    > If not, I would suggest you use TemplateField so that we can have more
    > flexible control over those controls. For example:
    >
    > =======================
    > <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    > DataKeyNames="id"
    > DataSourceID="SqlDataSource1" Height="50px" Width="125px"
    > AllowPaging="True" OnItemCreated="DetailsView1_ItemCreated">
    > <Fields>
    > ....................................
    > <asp:TemplateField HeaderText="Operate">
    > <EditItemTemplate>
    > <asp:CheckBox ID="CheckBox1" runat="server" />
    > <asp:TextBox ID="TextBox1"
    > runat="server"></asp:TextBox>
    > </EditItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label1" runat="server" Text="name:
    > "></asp:Label>
    > <asp:Label ID="Label2" runat="server" Text='<%#
    > Eval("name") %>' ></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    > <asp:CommandField ShowEditButton="True" />
    > </Fields>
    > </asp:DetailsView>
    > ============================================
    >
    > And we can define some client scripts which accept control's ClientID as
    > parameter and manipulate them. e.g
    >
    > ==============================
    > <head runat="server">
    > <title>Untitled Page</title>
    > <script type="text/javascript">
    > function chk_onchange(chkid, txtid)
    > {
    > var chk = document.getElementById(chkid);
    > var txt = document.getElementById(txtid);
    >
    > txt.value = chk.checked;
    >
    > }
    >
    > </script>
    > ===============================
    >
    > In code behind, we can use the DetailsView control's "ItemCreated" event to
    > bind script function to control's attributes as below:
    >
    > ===============================
    > protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    > {
    > if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
    > {
    >
    > CheckBox chk = DetailsView1.FindControl("CheckBox1") as
    > CheckBox;
    > TextBox txt = DetailsView1.FindControl("TextBox1") as TextBox;
    >
    > chk.Attributes["onclick"] = string.Format("chk_onchange( '{0}',
    > '{1}');", chk.ClientID, txt.ClientID);
    >
    > }
    > }
    >
    > ============================
    >
    > Hope this helps some. If you have anything unclear or any further questions
    > on this, please feel free to let me know.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
     
    LehrSJ, Sep 17, 2006
    #3
    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. Stu
    Replies:
    1
    Views:
    2,613
  2. =?Utf-8?B?UmFt?=

    DetailsView template field problem

    =?Utf-8?B?UmFt?=, Dec 1, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    6,618
    Supraman
    Sep 19, 2008
  3. Feege
    Replies:
    0
    Views:
    487
    Feege
    Dec 20, 2005
  4. Sound
    Replies:
    2
    Views:
    465
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    451
Loading...

Share This Page