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

L

LehrSJ

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
 
S

Steven Cheng[MSFT]

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.
 
L

LehrSJ

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


Steven Cheng said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top