我有一个FormView应返回0,1或2行。当FormView当前视图是ReadOnly(在PageLoad上)或Edit(在postBack上)时,DataItemCount是正确的。但是在Insert视图中(在postBack上),DataItemCount的数量始终为0.如何在Insert视图中保留正确的行数(在postBack上)?
内容页面
<asp:FormView ID="FormView3" ViewStateMode="Enabled"
OnDatabound="FormView3_DataBound"
runat="server"
DataKeyNames="IDeditorLanguage"
DataSourceID="SqlDataSource_languages">
<EditItemTemplate>
html stuff
</EditItemTemplate>
<InsertItemTemplate>
html stuff
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this?');"/>
<asp:LinkButton class="btn btn-default" ID="NewButton"
runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:label runat="server" ID="languagesNumer"></asp:label>
背后的代码
protected void FormView3_DataBound(object sender, EventArgs e)
{
if (FormView3.DataItemCount == 0)
{
languagesNumer.Text = "The number is 0";
}
else if (FormView3.DataItemCount == 1)
{
languagesNumer.Text = "The number is 1";
}
else if (FormView3.DataItemCount == 2)
{
languagesNumer.Text = "The number is 2";
}
}
这就是我解决问题的方法:
protected void FormView3_DataBound(object sender, EventArgs e)
{
if (FormView3.CurrentMode == FormViewMode.ReadOnly)
{
numberLanguages = FormView3.DataItemCount;
}
if (numberLanguages == 0)
{
languagesNumer.Text = "The number is 0";
}
else if (numberLanguages == 1)
{
languagesNumer.Text = "The number is 1";
}
else if (numberLanguages == 2)
{
languagesNumer.Text = "The number is 2";
}
}