asp.net - 更新面板重新加载整个页面 - Ajax 不工作
问题描述
我为我的应用程序使用母版页概念。我在 aspx 页面中有以下代码
我里面有一个gridview和一个文本框,我从codebehind绑定这个gridview(基于db值,我将在客户端有很多行)。每当我在文本框中输入一个值时,整个页面都会加载并且 ajax 不起作用。这段代码有什么错误。我已经在母版页中添加了脚本管理器。它总是加载整个页面。由于它是母版页概念,因此以下代码在 contentplaceholder 内
<tbody>
<asp:UpdatePanel ID="updPnl" UpdateMode="Always" runat="server">
<ContentTemplate>
<asp:GridView ID="grdBOQ" Width="100%" ShowFooter="true" OnRowDataBound="gvBOQDetails_OnRowDataBound" OnRowCommand="grdBOQ_RowCommand" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false" runat="server" CssClass="gvBOQdatatable">
<Columns>
<asp:TemplateField HeaderText="Sl No:">
<ItemTemplate>
<%#(grdBOQ.PageSize * grdBOQ.PageIndex) + grdBOQ.Rows.Count + 1%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="40px" Wrap="False" />
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<div style="text-align: center;">
<%#(grdBOQ.PageSize * grdBOQ.PageIndex) + grdBOQ.Rows.Count + 1%>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item ID" >
<HeaderStyle CssClass="HiddenCol" />
<ItemTemplate>
<asp:Label ID="lblItem_ID" runat="server" Text='<%# Bind("BOQ_ITEM_ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" CssClass="HiddenCol" />
<FooterTemplate>
<div style="text-align: center;">
<asp:Label runat="server" Text='' ID="lblItemID"></asp:Label>
</div>
</FooterTemplate>
<FooterStyle CssClass="HiddenCol" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Section">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("BOQ_SECTION") %>' ID="lblSection"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBOQSection" OnSelectedIndexChanged="ddlSectionItem_SelectedIndexChanged"
onmouseover="this.size=5;" onmouseout="this.size=1;"
Style="max-height: 100px; overflow: auto;" AutoPostBack="true" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Category_Name") %>' ID="lblCategoryName"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" Text='' ID="lblCatName"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category Description">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Description") %>' ID="lblCategoryDescription"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" Text='' ID="lblCatDesc"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" AutoPostBack="true" OnTextChanged="txtQuantity_TextChanged" class="form-control" runat="server" Text='<%# Bind("QUANTITY") %>' Style="width: 80px" MaxLength="8"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center">
<asp:TextBox ID="txtQuantityDyn" class="txtlbl" AutoPostBack="true" OnTextChanged="txtQuantityDyn_TextChanged" ReadOnly="true" Style="width: 80px" Text='0' runat="server" />
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("UOM_Name") %>' ID="lblUnit"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" Text='' ID="lblSectionUnit"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Rate">
<ItemTemplate>
<asp:TextBox ID="lblUnitRate" class="txtlbl" ReadOnly="true" runat="server" Text='<%# Bind("SellingPrice_Per_UOM") %>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxSectionUR" class="txtlbl" ReadOnly="true" Text="0.00" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:TextBox ID="lblTotalPrice" class="txtlbl" ReadOnly="true" runat="server" Text='<%# Bind("TOTAL") %>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxURT" class="txtlbl" ReadOnly="true" Text='0.00' runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New BOQ" CommandArgument="<%# Container.DataItemIndex %>" CommandName="addNew" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</tbody>
</table>
</div>
文本框更改代码
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
if (Session[clsCommon.SESSION_USER_ROLE_ID].ToString() == clsCommon.gConst_UserRole_PM.ToString())
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "OptionHide()", true);
}
if (Session["RowCount"]==null)
{
Session["RowCount"] = grdBOQ.Rows.Count;
}
else
{
int rowCount = grdBOQ.Rows.Count;
}
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;
int rowindex = 0;
rowindex = currentRow.RowIndex;
Label txt = (Label)currentRow.FindControl("lblItem_ID");
int val = Convert.ToInt32(txt.Text);
DataSet dsBOMInvoice = new DataSet();
objInvoiceUser.P_BOQ_ID = val;
dsBOMInvoice = objInvoiceUser.GetBOMValues();
decimal bomQuantity = 0;
decimal bomPrice;
if (btnobcInvoice.Visible == true)
{
clsContractorInvoiceEdit objConInvEdit = new clsContractorInvoiceEdit();
objInvoiceUser.P_BOQ_ID = val;
objInvoiceUser.P_ASSIGN_INVOICE_ORDER_ID = intAssignOrderID;
DataSet dsBOMNew = objInvoiceUser.GetAllPMBOM();
var list = new List<KeyValuePair<string, int>>();
string bom_section;
int bom_new_count = dsBOMNew.Tables[0].Rows.Count;
decimal result = 0.000M;
Session["result"] = "0.00";
if(bom_new_count>=1)
{
bom_section = dsBOMNew.Tables[0].Rows[0]["BOM_SECTION"].ToString().ToLower();
decimal value = Convert.ToDecimal(thisTextBox.Text);
objConInvEdit.P_BOQ = val;
objConInvEdit.P_BOM = bom_section;
objConInvEdit.P_Quantity = value;
objConInvEdit.P_order_ID = intAssignOrderID;
objConInvEdit.P_type = 1;
objConInvEdit.P_mode = 1;
result = objConInvEdit.InsertUPdateConINVEdit();
Session["result"] = result;
}
// Response.Write("<script>alert('Hello');</script>");
}
else
{
Session["result"] = thisTextBox.Text;
}
if (dsBOMInvoice.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in dsBOMInvoice.Tables[0].Rows)
{
var BOQ_ID = dr["BOM_ID"].ToString();
foreach (GridViewRow BOMrow in grdBOM.Rows)
{
Label lblBOMItem_ID = (Label)BOMrow.FindControl("lblBOMItem_ID");
if (lblBOMItem_ID.Text == BOQ_ID)
{
TextBox txtBOMQuantity = (TextBox)BOMrow.FindControl("txtBOMQuantity");
txtBOMQuantity.Text = Session["result"].ToString();
}
}
}
}
decimal ld_tot_tots = 0, TotalbomPrice = 0;
foreach (GridViewRow BOMrows in grdBOM.Rows)
{
decimal ld_tots = 0;
string BOmQuantity = ((TextBox)BOMrows.FindControl("txtBOMQuantity")).Text; //tiru
if (BOmQuantity != string.Empty && BOmQuantity != null && BOmQuantity != "")
{
bomQuantity = decimal.Parse(((TextBox)BOMrows.FindControl("txtBOMQuantity")).Text);
}
//bomQuantity = decimal.Parse(((TextBox)BOMrows.FindControl("txtBOMQuantity")).Text);
bomPrice = decimal.Parse(((TextBox)BOMrows.FindControl("lblBOMUnitRate")).Text);
TotalbomPrice = (bomQuantity * bomPrice);
TextBox totalBOM = (TextBox)BOMrows.FindControl("lblBOMTotalPrice");
totalBOM.Text = TotalbomPrice.ToString();
ld_tots = Convert.ToDecimal(((TextBox)BOMrows.FindControl("lblBOMTotalPrice")).Text);
ld_tot_tots = ld_tot_tots + ld_tots;
}
// grandBOMTotal = Convert.ToDecimal(ld_tot_tots) + Convert.ToDecimal(TotalbomPrice);
lblBOMtotal.Text = Convert.ToDecimal(ld_tot_tots).ToString();
lblGrandtotal.Text = Decimal.Add(Convert.ToDecimal(lblBOMtotal.Text), Convert.ToDecimal(lblBOQTotal.Text)).ToString();
lblFinalGrandTotal.Text = Decimal.Add(Convert.ToDecimal(lblGrandtotal.Text), Convert.ToDecimal(lblAllGrandtotal.Text)).ToString();
//BOQ - first Grid
decimal addQuantity = 0; decimal Quantity = 0; //tiru
decimal price, TotalPrice, addprice = 0;
foreach (GridViewRow gvRow in grdBOQ.Rows)
{
string quantity_val = ((TextBox)gvRow.FindControl("txtQuantity")).Text; //tiru
if (quantity_val != string.Empty && quantity_val != null && quantity_val != "")
{
Quantity = decimal.Parse(((TextBox)gvRow.FindControl("txtQuantity")).Text);
}
//decimal Quantity = decimal.Parse(((TextBox)gvRow.FindControl("txtQuantity")).Text);
price = decimal.Parse(((TextBox)gvRow.FindControl("lblUnitRate")).Text);
addQuantity = Convert.ToInt32(((TextBox)grdBOQ.FooterRow.FindControl("txtQuantityDyn")).Text);
addprice = decimal.Parse(((TextBox)grdBOQ.FooterRow.FindControl("TextBoxSectionUR")).Text);
TextBox total = (TextBox)gvRow.FindControl("lblTotalPrice");
TotalPrice = (Quantity * price);
total.Text = TotalPrice.ToString();
grandBOQTotal = Decimal.Add(Convert.ToDecimal(grandBOQTotal), Convert.ToDecimal(TotalPrice));
}
TotalPrice = (addQuantity * addprice);
grandBOQTotal = Decimal.Add(grandBOQTotal, TotalPrice);
lblBOQTotal.Text = Convert.ToDecimal(grandBOQTotal).ToString();
grandBOQTotal = 0;
lblGrandtotal.Text = Decimal.Add(Convert.ToDecimal(lblBOMtotal.Text), Convert.ToDecimal(lblBOQTotal.Text)).ToString();
lblFinalGrandTotal.Text = Decimal.Add(Convert.ToDecimal(lblGrandtotal.Text), Convert.ToDecimal(lblAllGrandtotal.Text)).ToString();
}
解决方案
我希望解决方案将被删除UpdateMode
,然后我想到的第一个解决方案是:
1-更改UpdateMode
为Conditional
并设置ChildrenAsTriggers
为,false
因此您应该具有以下内容:
<asp:UpdatePanel ID="updPnl" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
<ContentTemplate>
......
<asp:TextBox ID="txtQuantity" AutoPostBack="true" OnTextChanged="txtQuantity_TextChanged" class="form-control" runat="server" Text='<%# Bind("QUANTITY") %>' Style="width: 80px" MaxLength="8"></asp:TextBox>
........
......
2-第二个:如果第一个不起作用,则删除UpdateMode
更新面板并为该文本框定义触发器,以便代码类似于
<asp:UpdatePanel ID="updPnl" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtQuantity" AutoPostBack="true" OnTextChanged="txtQuantity_TextChanged" ....
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtQuantity" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
.......
希望这些对您有所帮助。
推荐阅读
- c++ - 创建一个 C++ 程序以查找给定整数数组中的整数对的数量,其总和等于指定数字
- uwp - 如何获取已安装的 nuget 包信息
- php - 为什么我的提交按钮不能将信息发送到我想要的电子邮件?(PHP)
- cordova - 应用程序和打开的窗口之间的通信
- php - 通过 PHP 生成 Excel 工作表 (CSV)
- typescript - 如何在新的命名空间下使用已经声明的类型?
- java - 在 Android 中使用 ContentProvider+Room 访问数据库的最佳实践是什么?
- netsuite - 如何在 NetSuite 中从 SOAP 请求中设置一个空的自定义国家/地区字段
- c# - 检查给定索引后集合中的所有内容是否为空的最简单方法是什么?
- python - 将 Pandas 系列转换为数据框