Folks,
I have some simple Javascript here that needs to reference not only a
CheckBox object being passed as a parameter, but also various other
fields on my web page.
My main question is HOW do I access/manipulate controls with
client-side script ?
I know how to access the value of the Checkbox because I pass it to my
javascript function like this : QuoteConv(this) . However, since Asp
2.0 renames all the ID properties of my web controls, what's the best
way to reference all the various controls (i.e. html TextBox, checkbox,
radio, etc) ???
Here's the beginning of my aspx page with the client-side javascript :
<%@ Page Language="C#" ... %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
function QuoteConv(chk) {
/* I'M TAKING A GUESS HERE !!! */
cCurr1 = document.aspnetForm.EditDropDownCur1.value ;
cCurr2 = document.aspnetForm.EditDropDownCur2.value ;
if (document.aspnetForm.hidInvert.value == "Off") {
cQteCur1 = cCurr1;
cQteCur2 = cCurr2;
} else {
cQteCur1 = cCurr2;
cQteCur2 = cCurr1;
}
document.aspnetForm.txtQuoteConv1.value = cQteCur1;
document.aspnetForm.txtQuoteConv1.value = cQteCur2;
return;
}
</script>
and some controls on the EditItemTemplate that I need to
access/manipulate :
<asp:RadioButtonList ID="radioBuySell" runat="server"
SelectedValue='<%# Bind("buy_sell")%>'
>
<asp:ListItem Value="1">Buy</asp:ListItem>
<asp:ListItem Value="2">Sell</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="EditDropDownCur1"
DataSourceID="ObjectDataSource2" ... />
<asp:ListItem ... />
<asp:TextBox ID="txtEditAmount" ... />
<asp:CheckBox ID="chkEditAct_Cur" runat=...
onClick="QuoteConv(this);" Checked='<%
#Eval("act_cur").ToString()=="1" ? false : true %>' />
<input id="txtQuoteConv1" value="USA" readonly="readonly" ... />
<input id="txtQuoteConv2" value="JPY" readonly="readonly" ... />
.
.
.
I know I can do all this stuff through server-side code, but that would
require AutoPostBack="True" on those controls I need to either validate
or manipulate.
Please help...
Sincerely,
Bob
![]() |
0 |
![]() |
First, I would recommend that you never reference controls like this:
document.aspnetForm.EditDropDownCur1.value ;
You should use
document.getElementById( "EditDropDownCur1" ).value;
What I have done for getting to the client side controls of a ItemRepeater (or actually any control in an INamingContainer ). I am not sure if this work in every scenario, but it works for me reasonably well.
In any given row, the same prefix gets applied to all the IDs. I use this function to get the prefix that is shared by all controls in the same row.
function GetBaseControlID( control )
{
var id = control.id;
var sa = id.split( "_" );
sa.length = sa.length - 1;
id = sa.join( "_" );
return id;
}
So in my click handler I can reference all of the "sibling" controls.
function QuoteConv( cbControl )
{
var baseID = GetBaseControlID( cbControl );
var ddl = document.getElementById( baseID + "_EditDropDownCur1" );
var txt = document.getElementById( baseID + "_txtEditAmount" );
}
bill
![]() |
0 |
![]() |
I usually drop aliases to controls like:
Page.RegisterClientScriptBlock("DDLAliases", "var DDLAlias = '" + EditDropDownCur1.ClientID +"';</script>");
And then use the alias in your javascript:
var objDDL = document.getElementById(DDLAlias);
cCurr1 = objDDL.value;
![]() |
0 |
![]() |
Great ideas everyone.
I've tried document.getElementById(), but always run into a road block on that .
I like the Alias idea. I will experiment with it.
Thanks !
Bob
![]() |
0 |
![]() |
I notice that several suggest accessing a web control directly such as EditDropDownCur1.ClientID; however, without getting a handle to it via the FindControl() method I always get the "EditDrop... does not exists in this context" error.
This works though :
CheckBox chkInvert = (CheckBox)FormView1.FindControl("chkEditInvert"); // get handle
StringBuilder cJavaScriptCode = new StringBuilder();
cJavaScriptCode.Append("<SCRIPT Language='JavaScript'>");
cJavaScriptCode.Append("var chkInvertAlias = " + chkInvert.ClientID);
cJavaScriptCode.Append("</SCRIPT>");
ClientScript.RegisterClientScriptBlock(this.GetType(),"chkInvertAlias",
cJavaScriptCode.ToString());
Thanks again.
Bob
![]() |
0 |
![]() |