如何传递查询字符串变量?

时间:2013-08-07 12:30:59

标签: c# asp.net

我想在链接按钮单击上将类变量从1页传递到另一页。链接按钮单击事件在javascript查询中写入如下:

<script type="text/javascript">
        function RedirectTo() {
            window.location.href = 'ApplyJobsByCandidate.aspx';
            return false;
        }

现在我如何通过查询字符串传递类变量?我想要像'ApplyJobsByCandidate.aspx?id='+Class1.ID;

这样的东西

请帮帮我。

6 个答案:

答案 0 :(得分:1)

您可以将值传入函数,然后将其添加到查询字符串

<script type="text/javascript">
        function RedirectTo(id) {
            window.location.href = 'ApplyJobsByCandidate.aspx?id=' + id;
            return false;
        }

答案 1 :(得分:0)

中的代码

    public string ss
    {
        get
        {
            return ViewState["ss"].ToString();
        }
        set
        {
            ViewState["ss"] = value;
        }
    }

在某些方法中设置ss值,如

ss = Class1.ID

在javascript中

  <script type="text/javascript">
    function RedirectTo() {
        window.location.href = 'ApplyJobsByCandidate.id=' + '<%= ss %>';
        return false;
    }
</script>

答案 2 :(得分:0)

尝试

<script type="text/javascript">
    function RedirectTo() {
        window.location.href = 'ApplyJobsByCandidate.aspx?Id=' +Id;
        return false;
    }
</script>

在C#代码中

   if (Request.QueryString["id"] != null) {
        try
        {
            id = int.Parse(Request.QueryString["id"]);
        }
        catch
        {
            // deal with it
        }
    }

答案 3 :(得分:0)

如果某个类中有public static属性,那么您可以像这样访问它;

window.location.href = '<%=string.format("ApplyJobsByCandidate.aspx?id={0}", MyClass.id)%> ';

答案 4 :(得分:0)

将类值设置为隐藏字段并发送该值的另一​​种方法

Js代码:

var getValue= $("#hiddenField1").val();
window.location.href = 'ApplyJobsByCandidate.aspx?id=' + getValue;

代码背后: On ApplyJobsByCandidate.aspx.cs

if(Request.QueryString["id"] != null)
{
string fetch_id = Request.QueryString["id"];
}

答案 5 :(得分:0)

如果您可以访问页面中的课程,则可以这样做。

<script type="text/javascript">
        function RedirectTo() {
            window.location.href = 'ApplyJobsByCandidate.aspx?id=<% =Class1.ID %>';
            return false;
        }