ASP.NET下根据QueryString决定使用哪块javascript的两种方法

80酷酷网    80kuku.com

  asp.net在asp下,有时候会使用下面的代码:

<%
if request("m") = "1" then%>
<script language="javascript">
<!--
m_name[0] = "《财经》杂志 ";
//m_period[0] = 6;
init(1, 1, 1);
//-->
</script>
<%
else%>
<script language="javascript">
<!--
m_name[0] = "《财经》杂志 ";
init(0, 1, 1);
//-->
</script>
<%end if%>
<%if request("m") = "2" then%>
<script language="javascript">
<!--
m_name[1] = "《证券市场》周刊(综合刊)";
m_name[2] = "《证券市场》周刊(市场刊)";
init(1, 2, 1);
init(1, 3, 1);
//-->
</script>
<%
else%>
<script language="javascript">
<!--
m_name[1] = "《证券市场》周刊(综合刊)";
m_name[2] = "《证券市场》周刊(市场刊)";
init(0, 2, 1);
init(0, 3, 1);
//-->
</script>
<%end if%>

如果是在ASP.NET下,如何解决呢?

方法1(使用ViewState):(蒙斑竹鸿雪指教,试验成功)

<SCRIPT language="javascript">
            <!--
            
            function howMuch2View(iselect, iview)
            {
                var m1, m2;
                switch(iselect)
                {
                    case 1:
                        m1 = 1;
                        m2 = 0;
                        break;
                    case 2:
                        m1 = 0;
                        m2 = 1;
                        break;
                    default:
                        m1 = 1;
                        m2 = 0;
                        break;
                
                }
                                
                switch(iview)
                    {
                    case 1:
                        m_name[0] = "《财经》杂志 ";
                        //m_period[0] = 6;
                        init(m1, 1, 1);                
                        break;
                    case 3:
                        m_name[0] = "《财经》杂志 ";
                        //m_period[0] = 6;
                        init(m1, 1, 1);
                        
                        m_name[1] = "《证券市场》周刊(综合刊)";
                        m_name[2] = "《证券市场》周刊(市场刊)";
                        init(m2, 2, 1);
                        init(m2, 3, 1);
                    
                        break;
                    default:
                        m_name[0] = "《财经》杂志 ";
                        //m_period[0] = 6;
                        init(m1, 1, 1);                
                        break;
                    break;
                    }
            }
            
            howMuch2View(<%=ViewState["which2Select"]%>,<%=ViewState["howMuch2View"]%>);
            
            
            //-->
                                                </SCRIPT>
------------------------------------
方法2(使用Panel的visible属性):
WebForm:
<asp:panel id="pnlJsInitM1" runat="server" Visible="False">
                            <SCRIPT language="javascript">
    <!--
    m_name[0] = "《财经》杂志 ";
    //m_period[0] = 6;
    init(1, 1, 1);
    //-->
                            </SCRIPT>
</asp:panel>

<asp:Panel id="pnlJsInitM1n" runat="server" Visible="False">
                        <SCRIPT language="javascript">
    <!--
    m_name[0] = "《财经》杂志 ";
    //m_period[0] = 6;
    init(0, 1, 1);
    //-->
    </SCRIPT>
</asp:Panel>

<asp:Panel id="pnlJsInitM2" runat="server" Visible="False">
                            <SCRIPT language="javascript">
    <!--
    m_name[1] = "《证券市场》周刊(综合刊)";
    m_name[2] = "《证券市场》周刊(市场刊)";
    init(1, 2, 1);
    init(1, 3, 1);
    //-->
    </SCRIPT>
</asp:Panel>

<asp:Panel id="pnlJsInitM2n" runat="server" Visible="False">
                            <SCRIPT language="javascript">
    <!--
    m_name[1] = "《证券市场》周刊(综合刊)";
    m_name[2] = "《证券市场》周刊(市场刊)";
    init(0, 2, 1);
    init(0, 3, 1);
    //-->
                        </SCRIPT>
</asp:Panel>
-----------------------------------------------

Codebehind:

string strM;
            try
            {
                strM = Request.QueryString["m"].Trim();
            }
            catch
            {
                strM = "3";
            }


    switch(strM)
    {
    case "1":
                    pnlJsInitM1.Visible = true;
                    pnlJsInitM1n.Visible = false;
                    pnlJsInitM2.Visible = false;
                    //pnlJsInitM2n.Visible = true;
                    pnlJsInitM2n.Visible = false;
    break;

    case "2":
                    pnlJsInitM1.Visible = false;
                    pnlJsInitM1n.Visible = true;
                    pnlJsInitM2.Visible = true;
                    pnlJsInitM2.Visible = false;
                    //pnlJsInitM2n.Visible = false;
    break;

    default :
                    pnlJsInitM1.Visible = false;
                    pnlJsInitM1n.Visible = true;
                    pnlJsInitM2.Visible = false;
                    //pnlJsInitM2n.Visible = true;
                    pnlJsInitM2n.Visible = false;
    break;

    }

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: