请问javascript怎么获取用户控件中的控件ID

时间:2021-06-22 00:58:24
比如我在SelectValue.ascx文件中注册javascript代码

 

 

        protected void ASPxButtonEdit1_ButtonClick(object source, DevExpress.Web.ASPxEditors.ButtonEditClickEventArgs e)
        {
            string strScript = "<script language='JavaScript' type='Text/JavaScript'>";
            strScript += "var str=window.showModalDialog('default2.aspx','','dialogWidth:500px;dialogHeight:400px','scroll:yes');";
            strScript += "if(str!=null){document.Form1." + this.ASPxButtonEdit1.ClientID + ".value=str;";
            strScript += "}";
            strScript += "</script>";
            Page.RegisterStartupScript("ASPxButtonEdit1", strScript);
        }

 

 

这时总是提示找不到tSelectValue1_ASPxButtonEdit1对象。这问题要怎么解决??谢谢!!

11 个解决方案

#1


eval()

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 

#2


eval()

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 

#3


引用 1 楼 showrock 的回复:
eval() 

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 



不行哦。。还是同样的提示错误!

#4


strScript += "if(str!=null){document.getElementById('" + this.ASPxButtonEdit1.ClientID + "').value=str;"; 

#5


但是你的 ASPxButtonEdit1 是什么呢?在客户端输出为什么?为他的客户端value属性设置值有什么意义?

你贴出浏览器上查出的源代码中 ASPxButtonEdit1 这个输出的html架构看看。

#6


就是document.getElementById()了,括号中是你需要的ID,后面的属性是什么就写什么,JS中找不到也可以

#7


不会吧?获取“用户控件的ID”?用户控件 解析后就相当于把控件中的代码贴到使用位置,哪来什么ID?
如:
usercontrol1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="usercontrol1.ascx.cs" Inherits="usercontrol1" %>
<div>用户控件示例</div>

webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="webform1.aspx.cs" Inherits="webform1" %>
<%@ Register Src="usercontrol1.ascx" TagName="usercontrol1" TagPrefix="uc1" %>

<div id="ucdiv"><uc1:usercontrol1 id="usercontrol1" runat="server"></uc1:usercontrol1></div>

浏览器查看webform1.aspx源码:
<div id="ucdiv"><div>用户控件示例</div></div>

#8


document.getElementById()取到相应控件

#9


引用 4 楼 sp1234 的回复:
strScript += "if(str!=null){document.getElementById('" + this.ASPxButtonEdit1.ClientID + "').value=str;"; 


不好意思哦,刚吃饭去了。。这是解析后的HTML代码,有点长。ASPxButtonEdit是别人开发的第三方组件,一个输入框旁边有一个小按钮,用于弹出选择项。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><link rel="stylesheet" type="text/css" href="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLubkPrpwPNrqXc1SVSc_xgaGkkqLpCTqDCqBnM95H0icQ2&amp;t=633622836000000000" />
<title>
无标题页
</title></head>
<body>
    <form name="form1" method="post" action="WebForm2.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTI1ODI2ODE5D2QWAgIDD2QWAgIBD2QWAmYPPCsABQEADxYEHg5fIVVzZVZpZXdTdGF0ZWceBVZhbHVlZWRkZJkGXClzpthYQpNbKJiawwSUWk3X" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


    <div>
    
        <input type="hidden"/><script id="dxis_994204488" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhc8q-Y3Cik3AInKr6bhf-ce_1uU4-tQerQEBo9VGEM5M_h0mIyI89_0FQUZGGHWyTO34H281XRDRNOtALA8PLaJjdmZQEIbaPt2qmCxN0st5u0&amp;t=633622836040000000" type="text/javascript"></script>
<script id="dxis_60042388" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLs8nepzGR2xzTwT3tRXd80nF0pUHvCqs8AaUTfWH3Jphg2&amp;t=633622836000000000" type="text/javascript"></script>
<script id="dxis_1734465408" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLtBKhlVDeT0S0b_C2NFH0KQMY1rTbp33ieDTUVlLwFpbw2&amp;t=633622836000000000" type="text/javascript"></script>
<table class="dxeButtonEdit" cellspacing="1" cellpadding="0" id="SelectValue1_ASPxButtonEdit1" border="0" style="border-collapse:separate;">
<tr>
<td class="dxic" style="width:100%;"><input class="dxeEditArea" id="SelectValue1_ASPxButtonEdit1_I" name="SelectValue1$ASPxButtonEdit1" type="text" style="background-color:Transparent;border-width:0px;width:100%;padding-left:0px;padding-right:0px;padding-top:0px;padding-bottom:0px;margin-left:1px;margin-right:1px;margin-top:0px;margin-bottom:1px;" /></td><td id="SelectValue1_ASPxButtonEdit1_B0" class="dxeButtonEditButton" onclick="aspxBEClick('SelectValue1_ASPxButtonEdit1',0)"><table class="dxbebt" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;border-collapse:separate;">
<tr>
<td class="dx"><img id="SelectValue1_ASPxButtonEdit1_B0Img" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLtjg_kpE1YglXNRhKdstw9ST0ONlE4fTk8z7VQn4ROOJw2&amp;t=633622836000000000" alt="..." title="" style="border-width:0px;" /></td>
</tr>
</table></td>
</tr>
</table><script id="dxss_1405206744" type="text/javascript">
<!--
aspxAddHoverItems('SelectValue1_ASPxButtonEdit1',[['dxeButtonEditButtonHover','',['B0']]]);
aspxAddPressedItems('SelectValue1_ASPxButtonEdit1',[['dxeButtonEditButtonPressed','',['B0']]]);

var dxo = new ASPxClientButtonEdit('SelectValue1_ASPxButtonEdit1');
window.SelectValue1_ASPxButtonEdit1 = dxo;
dxo.uniqueID = 'SelectValue1$ASPxButtonEdit1';
dxo.renderIFrameForPopupElements = true;
dxo.RegisterServerEventAssigned(['ButtonClick']);
dxo.widthCorrectionRequired = true;
dxo.buttonCount = 1;
dxo.InlineInitialize();

//-->
</script>

    
    </div>
    
<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLPyrerDQKCjMawBwLUu4i6BFdsOS8g5OeD5N3I2UrtgAKniJW5" />
</div>
<script language='JavaScript' type='Text/JavaScript'>var str=window.showModalDialog('default2.aspx','','dialogWidth:500px;dialogHeight:400px','scroll:yes');if(str!=null){eval(SelectValue1_ASPxButtonEdit1.value)=str;}</script></form>
</body>
</html>

#10


其实把用户控件里面的代码直接copy到.aspx页里,是可以实现功能的,主要可能是因为在用户控件里,所以ASPxButtonEdit的ID值已经变了。

#11


谢谢大家,我已经找到答案了

#1


eval()

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 

#2


eval()

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 

#3


引用 1 楼 showrock 的回复:
eval() 

strScript += "if(str!=null){eval(document.Form1." + this.ASPxButtonEdit1.ClientID + ".value)=str;"; 



不行哦。。还是同样的提示错误!

#4


strScript += "if(str!=null){document.getElementById('" + this.ASPxButtonEdit1.ClientID + "').value=str;"; 

#5


但是你的 ASPxButtonEdit1 是什么呢?在客户端输出为什么?为他的客户端value属性设置值有什么意义?

你贴出浏览器上查出的源代码中 ASPxButtonEdit1 这个输出的html架构看看。

#6


就是document.getElementById()了,括号中是你需要的ID,后面的属性是什么就写什么,JS中找不到也可以

#7


不会吧?获取“用户控件的ID”?用户控件 解析后就相当于把控件中的代码贴到使用位置,哪来什么ID?
如:
usercontrol1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="usercontrol1.ascx.cs" Inherits="usercontrol1" %>
<div>用户控件示例</div>

webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="webform1.aspx.cs" Inherits="webform1" %>
<%@ Register Src="usercontrol1.ascx" TagName="usercontrol1" TagPrefix="uc1" %>

<div id="ucdiv"><uc1:usercontrol1 id="usercontrol1" runat="server"></uc1:usercontrol1></div>

浏览器查看webform1.aspx源码:
<div id="ucdiv"><div>用户控件示例</div></div>

#8


document.getElementById()取到相应控件

#9


引用 4 楼 sp1234 的回复:
strScript += "if(str!=null){document.getElementById('" + this.ASPxButtonEdit1.ClientID + "').value=str;"; 


不好意思哦,刚吃饭去了。。这是解析后的HTML代码,有点长。ASPxButtonEdit是别人开发的第三方组件,一个输入框旁边有一个小按钮,用于弹出选择项。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><link rel="stylesheet" type="text/css" href="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLubkPrpwPNrqXc1SVSc_xgaGkkqLpCTqDCqBnM95H0icQ2&amp;t=633622836000000000" />
<title>
无标题页
</title></head>
<body>
    <form name="form1" method="post" action="WebForm2.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTI1ODI2ODE5D2QWAgIDD2QWAgIBD2QWAmYPPCsABQEADxYEHg5fIVVzZVZpZXdTdGF0ZWceBVZhbHVlZWRkZJkGXClzpthYQpNbKJiawwSUWk3X" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


    <div>
    
        <input type="hidden"/><script id="dxis_994204488" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhc8q-Y3Cik3AInKr6bhf-ce_1uU4-tQerQEBo9VGEM5M_h0mIyI89_0FQUZGGHWyTO34H281XRDRNOtALA8PLaJjdmZQEIbaPt2qmCxN0st5u0&amp;t=633622836040000000" type="text/javascript"></script>
<script id="dxis_60042388" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLs8nepzGR2xzTwT3tRXd80nF0pUHvCqs8AaUTfWH3Jphg2&amp;t=633622836000000000" type="text/javascript"></script>
<script id="dxis_1734465408" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLtBKhlVDeT0S0b_C2NFH0KQMY1rTbp33ieDTUVlLwFpbw2&amp;t=633622836000000000" type="text/javascript"></script>
<table class="dxeButtonEdit" cellspacing="1" cellpadding="0" id="SelectValue1_ASPxButtonEdit1" border="0" style="border-collapse:separate;">
<tr>
<td class="dxic" style="width:100%;"><input class="dxeEditArea" id="SelectValue1_ASPxButtonEdit1_I" name="SelectValue1$ASPxButtonEdit1" type="text" style="background-color:Transparent;border-width:0px;width:100%;padding-left:0px;padding-right:0px;padding-top:0px;padding-bottom:0px;margin-left:1px;margin-right:1px;margin-top:0px;margin-bottom:1px;" /></td><td id="SelectValue1_ASPxButtonEdit1_B0" class="dxeButtonEditButton" onclick="aspxBEClick('SelectValue1_ASPxButtonEdit1',0)"><table class="dxbebt" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;border-collapse:separate;">
<tr>
<td class="dx"><img id="SelectValue1_ASPxButtonEdit1_B0Img" src="/WebResource.axd?d=HSlWOufhfgDtxz2KB6Zhcx8ity7961g8ToAuDNBJhiaPClXOVIpa1ZeDbBU0CqFHsvxVYSiOwLuD0DGcxtLfSB3X7CDyOx1LWwrbqXcYiLtjg_kpE1YglXNRhKdstw9ST0ONlE4fTk8z7VQn4ROOJw2&amp;t=633622836000000000" alt="..." title="" style="border-width:0px;" /></td>
</tr>
</table></td>
</tr>
</table><script id="dxss_1405206744" type="text/javascript">
<!--
aspxAddHoverItems('SelectValue1_ASPxButtonEdit1',[['dxeButtonEditButtonHover','',['B0']]]);
aspxAddPressedItems('SelectValue1_ASPxButtonEdit1',[['dxeButtonEditButtonPressed','',['B0']]]);

var dxo = new ASPxClientButtonEdit('SelectValue1_ASPxButtonEdit1');
window.SelectValue1_ASPxButtonEdit1 = dxo;
dxo.uniqueID = 'SelectValue1$ASPxButtonEdit1';
dxo.renderIFrameForPopupElements = true;
dxo.RegisterServerEventAssigned(['ButtonClick']);
dxo.widthCorrectionRequired = true;
dxo.buttonCount = 1;
dxo.InlineInitialize();

//-->
</script>

    
    </div>
    
<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLPyrerDQKCjMawBwLUu4i6BFdsOS8g5OeD5N3I2UrtgAKniJW5" />
</div>
<script language='JavaScript' type='Text/JavaScript'>var str=window.showModalDialog('default2.aspx','','dialogWidth:500px;dialogHeight:400px','scroll:yes');if(str!=null){eval(SelectValue1_ASPxButtonEdit1.value)=str;}</script></form>
</body>
</html>

#10


其实把用户控件里面的代码直接copy到.aspx页里,是可以实现功能的,主要可能是因为在用户控件里,所以ASPxButtonEdit的ID值已经变了。

#11


谢谢大家,我已经找到答案了