ASP.NET DataGrid为空

GWR

我有一个用户控件上的ASP.NET数据网格。我有一个主页,其中添加了用户控件(有时是用户控件的多个副本),并在发生回发时将其还原。

dataGrid具有插入/编辑/删除链接。我可以将用户控件的多个副本添加到页面中,并且插入/编辑删除功能对于每个单独的控件都非常适用。

昨天我使用Text ='<%#DocumentTitle%>'格式向主页添加了一些与用户控件无关的属性绑定。最初,在添加Page.DataBind();之前,我无法使其正常工作。到主页的Page_Load方法。此时属性绑定开始正常工作,但随后我发现插入功能已停止在每个用户控件内的数据网格中工作。...我调试并发现,当执行以下行时,它将在控件内的控件中找到文本字段dataGrid为空或“”:

eInfo.Ref = ((TextBox)gvEG.FooterRow.FindControl("txtEmployeeName")).Text;

如果我从主页上删除page.DataBind()方法,则属性绑定停止工作,但userControl中的dataGrid开始工作。

我的问题有两个:i)为什么看似无关的更改会影响dataGrid,并且ii)我应该怎么做才能使它正常工作?

我在下面添加了代码的相关部分...或者至少我认为是相关的部分。

Default.aspx

    <div class="general">        
    <asp:TextBox Width="488" runat="server" placeholder="Document Title"  Text='<%# DocumentTitle %>'></asp:TextBox>
    </div>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {           
        if (!IsPostBack)
        {
            // Create an empty user control for the first requirements section.
            EmployeeSectionUserControl myUserControl1 = (EmployeeSectionUserControl )LoadControl("~/EmployeeSectionUserControl .ascx");


            // Add it to the panel control holding all the user controls.
            MainPanel.Controls.Add(myUserControl1);

            DocumentTitle = "I am the default document title and I'm bound.";
        }
        else
        {
            // Do nothing
        }            
        Page.DataBind();
    }

EmployeeSectionUserControl.ascx

<asp:GridView ID="gvEG" runat="server" AutoGenerateColumns="False" CssClass="grid"
AlternatingRowStyle-CssClass="gridAltRow" RowStyle-CssClass="gridRow" ShowFooter="True"
EditRowStyle-CssClass="gridEditRow" FooterStyle-CssClass="gridFooterRow" OnRowCancelingEdit="gvEG_RowCancelingEdit"
OnRowCommand="gvEG_RowCommand" OnRowDataBound="gvEG_RowDataBound" OnRowDeleting="gvEG_RowDeleting"
OnRowEditing="gvEG_RowEditing" OnRowUpdating="gvEG_RowUpdating" DataKeyNames="Id" ShowHeaderWhenEmpty="true">
<Columns>
    <asp:TemplateField HeaderText="Id" HeaderStyle-HorizontalAlign="Left" ControlStyle-Width="50px">
        <ItemTemplate>
            <%# Eval("Id")%>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Ref" HeaderStyle-HorizontalAlign="Left" ControlStyle-Width="90px">
        <EditItemTemplate>
            <asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Ref") %>'
                Width="90px"></asp:TextBox>                
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtEmployeeName" runat="server" Width="90px"></asp:TextBox>                
        </FooterTemplate>

EmployeeSectionUserControl.ascx.cs

protected void gvEG_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            employeeInfo eInfo = new employeeInfo();
            eInfo.Id = 999;// Convert.ToInt32(((TextBox)gvEG.FooterRow.FindControl("Id")).Text);

            // If we're inserting from the EmptyDataTemplate( ie an empty table ) of the gridview then we need to retreive the data differently.
            // So we perform a check on the gridView FooterRow and if it's null then we can assume it's an empty table.
            if (gvEG.FooterRow == null)
            {
                TextBox referenceTxtBox = (((Control)e.CommandSource).NamingContainer).FindControl("txtEmployeeName") as TextBox;
                eInfo.Ref = referenceTxtBox.Text;                    
            }
            else
            {                    
                eInfo.Ref = ((TextBox)gvEG.FooterRow.FindControl("txtEmployeeName")).Text;
                eInfo.Need = 
            }

            // Store Update and Re-bind data to grid.
        }           
    }
达克

Page.DataBind()在其子级上调用DataBind,因此它会在文本框中更新DocumentTitle,但也会对网格进行DataBind。我没有在网格中看到像EntityDataSource这样的DataSource设置,因此我假设您自己在代码中智能地检索(和准备)数据并自己设置了DataSource:

gvEg.DataSource = someCollection;
gvEg.DataBind();

在获取时,您正在加载用户控件,并可能通过指定DataSource来调用此DataBind。它将绑定,然后调用Page.DataBind(),这也可能会触发gvEg的另一个DataBind,但由于设置了DataSource,因此它显示了相同的内容。

在背面,您不应该在处理事件之前执行DataBind()。您对Page.DataBind()的调用做到了这一点。它触发一个没有数据源的DataBind()。然后,rowCommand出现并检查页脚中的TextBox,该文本框由于没有元素的DataBind而被清除。

您应该做的是:不应使用Page.DataBind()。如果这样做,则需要在正确设置所有DataSource的同时执行此操作,并且不应在回发期间将其踢出。通常,我不建议您使用它,因为它会使它更加复杂,并且如果您没有正确设置应用程序,那么它可能只会有所帮助。在您的情况下,绝对没有必要。您的textBox是一个服务器控件,不属于任何绑定(GridView,Repeater,ListView)。因此,您的textBox可以立即在后面的代码中使用。因此,您应该:

  1. 给textBox一个可以使用的ID,例如txtDocumentTitle

     <asp:TextBox Width="488" ID="txtDocumentTitle" runat="server" placeholder="Document Title"></asp:TextBox>
    
  2. 将设置DocumentTitle(除非您也需要它)替换为:

     txtDocumentTitle.Text = "I am the default document title and I'm bound.";
    
  3. 删除Page.DataBind();

因此,访问服务器控件也是页面或控件中的属性,因此您可以立即对其进行访问。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

asp.net 参数始终为空

来自分类Dev

asp.net DataGrid编辑

来自分类Dev

asp.Net MVC视图模型在发布时为空

来自分类Dev

Cookie域在ASP.NET 4.5中始终为空

来自分类Dev

IFormFile在Asp.Net Core WebAPI中始终为空

来自分类Dev

ASP NET MVC VB如果searchstring为空,则执行LINQ

来自分类Dev

ASP.NET MVC 5模型绑定列表为空

来自分类Dev

DoPostBack事件后,ASP.NET HiddenField值为空

来自分类Dev

asp.net mvc 5 DropDownListFor始终为空

来自分类Dev

asp .net中的Listview为空时如何隐藏div

来自分类Dev

Cookie域在asp.net 4.5中始终为空

来自分类Dev

ASP.NET GridView在回发时为空

来自分类Dev

Javascript函数中的ASP.net隐藏字段为空

来自分类Dev

ASP.NET MVC 5模型绑定列表为空

来自分类Dev

ASP.Net身份,角色中的用户始终为空

来自分类Dev

使用GraphApi ASP.NET的Facebook生日字段为空

来自分类Dev

JavaScript-AJAX /“数据”为空/asp.net MVC

来自分类Dev

Request.Cookies为空ASP.NET MVC

来自分类Dev

asp.net mvc-5 HttpPostedFileBase 为空

来自分类Dev

ASP.NET - 页面重新加载后 GridView 为空

来自分类Dev

C# .Net Core ASP.Net API 用户为空

来自分类Dev

ASP.NET DataGrid,设置列宽

来自分类Dev

ASP.NET DataGrid绑定列

来自分类Dev

ASP.NET DataGrid ItemDataBound事件

来自分类Dev

如何检查ID为id的输入是否为空。ASP.NET MVC

来自分类Dev

ASP.Net - FileUpload HasFile 始终为 false,FileName 始终为空

来自分类Dev

从ASP.NET Core 3.1升级到ASP.NET 5.0后,User.Claims为空

来自分类Dev

使用asp.net mvc进行剔除4部分模型被发布为空

来自分类Dev

Facebook MVC 5 ASP.NET身份-某些用户的电子邮件为空