CS代码:string g_strPosition="2nd Page";aspx代码:                    <select id="selPosition" name="selPosition">
                        <option>1st Page</option>
                        <option>2nd Page</option>
                        <option>3rd Page</option>
                        <option>4th Page</option>
                        <option>5th Page</option>
                    </select>
想根据strPosition变量的值在前台的下拉框selPosition的某一项选中,有什么好方法?

解决方案 »

  1.   

    这个取后台的值 var a='<%= strPosition%>'
    剩下的就是js给select选中值操作了。
      

  2.   

    是想后台选中吗??若是则在前台加runat ="server":
    <select id="selPosition" name="selPosition" runat ="server">
    后台设置:
            for (int i = 0; i < selPosition.Items.Count; i++)
            {
                if (selPosition.Items[i].Text == "2nd Page")
                    selPosition.SelectedIndex = i;
            }
      

  3.   

    <html>
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
          function test()
          {
             var g_strPosition="2nd Page";          
             var s = document.getElementById("selPosition");
             for(var i=0;i<s.length;i++)
             {
               if(s.options[i].text == g_strPosition)
               {
                 s.options[i].selected=true;
                 break;
                }
              }
          }
          window.onload=test;
        </script>
    </head>
    <body>
        
        <form id="form1" runat="server">
     <select id="selPosition" name="selPosition">
                            <option>1st Page</option>
                            <option>2nd Page</option>
                            <option>3rd Page</option>
                            <option>4th Page</option>
                            <option>5th Page</option>
                        </select>
        
        </form>
    </body>
    </html>
      

  4.   

    用事件加循环:
     protected void ddlSerchTable_SelectedIndexChanged(object sender, EventArgs e)
    {
       for(int i =0;i<ddlddlSerchTable.Items.count;i++)
    {
         if (ddlddlSerchTable.Items[i].Text.Contains(g_strPosition)
                    {
                        ddlddlSerchTable.Items[i].Selected = true;
                        return;
                    }}
    }
    }
      

  5.   

    设置strPosition为protected或public
    var sel="<%= strPosition%>" ;其他的就用js吧
      

  6.   

    如果在后台给g_strPosition赋值,则改为
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
          function test()
          {
             var g_strPosition='<%=g_strPosition %>';          
             var s = document.getElementById("selPosition");
             for(var i=0;i<s.length;i++)
             {
               if(s.options[i].text == g_strPosition)
               {
                 s.options[i].selected=true;
                 break;
                }
              }
          }    
          window.onload=test;  
        </script>
    </head>
    <body>
        
        <form id="form1" runat="server">
     <select id="selPosition" name="selPosition">
                            <option>1st Page</option>
                            <option>2nd Page</option>
                            <option>3rd Page</option>
                            <option>4th Page</option>
                            <option>5th Page</option>
                        </select>
        
        </form>
    </body>
    </html>
    后台: protected string g_strPosition = "2nd Page";
      

  7.   

    后台写一个方法
    public String isSelected(String seed, String value) 
    {
    if (seed.trim().equals(value)) 
    {
    return "selected";
    } else {
    return "";
    }
    }
    前台调用 <select id="selPosition" name="selPosition">
                            <option  <%=isSelected(g_strPosition,"1st Page")%>>1st Page</option>
                            <option  <%=isSelected(g_strPosition,"2st Page")%>>2nd Page</option>
                            <option  <%=isSelected(g_strPosition,"3st Page")%>>3rd Page</option>
                            <option  <%=isSelected(g_strPosition,"4st Page")%>>4th Page</option>
                            <option  <%=isSelected(g_strPosition,"5st Page")%>>5th Page</option>
                        </select>
      

  8.   

    谢谢大家参与,我感觉还是用JS操作比较好:
    js:
    function SelectPosition(strPosition) {
       var index=parseInt(strPosition); //可以将1234bcd将返回1234
        var t = document.getElementById("selPosition");
        t.options[index-1].selected=true; 
    }
    aspx:
    <script type="text/javascript"><%=g_strScript %></script>
    cs:
    g_strPosition="2nd Page";
    g_strScript += string.Format(" SelectPosition('{0}');",g_strPosition);