Javascript 通过另一个ASP控件发送ASP HiddenField值

Javascript 通过另一个ASP控件发送ASP HiddenField值,javascript,asp.net,.net,ektron,Javascript,Asp.net,.net,Ektron,我有一个asp.net站点,其中有两个文件需要相互通信。 下面是我的footer.ascx文件中的一段代码。我需要向MobileAd.ascx.cs文件发送一个字符串。下面是我在每个文件中的相关代码 我相信一切都设置正确,我只是不知道如何正确传递值。未正确发送的值为SendA.value 下面是footer.ascx中的一个片段 <%@ Register TagPrefix="PSG" TagName="MobileAd" Src="~/MobileAd.ascx" %> <

我有一个asp.net站点,其中有两个文件需要相互通信。 下面是我的footer.ascx文件中的一段代码。我需要向MobileAd.ascx.cs文件发送一个字符串。下面是我在每个文件中的相关代码

我相信一切都设置正确,我只是不知道如何正确传递值。未正确发送的值为SendA.value

下面是footer.ascx中的一个片段

<%@ Register TagPrefix="PSG" TagName="MobileAd" Src="~/MobileAd.ascx" %>

<asp:HiddenField runat="server" ID="SendA" value="" />

<script>
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ||
(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.platform))) 
{
    document.getElementById('<%=SendA.ClientID%>').value = "mobile";
}   
else
{
    document.getElementById('<%=SendA.ClientID%>').value = "other";
}
</script>

<div class="bottom" align="center"> 
    <PSG:MobileAd ID="MobileAd" runat="server" AdType = <%=SendA.value%> />    
</div>

您正在使用javascript检测用户代理。但是,作为一个服务器控件,
MobileAd.ascx
在javascript能够执行之前就被执行了。您应该在服务器端通过检查
Request.UserAgent
Request.Browser.IsMobileDevice
来完成此操作。如果属性
AdType
的唯一目的只是保存用户代理类型,则可以删除它并尝试修改
Page\u Load
方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{       
    string html = null;

    if (Request.Browser.IsMobileDevice)
    {
        html = "Mobile Ad Code";
    }
    else
    {
        html = "Tablet or Desktop Ad Code";
    }

    divHtml.InnerHtml = html;    
}

谢谢,这是我目前为止的测试结果!我还没有机会在平板电脑上进行测试,但我祈求好运。
protected void Page_Load(object sender, EventArgs e)
{       
    string html = null;

    if (Request.Browser.IsMobileDevice)
    {
        html = "Mobile Ad Code";
    }
    else
    {
        html = "Tablet or Desktop Ad Code";
    }

    divHtml.InnerHtml = html;    
}