如何使用/不使用ASP.net C#中的CodeBehind将DropDownList中的SelectedValue作为查询字符串传递给LinkBut​​ton#

时间:2022-12-02 10:09:07

I've been fiddling with this issue for quite sometime now where I am not able to pass the value of a DropDownList to a Link Button. The Eval doesn't seem to work at all. Here's the code:

我一直在摆弄这个问题很长一段时间,因为我无法将DropDownList的值传递给Link Button。 Eval似乎根本不起作用。这是代码:

<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# string.Format(~/DataView.aspx?id=) + Eval(DropDownList1.SelectedValue) %>' Text="New ECRS"></asp:LinkButton>

When I run the above code, the link button doesn't function and there's no page redirection. But I see in many forums that people have given the above code as the answer. Am I doing something wrong? Thanks in advance :)

当我运行上面的代码时,链接按钮不起作用,并且没有页面重定向。但我在很多论坛上都看到人们已将上述代码作为答案。难道我做错了什么?提前致谢 :)

3 个解决方案

#1


3  

You could use Response.Redirect in the code behind instead:

您可以在后面的代码中使用Response.Redirect:

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" Text="New ECRS"></asp:LinkButton>

And in your code-behind:

在您的代码隐藏中:

void LinkButton_Click(Object sender, EventArgs 
{
   Response.Redirect("~/DataView.aspx?id=" + DropDownList1.SelectedValue, true);
}

#2


1  

LinkButton is meant to do a postback to your page. It sounds like you want to link to a new page in which case you should just use HyperLink. Also your PostBackUrl content looks a bit strange:

LinkBut​​ton旨在对您的页面进行回发。听起来您想链接到新页面,在这种情况下您应该只使用HyperLink。你的PostBackUrl内容看起来有点奇怪:

<asp:HyperLink NavigateUrl='<%# string.Format("~/DataView.aspx?id={0}", DropDownList1.SelectedValue) %>' Text="Click here" />

Also, don't forget to DataBind the control or page otherwise the <%# %> part wont do anything.

另外,不要忘记DataBind控件或页面,否则<%#%>部分不会做任何事情。

#3


1  

Try using Jquery instead. See my code below.

请尝试使用Jquery。请参阅下面的代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>
        $(document).ready(function() {
            $('#<%=DropDownList1.ClientID%>').bind('change', function(e) {
            $('#<%=HyperLink1.ClientID%>').attr("href", "DataView.aspx?id=" + $(this).val())
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Selected="True" Text="One" Value="One"> </asp:ListItem>
            <asp:ListItem  Text="Two" Value="Two"> </asp:ListItem>
        </asp:DropDownList>

        <asp:HyperLink ID="HyperLink1" NavigateUrl="DataView.aspx?id=One" runat="server">New ECRS</asp:HyperLink>
    </div>
    </form>
</body>
</html>

#1


3  

You could use Response.Redirect in the code behind instead:

您可以在后面的代码中使用Response.Redirect:

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" Text="New ECRS"></asp:LinkButton>

And in your code-behind:

在您的代码隐藏中:

void LinkButton_Click(Object sender, EventArgs 
{
   Response.Redirect("~/DataView.aspx?id=" + DropDownList1.SelectedValue, true);
}

#2


1  

LinkButton is meant to do a postback to your page. It sounds like you want to link to a new page in which case you should just use HyperLink. Also your PostBackUrl content looks a bit strange:

LinkBut​​ton旨在对您的页面进行回发。听起来您想链接到新页面,在这种情况下您应该只使用HyperLink。你的PostBackUrl内容看起来有点奇怪:

<asp:HyperLink NavigateUrl='<%# string.Format("~/DataView.aspx?id={0}", DropDownList1.SelectedValue) %>' Text="Click here" />

Also, don't forget to DataBind the control or page otherwise the <%# %> part wont do anything.

另外,不要忘记DataBind控件或页面,否则<%#%>部分不会做任何事情。

#3


1  

Try using Jquery instead. See my code below.

请尝试使用Jquery。请参阅下面的代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>
        $(document).ready(function() {
            $('#<%=DropDownList1.ClientID%>').bind('change', function(e) {
            $('#<%=HyperLink1.ClientID%>').attr("href", "DataView.aspx?id=" + $(this).val())
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Selected="True" Text="One" Value="One"> </asp:ListItem>
            <asp:ListItem  Text="Two" Value="Two"> </asp:ListItem>
        </asp:DropDownList>

        <asp:HyperLink ID="HyperLink1" NavigateUrl="DataView.aspx?id=One" runat="server">New ECRS</asp:HyperLink>
    </div>
    </form>
</body>
</html>