C# 如何在ASP.net C中使用/不使用CodeBehind将DropDownList中的SelectedValue作为链接按钮中的查询字符串传递#
我已经处理这个问题很长一段时间了,我无法将下拉列表的值传递给链接按钮。评估似乎根本不起作用。 代码如下:C# 如何在ASP.net C中使用/不使用CodeBehind将DropDownList中的SelectedValue作为链接按钮中的查询字符串传递#,c#,asp.net,hyperlink,eval,linkbutton,C#,Asp.net,Hyperlink,Eval,Linkbutton,我已经处理这个问题很长一段时间了,我无法将下拉列表的值传递给链接按钮。评估似乎根本不起作用。 代码如下: <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# string.Format(~/DataView.aspx?id=) + Eval(DropDownList1.SelectedValue) %>' Text="New ECRS"></asp:LinkButton> 当我
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# string.Format(~/DataView.aspx?id=) + Eval(DropDownList1.SelectedValue) %>' Text="New ECRS"></asp:LinkButton>
当我运行上面的代码时,link按钮不起作用,也没有页面重定向。但我在许多论坛上看到,人们给出了上述代码作为答案。我做错什么了吗?提前感谢:)链接按钮用于回发到您的页面。听起来你想链接到一个新的页面,在这种情况下,你应该只使用超链接。此外,您的PostBackUrl内容看起来有点奇怪:
<asp:HyperLink NavigateUrl='<%# string.Format("~/DataView.aspx?id={0}", DropDownList1.SelectedValue) %>' Text="Click here" />
另外,不要忘记对控件或页面进行数据绑定,否则该部件将无法执行任何操作。LinkButton用于回发到您的页面。听起来你想链接到一个新的页面,在这种情况下,你应该只使用超链接。此外,您的PostBackUrl内容看起来有点奇怪:
<asp:HyperLink NavigateUrl='<%# string.Format("~/DataView.aspx?id={0}", DropDownList1.SelectedValue) %>' Text="Click here" />
另外,不要忘记对控件或页面进行数据绑定,否则该部件将无法执行任何操作。您可以使用Response。改为在代码隐藏中重定向:
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" Text="New ECRS"></asp:LinkButton>
您可以在代码隐藏中使用Response.Redirect:
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" Text="New ECRS"></asp:LinkButton>
尝试改用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>
无标题页
$(文档).ready(函数(){
$('#').bind('change',function(e){
$('#').attr(“href”,“DataView.aspx?id=”+$(this.val())
});
});
新ECRS
尝试改用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>
无标题页
$(文档).ready(函数(){
$('#').bind('change',function(e){
$('#').attr(“href”,“DataView.aspx?id=”+$(this.val())
});
});
新ECRS
已尝试执行此操作,但未在页面上创建任何链接。它只显示纯文本。也许我没有正确地进行数据绑定,但这样做不会在页面上创建任何链接。它只显示纯文本。也许我的数据库绑定不好。我想这是最简单的方法。当然链接按钮的数量越多,代码就会越长。您还可以帮助我在GridView中的超链接字段中执行同样的操作。这是一个类似的操作,除了重定向是从Gridview HyperLinkField完成的。谢谢。我想这是最简单的方法。当然链接按钮的数量越多,代码就会越长。您还可以帮助我在GridView中的超链接字段中执行同样的操作。除了从Gridview超链接字段执行重定向之外,它的操作与此类似。