简体   繁体   English

无法更改中继器内的文本框的文本/值

[英]Cant change text/value of textbox inside repeater

Hello and thanks for taking your time to help me. 您好,感谢您抽出宝贵时间为我提供帮助。

I'm trying to change the text of a textbox thats located inside my repeater. 我正在尝试更改位于中继器内部的文本框的文本。

<asp:Repeater runat="server" ID="rpCategories">
                        <HeaderTemplate>
                            <ul id="nav_down" class="nav_down">
                        </HeaderTemplate>
                        <ItemTemplate>
                            <li><a href="<%# Eval("ID", "/products.aspx?id={0}") %>"><%# Eval("Title") %></a></li>
                        </ItemTemplate>
                        <FooterTemplate>
                            <li><a href="#"></a></li>
                            <li><a href="#">Contact</a></li>
                            <li><a id="cart_logo"></a>
                                <asp:Panel runat="server" ID="pnlBasket">
                                    <asp:textbox runat="server" id="txtTotalCount" Enabled="false" CssClass="ltTotalCount"></asp:textbox>
                                </asp:Panel>
                            </li>
                            </ul>
                        </FooterTemplate>
                    </asp:Repeater>

It's the asp:textbox with the id="txtTotalCount" that I want to change the text of. 我要更改其文本的是带有id =“ txtTotalCount”的asp:textbox。

Here is my C# code: 这是我的C#代码:

TextBox ltTotalCount = (TextBox)FindControl("lblTotalCount");
ltTotalCount.Text = "1";

But if I run the code I get this error : Object reference not set to an instance of an object. 但是,如果我运行代码,则会出现此错误:对象引用未设置为对象的实例。

Would be so happy if someone could tell me what I'm doing wrong. 如果有人可以告诉我我做错了的事,那会很高兴。

Becuase lblTotalCount is inside a parent control - the repeater, you have to reference it through the repeater. 因为lblTotalCount在父控件内部-转发器,所以您必须通过转发器引用它。

You should be able to just add the id of your repeater before FindControl, like this... 您应该能够在FindControl之前添加中继器的ID,就像这样...

TextBox ltTotalCount = (TextBox)rpCategories.FindControl("lblTotalCount"); TextBox ltTotalCount =(TextBox)rpCategories.FindControl(“ lblTotalCount”);

You have to specify the repeater as the parent control to look for the text box and also since it's repeater its quite possible to have more than one text box with that Id so you have to specify which repeater item to look into like so: 您必须将转发器指定为父控件以查找文本框,并且由于它是转发器,因此很可能有多个带有该ID的文本框,因此您必须指定要查找的转发器项,如下所示:

TextBox ltTotalCount = rpCategories.Items[0].FindControl("txtTotalCount") as TextBox;

This will return the textbox in the first row of the repeater. 这将返回转发器第一行中的文本框。 And you should use the Id value not the CssClass value 而且您应该使用Id值而不是CssClass值

The ID is probably being 'enhanced' to prevent duplicate IDs in the rendered HTML. 该ID可能已被“增强”,以防止在呈现的HTML中出现重复的ID。 You can verify this by looking at the html. 您可以通过查看html来验证这一点。

You can add this to the textbox: ClientIDMode="Static" to make the ID stay the same. 您可以将其添加到文本框: ClientIDMode="Static"以使ID保持不变。

You are getting the error because FindControl is returning a null but you are trying to access it anyway. 您收到错误消息是因为FindControl返回的是null,但是无论如何您都试图访问它。

[Edit] Someone pointed out that this shouldn't work. [编辑]有人指出这不起作用。 I agree, here is what does work: 我同意,这是有效的方法:

Control FooterTemplate = rpCategories.Controls[rpCategories.Controls.Count - 1].Controls[0];

TextBox MyTextBox = FooterTemplate.FindControl("txtTotalCount") as TextBox;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM