Question

I have a asp.net site in which I have two files that need to talk to one another. Below is a snippet of code from my footer.ascx file. I need to send a string to the MobileAd.ascx.cs file. Below is my relevant code from each file.

I believe everything is set up right I just have no clue how to pass the value properly. The value that is not being sent properly is SendA.value

Here is a snippet from 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>

Here is the receiving end at MobileAd.ascx.cs

private string _AdType;

public string AdType
{
    set
    {
        this._AdType = value;
    }
}

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

    if (!string.IsNullOrEmpty(_AdType))
    {
        if (_AdType == "mobile")
        {
            html = "Mobile Ad Code";
        }
        else
        {
            html = "Tablet or Desktop Ad Code";
        }
        divHtml.InnerHtml = html;
    }
Was it helpful?

Solution

You are detecting the user-agent with javascript. But, being a server control, the MobileAd.ascx got executed before javascript could execute. You should do it on server-side by checking Request.UserAgent or Request.Browser.IsMobileDevice. If the sole purpose of the property AdType is to just hold user-agent type, you can remove it and try modifying your Page_Load method like this:

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;    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top