ASPX 组合框回发问题

发布于 2024-12-07 03:09:21 字数 2071 浏览 0 评论 0原文

我创建了一个简单的 ASPX 页面,其中列出了 GridView 中的记录。这些记录是事件列表,其中一列是报告事件的人的 ID。

初始页面显示所有记录,但我想为 ReportedBy 列提供过滤器。我通过允许用户在文本框中输入 ReportedByID 然后单击提交按钮来实现此目的。这将按预期使用过滤视图刷新页面。

该页面的代码如下:

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

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        DataAccessObj daObj = new DataAccessObj();
        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int reportedById = 0;

    if (int.TryParse(txtReportedById.Text, out reportedById) == false)
    {
        reportedById = 0;
    }

    DataAccessObj daObj = new DataAccessObj();
    IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
    IncidentGrid.DataBind();
}

为了使其更加用户友好,我决定添加一个填充有 ReportedBy 名称的下拉框,供用户选择,然后在单击提交按钮时用于过滤。下拉框以名称作为显示项,但值仍应设置为 ID。

我遇到的问题是,我从下拉框中获得的 ID 号始终作为列表的第一个元素出现,而不是用户在单击提交按钮时选择的元素。

此页面的实现代码如下:

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

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

    foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
    {
        ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
        combobox.Items.Add(listItem);
    }

    if (IsPostBack == false)
    {
        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int reportedById = 0;

    if (combobox.SelectedItem != null)
    {
        if (int.TryParse(combobox.SelectedItem.Value, out reportedById) == false)
        {
            reportedById = 0;
        }
    }

    DataAccessObj daObj = new DataAccessObj();
    IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
    IncidentGrid.DataBind();
}

如有任何帮助,我们将不胜感激。 TIA

I've created a simple ASPX page that lists records in a GridView. The records are a list of incidents and one of the columns is the ID of the person who reported the incident.

The initial page shows all records but I would like to provide a filter for the ReportedBy column. I've gotten this working by allowing the user to type in the ReportedByID in a textbox and then clicking on the submit button. This refreshes the page as expected with the filtered view.

The code for this page is as follows:

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

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        DataAccessObj daObj = new DataAccessObj();
        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int reportedById = 0;

    if (int.TryParse(txtReportedById.Text, out reportedById) == false)
    {
        reportedById = 0;
    }

    DataAccessObj daObj = new DataAccessObj();
    IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
    IncidentGrid.DataBind();
}

To make it more user friendly, I decided to add a dropdown box populated with the ReportedBy names for the user to select which would then be used to filter on upon clicking the submit button. The dropdown box has names as the display items but the values should still be set to the IDs.

The problem I have is that the ID number I get from the dropdown box always comes up as the first element of the list rather than the one the user selected at the time they clicked on the submit button.

The code for this page with this implementation is as follows:

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

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

    foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
    {
        ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
        combobox.Items.Add(listItem);
    }

    if (IsPostBack == false)
    {
        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int reportedById = 0;

    if (combobox.SelectedItem != null)
    {
        if (int.TryParse(combobox.SelectedItem.Value, out reportedById) == false)
        {
            reportedById = 0;
        }
    }

    DataAccessObj daObj = new DataAccessObj();
    IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
    IncidentGrid.DataBind();
}

Any help would be gratefully appreciated. TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福不弃 2024-12-14 03:09:21

请记住,对于 WebForms,Page_Load 代码在创建回发的控件的事件处理程序代码之前执行。

您必须在检查回发标志的部分中填充列表,就像对网格所做的那样。

if (IsPostBack == false){
    //bind the combobox
}

否则,在回发时,列表将重新填充并且选择将消失。

Keep in mind that with WebForms the Page_Load code is executed before the event handler code for the control which created the postback.

You have to populate the list in the section where postbacks flags are checked, just like you do with the grid.

if (IsPostBack == false){
    //bind the combobox
}

Otherwise, on a postback, the list will re-populate and the selection will be gone.

音盲 2024-12-14 03:09:21
protected void Page_Load(object sender, EventArgs e)
{   
    if (!Page.IsPostBack)
    {
        DataAccessObj daObj = new DataAccessObj();

        foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
        {
            ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
            combobox.Items.Add(listItem);
        }

        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}
protected void Page_Load(object sender, EventArgs e)
{   
    if (!Page.IsPostBack)
    {
        DataAccessObj daObj = new DataAccessObj();

        foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
        {
            ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
            combobox.Items.Add(listItem);
        }

        IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
        IncidentGrid.DataBind();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文