Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Detailsview - how to reference a field in Java/VB Script

Reply
Thread Tools

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

 
 
LehrSJ
Guest
Posts: n/a
 
      09-13-2006
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
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      09-14-2006
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:

=======================
<aspetailsView 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>
</aspetailsView>
============================================

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/subscripti...ult.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/subscripti...t/default.aspx.

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



This posting is provided "AS IS" with no warranties, and confers no rights.



 
Reply With Quote
 
 
 
 
LehrSJ
Guest
Posts: n/a
 
      09-17-2006
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:
>
> =======================
> <aspetailsView 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>
> </aspetailsView>
> ============================================
>
> 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/subscripti...ult.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/subscripti...t/default.aspx.
>
> ==================================================
>
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
DetailsView - Changing to Insert Mode (Not By Using the Insert Button In the DetailsView Control) Feege ASP .Net Web Controls 0 12-20-2005 12:28 AM
DetailsView field value at runtime Stu ASP .Net 1 10-10-2005 01:17 PM



Advertisments