简体   繁体   English

FindControl() 总是从 DataList 文本框返回 null

[英]FindControl() always returns null from DataList Textbox

I have 8 items in a SQL table with a Description, ItemNumber, and ImagePath.我在 SQL 表中有 8 个项目,其中包含 Description、ItemNumber 和 ImagePath。

protected void Page_Load(object sender, EventArgs e)
    {
        //fill the datalist for the Signs Table
        string tCallFrom = "Program." + System.Reflection.MethodBase.GetCurrentMethod().Name;
        string tQry = @"SELECT * FROM [js_Signs]";

        DataTable tblSigns = objCommMethods.fReturnTableFromQry(clsDBSelect.enumDBSelect.ProjectMaster, tQry, tCallFrom);

        SignsList.DataSource = tblSigns;
        SignsList.DataBind();
    }

I am using a DataList to show all of the items along with a textbox that will allow a user to input the number of items they want:我正在使用 DataList 显示所有项目以及一个允许用户输入他们想要的项目数量的文本框:

    <asp:DataList ID="SignsList" runat="server" RepeatColumns="4" CellPadding="2" Width="90%" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <th>
                                       <%#DataBinder.Eval(Container.DataItem, "Description") %>
                                    </th>
                                </tr>
                                 <tr>
                                    <th>
                                    <%#DataBinder.Eval(Container.DataItem, "ItemNumber") %>
                                    </th>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Image ID="SignImage" runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "ImagePath") %>' />
                                    </td>
                                </tr>
                                <tr style="text-align: center;">
                                    <td>
                                        <asp:TextBox ID="txtSignQuantity" runat="server" CssClass="txtBox" BackColor="White" Enabled="true" 
                                            type="number" min="0" ToolTip="Please choose quantity of signs needed."/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <p> </p>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
             <div class="btnGroup">
                <div class="btnDiv">
                    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btnSave" Width="90px" Visible="true" OnClick="btnSave_Click"/>
                </div>
                <div class="btnDiv">
                    <asp:Button ID="btnCancel" runat="server" Text="Clear" CssClass="btnCancel" Width="90px" Visible="true" OnClick="btnCancel_Click"/>
                </div>
            </div>

After the saved button is clicked in C# I want to find out the value that a user entered but it is always returning null:在 C# 中单击保存按钮后,我想找出用户输入的值,但它总是返回 null:

protected void btnSave_Click(object sender, EventArgs e)
{
        TextBox txtSignQuantity;
        string tempSignQuantity;
        try
        {
            foreach (DataListItem item in SignsList.Items)
            {
                txtSignQuantity = item.FindControl("txtSignQuantity") as TextBox;
                tempSignQuantity = txtSignQuantity.Text;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }   
}

I also checked and the Count of SignsList.Items was 8, so I know that it is retrieving the correct information.我还检查了 SignsList.Items 的计数为 8,所以我知道它正在检索正确的信息。 Sorry, I've never used a Data List before so I'm not really sure how to go about this...抱歉,我以前从未使用过数据列表,所以我不太确定如何 go 关于这个...

The short answer is that data binding on postback is causing the problem.简短的回答是回发上的数据绑定导致了问题。 Check the Page.IsPostBack property before doing data binding:在进行数据绑定之前检查Page.IsPostBack属性:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //fill the datalist for the Signs Table
        string tCallFrom = "Program." + System.Reflection.MethodBase.GetCurrentMethod().Name;
        string tQry = @"SELECT * FROM [js_Signs]";
        DataTable tblSigns = objCommMethods.fReturnTableFromQry(clsDBSelect.enumDBSelect.ProjectMaster, tQry, tCallFrom);

        SignsList.DataSource = tblSigns;
        SignsList.DataBind();
    }
}

ASP.NET WebForms attempts to abstract away the fact that the control instances that were used to render the page are not the same instances it has when handling postback events. ASP.NET WebForms 试图抽象出这样一个事实,即用于呈现页面的控件实例与处理回发事件时的实例不同。 Data binding compounds the abstraction because it has to create controls in response to the DataBind call, and then on postback, recreate them based on the ViewState it saved.数据绑定复合了抽象,因为它必须创建控件以响应DataBind调用,然后在回发时根据它保存的ViewState重新创建它们。

Initializing controls from ViewState happens on the Init event, so when the Load event is fired later in the page lifecycle and you call DataBind on the control, everything it restored gets wiped out and recreated.ViewState初始化控件发生在Init事件上,因此当在页面生命周期的后期触发Load事件并且您在控件上调用DataBind时,它恢复的所有内容都会被清除并重新创建。

As for why you were getting null , it may have been that the controls were wiped out but not recreated;至于你为什么得到null ,可能是控件被清除但没有重新创建; it may have had to do with other event handlers that didn't get rewired after the second data binding.它可能与在第二次数据绑定后没有重新连接的其他事件处理程序有关。

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

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