为什么没有找到UpdatePanel来更新数据?
本文关键字:更新 数据 UpdatePanel 为什么 | 更新日期: 2023-09-27 18:18:25
我的ASP.net页面中有以下代码:
<asp:UpdatePanel runat="server" ClientIDMode="Static" UpdateMode="Conditional" ID="upClickShowTask">
<ContentTemplate>
<asp:LinkButton ID="lbShowTask" CssClass="btnExport" ClientIDMode="Static" runat="server" Text="Generate Tasks" OnClick="btnFilter_Click"></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="pageTab8" ClientIDMode="Static" runat="server">
<asp:Panel ID="demoCli" ClientIDMode="Static" runat="server" BorderWidth="2" BorderColor="#FF0000">
<asp:UpdatePanel ID="upGenTaskCli" runat="server" UpdateMode="Conditional" ClientIDMode="Static">
<ContentTemplate>
Client: <asp:Label ID="lblCli" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Onboarding Date: <asp:Label ID="lblCliDate" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Contact Information: <asp:Label ID="lblCliCont" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Notes: <asp:Label ID="lblCliNotes" runat="server" ClientIDMode="Static" Text=""></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</asp:Panel>
后台代码:
protected void btnFilter_Click(object sender, EventArgs e)
{
demoCli.Visible = false;
demoSit.Visible = false;
demoPra.Visible = false;
demoPro.Visible = false;
connString = @""; //my connection string
#region queries
string queryCli = @"SELECT
C.OBJECTID
,C.ATTR2815 'Client'
,C.ATTR2881 'Onboarding Date'
,C.ATTR2880 'Contact Information'
,M.MEMO 'Notes'
FROM HSI.RMOBJECTINSTANCE1231 C INNER JOIN HSI.RMMEMO M ON C.MK2879 = M.MEMOID";
string querySit = @"SELECT
S.OBJECTID
,S.ATTR2819 'Site'
,S.FK2820 'RelatedClient'
,S.ATTR2873 'Address'
,S.ATTR2874 'City'
,S.ATTR2875 'State'
,S.ATTR2876 'Zip'
,M.MEMO 'Notes'
,S.ATTR2878 'Onboarding Date'
FROM HSI.RMOBJECTINSTANCE1229 S INNER JOIN HSI.RMMEMO M ON S.MK2877 = M.MEMOID";
string queryPra = @"SELECT
P.OBJECTID
,P.ATTR2817 'Practice'
,P.FK2818 'RelatedSite'
,P.ATTR2882 'Onboarding Date'
,M.MEMO 'Notes'
FROM HSI.RMOBJECTINSTANCE1230 P INNER JOIN HSI.RMMEMO M ON P.FK2818 = M.MEMOID";
string queryPro = @"SELECT
P.OBJECTID
,P.ATTR2919 'Provider'
,P.ATTR2920 'Start Date'
,P.FK2921 'RelatedPractice'
,P.FK2922 'RelatedClient'
FROM HSI.RMOBJECTINSTANCE1249 P";
#endregion
string query = "";
using (SqlConnection conn = new SqlConnection(connString))
{
if (ddlCli.SelectedIndex > 0)
{
try
{
demoCli.Visible = true;
query = queryCli + " WHERE C.ATTR2815 = '" + ddlCli.SelectedValue + "'";
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
lblCli.Text = myDataSet.Tables[0].Rows[0]["Client"].ToString();
lblCliDate.Text = myDataSet.Tables[0].Rows[0]["Onboarding Date"].ToString();
lblCliCont.Text = myDataSet.Tables[0].Rows[0]["Contact Information"].ToString();
lblCliNotes.Text = myDataSet.Tables[0].Rows[0]["Notes"].ToString();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
if (ddlSit.SelectedIndex > 0)
{
try
{
demoSit.Visible = true;
query = querySit + " WHERE S.ATTR2819 = '" + ddlSit.SelectedValue + "'";
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
lblSit.Text = myDataSet.Tables[0].Rows[0]["Site"].ToString();
lblSitRC.Text = myDataSet.Tables[0].Rows[0]["RelatedClient"].ToString();
lblSitAdd.Text = myDataSet.Tables[0].Rows[0]["Address"].ToString();
lblSitCity.Text = myDataSet.Tables[0].Rows[0]["City"].ToString();
lblSitSt.Text = myDataSet.Tables[0].Rows[0]["State"].ToString();
lblSitzip.Text = myDataSet.Tables[0].Rows[0]["Zip"].ToString();
lblSitOnDate.Text = myDataSet.Tables[0].Rows[0]["Onboarding Date"].ToString();
lblSitNotes.Text = myDataSet.Tables[0].Rows[0]["Notes"].ToString();
//upGenTaskCli.Update();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
if (ddlPra.SelectedIndex > 0)
{
try
{
demoPra.Visible = true;
query = queryPra + " WHERE P.ATTR2817 = '" + ddlPra.SelectedValue + "'";
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
lblPra.Text = myDataSet.Tables[0].Rows[0]["Client"].ToString();
lblPraRS.Text = myDataSet.Tables[0].Rows[0]["RelatedSite"].ToString();
lblPraOnDate.Text = myDataSet.Tables[0].Rows[0]["Onboarding Date"].ToString();
lblPraNotes.Text = myDataSet.Tables[0].Rows[0]["Notes"].ToString();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
if (ddlPro.SelectedIndex > 0)
{
try
{
demoPro.Visible = true;
query = queryPro + " WHERE P.ATTR2919 = '" + ddlPro.SelectedValue + "'";
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
lblPro.Text = myDataSet.Tables[0].Rows[0]["Provider"].ToString();
lblProStart.Text = myDataSet.Tables[0].Rows[0]["Start Date"].ToString();
lblProRP.Text = myDataSet.Tables[0].Rows[0]["RelatedPractice"].ToString();
lblProRC.Text = myDataSet.Tables[0].Rows[0]["RelatedClient"].ToString();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
upGenTaskCli.Update();
}
}
我为这些行添加了断点:
lblCli.Text = myDataSet.Tables[0].Rows[0]["Client"].ToString();
lblCliDate.Text = myDataSet.Tables[0].Rows[0]["Onboarding Date"].ToString();
lblCliCont.Text = myDataSet.Tables[0].Rows[0]["Contact Information"].ToString();
lblCliNotes.Text = myDataSet.Tables[0].Rows[0]["Notes"].ToString();
并将鼠标悬停在它们上面,显示文本数据,但upGenTaskCli
没有更新以显示数据。
我收到这个错误:
0x800a139e - Microsoft JScript runtime error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'upGenTaskCli'. If it is being updated dynamically then it must be inside another UpdatePanel.
如何解决这个问题?
因为这是一个JS可见性错误,尝试把你的更新面板在另一个更新面板这样的:
//你的视图应该是这样的
<asp:UpdatePanel ID="pnlParent" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:UpdatePanel ID="upGenTaskCli" runat="server" UpdateMode="Conditional" ClientIDMode="Static">
<ContentTemplate>
Client: <asp:Label ID="lblCli" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Onboarding Date: <asp:Label ID="lblCliDate" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Contact Information: <asp:Label ID="lblCliCont" ClientIDMode="Static" runat="server" Text=""></asp:Label>
<br />
Notes: <asp:Label ID="lblCliNotes" runat="server" ClientIDMode="Static" Text=""></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</contentTemplate>
</updatePanel>
所以在你的代码后面做如下操作:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ScriptManager.RegisterStartupScript
(this, typeof(Page), "UpdateMsg", "$(document).ready(function(){$('#upGenTaskCli').hide();});", true);
}
}
//然后在您的按钮逻辑中,在If子句中添加以下行:
protected void btnFilter_Click(Object sender, EventArgs e)
{
if (ddlCli.SelectedIndex > 0)
{
try
{
query = queryCli + " WHERE C.ATTR2815 = '" + ddlCli.SelectedValue + "'";
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
lblCli.Text = myDataSet.Tables[0].Rows[0]["Client"].ToString();
lblCliDate.Text = myDataSet.Tables[0].Rows[0]["Onboarding Date"].ToString();
lblCliCont.Text = myDataSet.Tables[0].Rows[0]["Contact Information"].ToString();
lblCliNotes.Text = myDataSet.Tables[0].Rows[0]["Notes"].ToString();
}
catch (Exception ex)
{
string error = ex.Message;
}
ScriptManager.RegisterClientScriptBlock
(this, typeof(System.Web.UI.Page), "MyJSFunction", "$('#upGenTaskCli').toggle();", true);
}
//它应该可以工作