English 中文(简体)
SelectedIndexChanged以筛选ascx控件
原标题:SelectedIndexChanged to filter ascx control

我使用一个数据绑定的下拉列表来填充一个包含项目迭代的组合框,并使用一个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();

    }
}
最佳回答

问题是在初始加载时将迭代值设置为null。如果您的会话变量由于任何原因变为null,您可以使用此代码始终返回到默认值。您可能希望将迭代变量设为整数,以便在负载中转换它。

if(String.IsNullOrEmpty(Sesssion["iteration"])
     iteration = "0";
else
     iteration = Session["iteration"]

并改变你添加物品的方式。

问题回答

好吧,你不是在存储一个整数值。此代码:

<code>filteroptions.Items.Insert(0,“整个项目”)

可能没有做你认为它正在做的事情。这不是说“添加一个新的列表项,键为0,文本为“整个项目”。相反,它说在位置插入一个新列表项,值和文本为“完整项目”

你可能想要这样的东西,

<code>filteroptions.Items.Insert(0,new ListItem(“整个项目”,“0”))

使用这个。。

if(Session["iteration"] == Defaultvalue)
  itereation = "0";
else
  iteration = (string)Session["iteration"]; 

默认值是存储在会话[“迭代”]中的值,如果没有存储任何值,则使用null作为默认值。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!