我在gridview中有两个级联下拉列表。 gridview绑定到数据表。在添加级联下拉列表之前,我每行都有文本框 - 这很好用。现在,在我尝试向gridview添加另一行后,项目崩溃了。我尝试使用Ajax更新面板。我尝试放置3个面板 - Gridview的每个ItemTemplate中有一个面板。添加这些Ajax更新面板后,页面将无法加载,因为它无法从gridview页脚中找到该按钮。
我该怎么办?我想要启用验证。
无效的回发或回调参数。事件验证已启用 在配置中使用或<%@ Page EnableEventValidation =“true”%>在一个页面中。为了安全 目的,此功能验证回发或回调的参数 事件源自最初呈现的服务器控件 他们。如果数据有效且预期,请使用 ClientScriptManager.RegisterForEventValidation方法 注册回发或回调数据以进行验证。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("ItemType", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Job", typeof(string)));
dt.Columns.Add(new DataColumn("UnitPrice", typeof(string)));
dt.Columns.Add(new DataColumn("LineTotal", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Quantity"] = string.Empty;
dr["ItemType"] = string.Empty;
dr["Description"] = string.Empty;
dr["Job"] = string.Empty;
dr["UnitPrice"] = string.Empty;
dr["LineTotal"] = "$0.00";
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
gvPO.DataSource = dt;
gvPO.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
DataRow drNewRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)gvPO.Rows[rowIndex].Cells[1].FindControl("QuantityTextBox");
DropDownList dropdwn1 = (DropDownList)gvPO.Rows[rowIndex].Cells[2].FindControl("ddlItemType");
DropDownList dropdwn2 = (DropDownList)gvPO.Rows[rowIndex].Cells[2].FindControl("ddlItems");
TextBox box4 = (TextBox)gvPO.Rows[rowIndex].Cells[4].FindControl("JobTextBox");
TextBox box5 = (TextBox)gvPO.Rows[rowIndex].Cells[5].FindControl("UnitPriceTextBox");
Label lbl1 = (Label)gvPO.Rows[rowIndex].Cells[6].FindControl("LineTotalLabel");
try
{
double dbl1 = 0.0;
double dbl2 = 0.0;
bool bResult1 = false;
bool bResult2 = false;
String str1 = box1.Text;
String str2 = box5.Text;
bResult1 = (double.TryParse(str1, out dbl1));
bResult2 = (double.TryParse(str2, out dbl2));
dbl2 = dbl1 * dbl2;
if (bResult1 && bResult2)
{
lbl1.Text = String.Format("{0:C}", dbl2);
lbl1.Visible = true;
}
}
catch (Exception except)
{
lbl1.Text = "$0.00";
lbl1.Visible = true;
String strError = "<br /><span style=\"color:red; font-weight:bold\">";
strError += except.ToString();
strError += "</span><br />";
Response.Write(strError);
}
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Quantity"] = box1.Text;
drCurrentRow["ItemType"] = dropdwn1.SelectedValue.ToString();
drCurrentRow["Description"] = dropdwn2.SelectedValue.ToString();
drCurrentRow["Job"] = box4.Text;
drCurrentRow["UnitPrice"] = box5.Text;
drCurrentRow["LineTotal"] = lbl1.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
gvPO.DataSource = dtCurrentTable;
gvPO.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)gvPO.Rows[rowIndex].Cells[1].FindControl("QuantityTextBox");
DropDownList dropdwn1 = (DropDownList)gvPO.Rows[rowIndex].Cells[2].FindControl("ddlItemType");
DropDownList dropdwn2 = (DropDownList)gvPO.Rows[rowIndex].Cells[2].FindControl("ddlItems");
TextBox box4 = (TextBox)gvPO.Rows[rowIndex].Cells[4].FindControl("JobTextBox");
TextBox box5 = (TextBox)gvPO.Rows[rowIndex].Cells[5].FindControl("UnitPriceTextBox");
Label lbl1 = (Label)gvPO.Rows[rowIndex].Cells[6].FindControl("LineTotalLabel");
box1.Text = dt.Rows[i]["Quantity"].ToString();
dropdwn1.SelectedItem.Value = dt.Rows[i]["ItemType"].ToString();
dropdwn2.SelectedItem.Value = dt.Rows[i]["Description"].ToString();
box4.Text = dt.Rows[i]["Job"].ToString();
box5.Text = dt.Rows[i]["UnitPrice"].ToString();
lbl1.Text = dt.Rows[i]["LineTotal"].ToString();
rowIndex++;
}
}
}
}
<asp:GridView ID="gvPO" runat="server" ShowFooter="True" AutoGenerateColumns="False" OnDataBound="gvPO_DataBound" Width="800px" >
<AlternatingRowStyle BackColor="#E2E8F3" Width="100%" />
<EmptyDataTemplate>
Empty Grid
</EmptyDataTemplate>
<HeaderStyle CssClass="trBackLightBlueAlternate" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" >
<ItemStyle Width="0%" />
</asp:BoundField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="QuantityTextBox" runat="server" CssClass="txtbox" MaxLength="4" Width="100%" ></asp:TextBox>
<asp:FilteredTextBoxExtender ID="FiltTextBoxExtQuantity" runat="server" TargetControlID="QuantityTextBox" FilterType="Numbers"></asp:FilteredTextBoxExtender>
</ItemTemplate>
<ItemStyle Width="8%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ITEM TYPE">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanelAjax" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlItemType" runat="server" AppendDataBoundItems="True"></asp:DropDownList>
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="ddlItemType" ServicePath="~/ItemList.asmx"
ServiceMethod="GetItemTypes" LoadingText="[loading...]" PromptText="[select item type]" Category="ItemTypeID"></asp:CascadingDropDown>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<ItemStyle Width="22%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="DESCRIPTION">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanelAjax2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlItems" runat="server" AppendDataBoundItems="True" AutoPostBack="True"></asp:DropDownList>
<asp:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlItems" ServicePath="~/ItemList.asmx"
ServiceMethod="GetItemsForItemTypes" ParentControlID="ddlItemType" LoadingText="[loading...]"
PromptText="[select item]" Category="Description">
</asp:CascadingDropDown>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="40%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="JOB">
<ItemTemplate>
<asp:TextBox ID="JobTextBox" runat="server" CssClass="txtbox" Width="100%" ></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="UNIT PRICE">
<ItemTemplate>
<asp:TextBox ID="UnitPriceTextBox" runat="server" CssClass="txtbox" Width="100%" ></asp:TextBox>
<asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="UnitPriceTextBox" ValidChars="1234567890.$"></asp:FilteredTextBoxExtender>
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LINE TOTAL">
<ItemTemplate>
<asp:Label ID="LineTotalLabel" runat="server" Width="100%" ></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="ADD NEW ROW" CssClass="btnStyle1" OnClick="ButtonAdd_Click" Width="100%" CausesValidation="True" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>