意外错误 ASP.net 重复器 FindControl

huangapple go评论62阅读模式
英文:

Unexpected Error ASP.net repeater FindControl

问题

Using the following ItemDataBound 用以下的 ItemDataBound 来计算页面字段的值,但我得到了一个 Unexpected Error(意外错误):

C#:

protected void rptLicenseHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((TextBox)e.Item.FindControl("txLicenseHistoryType")).Text = "Test";
}

ASP.NET:

<asp:Repeater ID="rptLicHistory" runat="server" onitemdatabound="rptLicenseHistory_ItemDataBound">
    <ItemTemplate>
        <div style="display:flex;width: 100%;align-items: center;">
            <div style="width:60px;">License:</div>
            <div style="width:170px;"><asp:TextBox ID="txLicenseHistoryType" runat="server"/></div>
        </div>
    </ItemTemplate>                    
    <SeparatorTemplate>
        <div style="height: 30px;">&nbsp;</div>
    </SeparatorTemplate>
</asp:Repeater>

Textbox ID "txLicenseHistoryType" 应该返回 "Test",但我得到了一个 "Unexpected Error"(意外错误)。对我做错了什么有什么想法吗?

英文:

Using the following ItemDataBound to calculate values for my page fields but I am getting and Unexpected Error:

C#:

protected void rptLicenseHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((TextBox)e.Item.FindControl(&quot;txLicenseHistoryType&quot;)).Text = &quot;Test&quot;;

}

ASP.NET:

&lt;asp:Repeater ID=&quot;rptLicHistory&quot; runat=&quot;server&quot; onitemdatabound=&quot;rptLicenseHistory_ItemDataBound&quot;&gt;
    &lt;ItemTemplate&gt;
        &lt;div style=&quot;display:flex;width: 100%;align-items: center;&quot;&gt;
            &lt;div style=&quot;width:60px;&quot;&gt;License:&lt;/div&gt;
            &lt;div style=&quot;width:170px;&quot;&gt;&lt;asp:TextBox ID=&quot;txLicenseHistoryType&quot; runat=&quot;server&quot;/&gt;&lt;/div&gt;
        &lt;/div&gt;
     &lt;/ItemTemplate&gt;                    
     &lt;SeparatorTemplate&gt;
        &lt;div style=&quot;height: 30px;&quot;&gt;&amp;nbsp;&lt;/div&gt;
     &lt;/SeparatorTemplate&gt;
&lt;/asp:Repeater&gt;

Textbox ID &quot;txLicenseHistoryType&quot; Should return "Test" but I get an Unexpected Error. Any ideas on what I am doing incorrectly?

答案1

得分: 1

你使用了错误的控件事件。大多数控件(即使是一个简单的文本框)都有一个数据绑定事件。

你想要处理每一行,因此你应该使用数据 "item" 事件。

在大多数情况下,对于 GridView、ListView 和其他类似的控件,通常称为 "row data bind" 或类似的事件。

在 Repeater 的情况下,我们希望对每个项目都执行这个操作,因此标记应该是这样的:

<asp:Repeater ID="rptLicHistory" runat="server"
    OnItemDataBound="rptLicHistory_ItemDataBound">
    <ItemTemplate>
        <div style="display: flex; width: 100%; align-items: center;">
            <div style="width: 60px;">License:</div>
            <div style="width: 170px;">
                <asp:TextBox ID="txLicenseHistoryType" runat="server" />
            </div>
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <div style="height: 30px;">&nbsp;</div>
    </SeparatorTemplate>
</asp:Repeater>

注意使用了 OnItemDataBound - 对于 "每个" 项目,这有助于你记住。

所以,现在你的代码后台应该是这样的:

protected void rptLicHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) ||
        (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        Debug.Print($"Working on row = {e.Item.ItemIndex}");
        ((TextBox)e.Item.FindControl("txLicenseHistoryType")).Text = "Test";
    }
}

注意如何测试这是一个 "item" 还是 "alternate row" 项目。原因是你有标题行和其他类型的模板行,所以你必须在代码中确保你只获取和处理行或交替行。

当运行时,上述代码应该如下所示:

意外错误 ASP.net 重复器 FindControl

编辑:获取每行绑定的完整数据行

所以,以 "硬编码" 分配 "test" 为例?

我们可以这样做:

protected void rptLicHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) ||
        (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        DataRowView MyDataRow = (DataRowView)e.Item.DataItem;
        ((TextBox)e.Item.FindControl("txLicenseHistoryType")).Text = MyDataRow["HotelName"].ToString();
    }
}

结果:

意外错误 ASP.net 重复器 FindControl

英文:

You using the wrong control event. Most controls (even a plain text box) has a databind event.

You want to deal with EACH row, and thus you want to use the data "item" event.

In most cases for GridView, ListView and others, it usually called "row data bind" something or other.

In the case of a repeater, we want that even FOR EACH ITEM, and thus the markup should be this:

&lt;asp:Repeater ID=&quot;rptLicHistory&quot; runat=&quot;server&quot;
    OnItemDataBound=&quot;rptLicHistory_ItemDataBound&quot; &gt;
    &lt;ItemTemplate&gt;
        &lt;div style=&quot;display: flex; width: 100%; align-items: center;&quot;&gt;
            &lt;div style=&quot;width: 60px;&quot;&gt;License:&lt;/div&gt;
            &lt;div style=&quot;width: 170px;&quot;&gt;
                &lt;asp:TextBox ID=&quot;txLicenseHistoryType&quot; runat=&quot;server&quot; /&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/ItemTemplate&gt;
    &lt;SeparatorTemplate&gt;
        &lt;div style=&quot;height: 30px;&quot;&gt;&amp;nbsp;&lt;/div&gt;
    &lt;/SeparatorTemplate&gt;
&lt;/asp:Repeater&gt;

Note the use of OnItemDataBound - for "each" item it means to help you remember.

So, now your code behind would be this:

protected void rptLicHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) ||
        (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        Debug.Print($&quot;Working on row = {e.Item.ItemIndex}&quot;);
        ((TextBox)e.Item.FindControl(&quot;txLicenseHistoryType&quot;)).Text = &quot;Test&quot;;
    }
}

Note how you have to test if this is a "item" or a "alternate row" item. The reason is that you have heading rows, and other types of template rows, so you have to in code make sure you ONLY are grabbing + dealing with the row, or alternating row.

The above when run should look like this:

意外错误 ASP.net 重复器 FindControl

Edit: Get full data row for each row binding

So, say in place of "hard coded" assignment of "test"?

We could do this:

    protected void rptLicHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) ||
            (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            DataRowView MyDataRow = (DataRowView)e.Item.DataItem;
            ((TextBox)e.Item.FindControl(&quot;txLicenseHistoryType&quot;)).Text = MyDataRow[&quot;HotelName&quot;].ToString();
        }

Result:

意外错误 ASP.net 重复器 FindControl

huangapple
  • 本文由 发表于 2023年5月21日 22:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定