SelectedIndexChanged 过滤 ascx 控件
我使用数据绑定下拉列表来填充带有项目迭代的组合框,并使用 ascx 控件来显示标签云。我正在检索下拉列表的 selectedValue 并将其存储为会话以过滤掉标签云(对于整个项目或通过迭代)。我收到错误,因为我输入的默认值无法转换为整数。预先感谢您的帮助!
filteroptions.Items.Insert(0, "Entire Project");
ASP.NET 文件:
protected void filteroptions_SelectedIndexChanged(object sender, EventArgs e)
{
string selected_iteration = filteroptions.SelectedValue;
Session["iteration"] = selected_iteration;
}
ASCX 控制:
protected void Page_Load(object sender, EventArgs e)
{
proj_name = Request.QueryString["project"].ToString();
proj_id = Request.QueryString["id"].ToString();
iteration = (string)Session["iteration"];
BindTagCloud();
}
private void BindTagCloud()
{
int pro_id = Convert.ToInt32(proj_id);
int iteration_id = Convert.ToInt32(iteration);
....
if (iteration_id != 0)
{
ListView1.DataSource = tagCloudNegativeIteration;
ListView1.DataBind();
ListView2.DataSource = tagCloudPositiveIteration;
ListView2.DataBind();
}
else
{
ListView1.DataSource = tagCloudNegative;
ListView1.DataBind();
ListView2.DataSource = tagCloudPositive;
ListView2.DataBind();
}
}
I am using a databound dropdown list to populate a combobox with project iterations and an ascx control to display a tag cloud. I am retrieving the selectedValue of the dropdown and storing it as a session to filter out the tag cloud (for the entire project or by iteration). I am getting an error, because the default value which I have entered cannot be then converted to an integer. Thanks in advance for your help!
filteroptions.Items.Insert(0, "Entire Project");
ASP.NET FILE:
protected void filteroptions_SelectedIndexChanged(object sender, EventArgs e)
{
string selected_iteration = filteroptions.SelectedValue;
Session["iteration"] = selected_iteration;
}
ASCX CONTROL:
protected void Page_Load(object sender, EventArgs e)
{
proj_name = Request.QueryString["project"].ToString();
proj_id = Request.QueryString["id"].ToString();
iteration = (string)Session["iteration"];
BindTagCloud();
}
private void BindTagCloud()
{
int pro_id = Convert.ToInt32(proj_id);
int iteration_id = Convert.ToInt32(iteration);
....
if (iteration_id != 0)
{
ListView1.DataSource = tagCloudNegativeIteration;
ListView1.DataBind();
ListView2.DataSource = tagCloudPositiveIteration;
ListView2.DataBind();
}
else
{
ListView1.DataSource = tagCloudNegative;
ListView1.DataBind();
ListView2.DataSource = tagCloudPositive;
ListView2.DataBind();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您没有存储整数值。此代码:
filteroptions.Items.Insert(0, "Entire Project");
可能没有执行您认为它正在执行的操作。这不是说“添加一个键为 0 且文本为“整个项目”的新列表项。相反,它是说在 位置 0 处插入一个带有值的新列表项和“整个项目”的文本,
您可能需要类似
filteroptions.Items.Insert(0, new ListItem("Entire Project", "0"));
Well, you're not storing an integer value. This code:
filteroptions.Items.Insert(0, "Entire Project");
is probably not doing what you think it is doing. This is not saying "add a new listitem with a key of 0 and the text "Entire Project". Instead, it is saying insert a new listitem at Position 0 with Value and Text of "Entire Project"
you probably want something like,
filteroptions.Items.Insert(0, new ListItem("Entire Project", "0"));
问题是您在初始加载时将迭代值设置为 null。如果由于任何原因您的会话变量变为空,您可以使用此代码始终回退到默认值。您可能希望将迭代变量设置为整数,以便可以在负载中对其进行转换。
并按照阿奎那的建议改变添加项目的方式。
The problem is that you are setting the iteration value to null on the initial load. You can use this code to always fall back to a default if for any reason your session variable becomes null. You might want to make your iteration variable an integer so you can convert it in your load.
And change the way your adding items to what aquinas suggested.
使用这个..
默认值是存储在 session["iteration"] 中的值,如果没有存储任何值,则使用 null 作为默认值。
use this..
And default value is the value that is stored in session["iteration"] if no value is stored in that then use null as defaultvalue.