在EventHandler中访问RepeaterItem控件

时间:2022-06-01 19:41:15

I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.

我有一个带有转发器的页面。我正在编写一个事件处理程序,以便当用户单击我的WebControl按钮时,所述按钮的事件处理程序使用FindControl遍历转发器中的项目,然后使用某些控件的值。看来,在页面加载后,转发器项目会填充,但是当单击按钮将其发回时,当我遍历转发器项目时,我发现它们都是空的。我不完全理解排序,但我假设它是因为我的迭代代码试图访问尚未设置的RepeaterItems。

The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:

转发器代码在我的OnLoad方法中。除此之外,我让我的事件处理程序在单击后尝试迭代这些项目。这基本上就是我想要做的事情:

protected void MyButton_Click(object sender, EventArgs e)
{
    foreach(RepeaterItem item in MyRepeater.Items)
    {
        MyLabel = (Label)item.FindControl("MyLabel");
    }
}

The button is located in the FooterTemplate of the repeater.

该按钮位于转发器的FooterTemplate中。

<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />

Thanks in advance.

提前致谢。

Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)

编辑:为了澄清,当我尝试做某事时,我得到的确切错误是NullReferenceException,例如,Response.Write(MyLabel.Text)

Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?

编辑:今天进一步研究之后,这就是我理解的情况:转发器在数据回发上是数据绑定的。然后,当我从生成的下拉列表中进行选择并按下我的按钮时,它会再次发回。此时,转发器再次数据绑定到它的初始值。因此,如果我必须回发以获得用户的选择,我如何在按钮的事件处理程序中进行此操作,以便在转发器再次获得数据绑定之前我可以获取所选值?

3 个解决方案

#1


THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.

这个问题,听起来是,你可能会在加载时将数据绑定到转发器,但不首先检查以确保它不是回发。

example:

  1. You request the page. On Load Fires. You bind the data to the repeater.
  2. 您请求页面。加载火灾。您将数据绑定到转发器。

  3. You maniupulate the data in the reapter then click your button
  4. 您在reapter中编制数据然后单击按钮

  5. The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
  6. 页面刷新回发,触发onload事件。数据将反弹到您的转发器,并且所有先前输入的数据都已无效。

  7. the onclick event is triggered and your code tries to retrieve values that no longer exist.
  8. onclick事件被触发,您的代码尝试检索不再存在的值。

Make sure your databinding code in your onLoad event is nested within an postback check

确保onLoad事件中的数据绑定代码嵌套在回发检查中

if (!Page.IsPostBack)
{
   Repeater.DataSource = Datatable;
   Repeater.DataBind();
}

#2


I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.

我见过同样的事情。我不明白为什么,但是在所有事件都被解雇之后,数据实际上并没有被绑定。我最终在类级别上提供了数据源,然后编制索引。

private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
    //populate dataTable
    if (!IsPostBack)
    {
        //databind to repeater
    }
}

protected void Submit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in repeater1.Items)
    {
        DataRow row = myTable.Rows[item.ItemIndex];
    }
}

Ideal? Certainly not but it works.

理想?当然不是,但它的工作原理。

#3


Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.

我没有依赖于OnLoad中的IsPostBack,而是通过在用户选择第一个选项后将转发器的数据绑定放在事件处理程序中而不是依赖OnLoad的IsPostBack来分离所有不同的状态。这有点令人费解,但我想我这次正确的做法。

#1


THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.

这个问题,听起来是,你可能会在加载时将数据绑定到转发器,但不首先检查以确保它不是回发。

example:

  1. You request the page. On Load Fires. You bind the data to the repeater.
  2. 您请求页面。加载火灾。您将数据绑定到转发器。

  3. You maniupulate the data in the reapter then click your button
  4. 您在reapter中编制数据然后单击按钮

  5. The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
  6. 页面刷新回发,触发onload事件。数据将反弹到您的转发器,并且所有先前输入的数据都已无效。

  7. the onclick event is triggered and your code tries to retrieve values that no longer exist.
  8. onclick事件被触发,您的代码尝试检索不再存在的值。

Make sure your databinding code in your onLoad event is nested within an postback check

确保onLoad事件中的数据绑定代码嵌套在回发检查中

if (!Page.IsPostBack)
{
   Repeater.DataSource = Datatable;
   Repeater.DataBind();
}

#2


I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.

我见过同样的事情。我不明白为什么,但是在所有事件都被解雇之后,数据实际上并没有被绑定。我最终在类级别上提供了数据源,然后编制索引。

private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
    //populate dataTable
    if (!IsPostBack)
    {
        //databind to repeater
    }
}

protected void Submit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in repeater1.Items)
    {
        DataRow row = myTable.Rows[item.ItemIndex];
    }
}

Ideal? Certainly not but it works.

理想?当然不是,但它的工作原理。

#3


Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.

我没有依赖于OnLoad中的IsPostBack,而是通过在用户选择第一个选项后将转发器的数据绑定放在事件处理程序中而不是依赖OnLoad的IsPostBack来分离所有不同的状态。这有点令人费解,但我想我这次正确的做法。