在asp.net中,按钮可以有回发PostBackUrl (例如,通过指定一个url --我过去曾用它来截断查询字符串参数--只需指定页面url而不带任何参数)。只需一个按钮,这就非常简单了。
有没有人知道使用下拉列表最好的方法是什么?如果我指定AutoPostBack (当选择更改时回发),似乎没有一种简单的方法来修改回发url (即不带查询字符串参数的回发到页面)。
我猜可能是使用javascript进行自定义回发...但是,有没有更好的方法--像asp.net按钮中那样的属性,而我没有呢?
发布于 2011-09-09 21:17:45
不,DropDownList没有属性。您可以使用Response.Redirect方法重定向用户,并使用Session collection在请求之间持久化数据。
发布于 2015-01-28 18:21:40
DropDownList没有该属性,但您可以使用一些技巧将此功能添加到页面中。首先,让我描述一种情况,为什么这个属性对您来说是必要的:
因此,以下是解决方案:
namespace myspace
{
public partial class EmployeePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//now you should get the correct url
//you can generate it right here but i prefer to use a special method to
//ensure that this url will be the same in all places of my code
string emptyEmpIdPostbackUrl = Utils.GetEmployeePageURL("");
//now call the main method
Utils.CreatePostbackUrl(this, "SetFilterUrl", emptyEmpIdPostbackUrl,
new List<WebControl> { ddlFilterCompany, ddlFilterDepartment, ddlFilterOwner,
ddlFilterType, ddlFilterDiscarded, ddlFilterChangeDate });
if (!IsPostBack)
{
...
}
}
...
}
public static class Utils
{
//page - your gridview page
//name - some custom name to ensure that different postbacks will work independently from each other
//url - the url with empty employee id
//controls - list of webcontrols for which you want to create postback url (i've got 6 dropdownlists on my own page)
public static void CreatePostbackUrl(Page page, string name, string url, List<WebControl> controls)
{
//create a hidden button with your postbackurl
Button btn = new Button();
btn.ID = name;
btn.PostBackUrl = url;
btn.Attributes.Add("style", "display: none;");
page.Form.Controls.Add(btn);
//register javascript that will simulate click on the hidden button
page.ClientScript.RegisterClientScriptBlock(page.GetType(), name + "Script",
"<script type=\"text/javascript\"> function " + name + "() {" +
"var btn = document.getElementById('" + btn.ClientID + "'); " +
"if (btn) btn.click();} </script>", false);
//and link this script to each dropdownlist in the list
foreach (WebControl ctrl in controls)
{
string attrName = "";
if (ctrl is DropDownList)
attrName = "onchange";
if (attrName != "")
ctrl.Attributes.Add(attrName, name + "()");
}
}
public static string GetEmployeePageURL(string empId)
{
return "emp.aspx" +
"?empid=" + empId;
}
}
}完成这些操作后,您将获得一个带有隐藏按钮的页面和一堆将链接到此按钮并共享其PostBackUrl属性的get控件。
发布于 2014-07-08 08:02:58
如果您想要直接发布到另一个页面,可以尝试使用隐藏按钮方法
<asp:DropDownList ID="lstMyDropDown" runat="server" ClientIDMode="Static" onchange="javascript:$get('btnHidden').click(); ">
<asp:ListItem Value="0" Text="Some Value 1" />
<asp:ListItem Value="1" Text="Some Value 2" />
</asp:DropDownList>
<asp:Button ID="btnHidden" runat="server" ClientIDMode="Static" PostBackUrl="~/myProcessingPage.aspx" OnClientClick="javascript:if($get('lstPrinterModel').selectedIndex < 1){return false;}" style="display:none" />https://stackoverflow.com/questions/7362092
复制相似问题