ASP.Net:GridView控件和组合框问题 [英] ASP.Net: GridView control and combobox woes

查看:72
本文介绍了ASP.Net:GridView控件和组合框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView控件和Combobox控件,它们都已成功填充到我的Page_Load事件中(在检查IsPostBack == false的块中).

I've got a GridView control and Combobox control that are both successfully populated in my Page_Load event (within a block that checks for IsPostBack == false).

我有一个空按钮'btnClick'事件处理程序,单击该事件处理程序将重新加载页面.GridView和Combobox控件的 EnableViewState 属性都设置为True.我期待和期望的行为是:

I've got an empty button 'btnClick' event handler which will reload the page when clicked. Both the GridView and Combobox controls have their EnableViewState property set to True. The behaviour I was expecting and hoping for was:

  1. 页面将在仍填充GridView控件的情况下重新加载.
  2. 页面将重新加载,并且仍会填充组合框,并且用户选择的项目仍将设置为所选项目.

不幸的是,我得到的行为如下:

Unfortunately, the behaviour I'm getting is as follows:

  1. GridView控件现在为空,不显示任何数据.
  2. 组合框现在为空.

代码如下:

public MyPage()
{
    this.Load += new EventHandler(Page_Load);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        DataAccessObj daObj = new DataAccessObj();

        foreach (DataRow dataRow in daObj.GetAllData())
        {
            ListItem listItem = new ListItem(dataRow.ToString(), dataRow.Id.ToString());
            myCombobox.Items.Add(listItem);
        }

        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    // Do nothing
}

我想做的是允许用户从组合框中选择一个项目.单击提交后,将重新填充GridView(基于所选项目).组合框将保持填充并显示最后选择的项目.

What I would like to do is allow the user to select an item from the Combobox. Upon clicking Submit, the GridView would be repopulated (based upon the selected item). The Combobox will remain populated and show the last selected item.

有人可以帮忙解释我可能要出问题的地方吗?TIA

Can someone help explain where I might be going wrong? TIA

推荐答案

单击按钮时,页面会回发,在页面加载时,如果,则需要进行数据绑定网格,您需要为您的页面加载事件添加条件,例如

When you click your button, the page is posted back, in your page load, if it is a postback you need to databind the grid appropriately you need to add a condition to your page load event like

首先,在btn_click上,您需要使用以下方式存储所选的ID:

Firstly on your btn_click, you need to store the id selected with something like:

if (myCombobox.SelectedItem != null)
    {
        if (int.TryParse(myCombobox.SelectedItem.Value, out reportedById) == false)
        {
            reportedById = 0;
            ViewState["reportedById"] = reportedById; // Need to remember which one was selected
        }
    }

然后在您的帖子背面

    else (IsPostBack)
    {
       if (ViewState["reportedById"]) != null)
    {
       IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(Convert.ToInt32(ViewState["reportedById"]));
       IncidentGrid.DataBind();
myCombobox.SelectedItem.Value = ViewState["reportedById"].ToString(); // set combo

    }

        else
        {
         IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();

            }
    }

这篇关于ASP.Net:GridView控件和组合框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆