更新不会反映在更新面板内的转发器中

时间:2017-06-30 06:33:37

标签: asp.net updatepanel repeater

我在更新面板中有一个转发器,如下所示

 <form id="frmQuestion" action="AddQuestion.aspx">
            <div class="panel">
            <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
              <div class="panel-body" style="max-height: 420;overflow-y: scroll; border:1px solid Blue">
        <asp:Repeater ID="rptQuestions" runat="server">
            <HeaderTemplate>
                <table class="table" cellpadding="5px" cellspacing="10px" border="solid" id="tableQuestion">
                    <tr style="color: Black; background-color: Teal; border: solid;">
                        <th style="width: 20px">No.</th>
                        <th style="width: 100px">Question</th>
                        <th style="width: 40px">QuestionType</th>
                        <th style="width: 110px">Text Answer</th>
                        <th style="width: 100px">Multiple Choice 1</th>
                        <th style="width: 100px">Multiple Choice 2</th>
                        <th style="width: 100px">Multiple Choice 3</th>
                        <th style="width: 100px">Multiple Choice 4</th>
                        <th style="width: 70px">True/False</th>
                        <th>Controls</th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr style="color: Black; background-color: Silver; border: solid;" id="trdata" class="trclass">

                    <td id="no">
                        <asp:Label runat="server" ID="lblNo" Text='<%#Eval("Qno") %>' /></td>

                    <td id="que">
                        <asp:Label runat="server" ID="lblQuestion" Text='<%#Eval("Question") %>'></asp:Label>
                        <asp:TextBox runat="server" ID="txtQuestion" Text='<%#Eval("Question") %>' Visible="false"></asp:TextBox>
                        <asp:Label runat="server" ID="lblQid" Text='<%#Eval("Qid") %>'></asp:Label></td>

                    <td>
                        <asp:Label runat="server" ID="lblQtype" Text='<%#Eval("Qtype") %>'></asp:Label>
                        <asp:DropDownList runat="server" ID="ddlQType" Visible="false"></asp:DropDownList>
                        <%-- <select class="dropdown" id="ddlQType" name="ddlQType" style="width:100px" runat="server">
                                        <option value="0">--Select--</option>
                                        <option value="1">Descriptive Answer</option>
                                        <option value="2">Multiple Choice </option>
                                        <option value="3">True/False answer</option></select>--%></td>

                    <td>
                        <asp:Label runat="server" ID="lblAnsDesc" Text='<%#Eval("description") %>' Width="100px"></asp:Label>
                        <asp:TextBox runat="server" ID="txtAnsDesc" Text='<%#Eval("description") %>' Width="100px" Visible="false"></asp:TextBox></td>

                    <td>
                        <asp:Label runat="server" ID="lblch1" Text='<%#Eval("choice1") %>' Width="100px"></asp:Label>
                        <asp:TextBox runat="server" ID="txtch1" Text='<%#Eval("choice1") %>' Width="100px" Visible="false"></asp:TextBox></td>

                    <td>
                        <asp:Label runat="server" ID="lblch2" Text='<%#Eval("choice2") %>' Width="100px"></asp:Label>
                        <asp:TextBox runat="server" ID="txtch2" Text='<%#Eval("choice2") %>' Width="100px" Visible="false"></asp:TextBox></td>

                    <td>
                        <asp:Label runat="server" ID="lblch3" Text='<%#Eval("choice3") %>' Width="100px"></asp:Label>
                        <asp:TextBox runat="server" ID="txtch3" Text='<%#Eval("choice3") %>' Width="100px" Visible="false"></asp:TextBox></td>

                    <td>
                        <asp:Label runat="server" ID="lblch4" Text='<%#Eval("choice4") %>' Width="100px"></asp:Label>
                        <asp:TextBox runat="server" ID="txtch4" Text='<%#Eval("choice4") %>' Width="100px" Visible="false"></asp:TextBox>
                        <asp:Label runat="server" ID="lblCorrect" Text='<%#Eval("correct") %>' /></td>

                    <td>
                        <asp:Label runat="server" ID="lblisTrue" Text='<%# Eval("isTrue") %>' Width="70px"></asp:Label>
                        <asp:CheckBox runat="server" ID="chkistrue" Checked='<%#(Eval("isTrue")=="true") %>' Width="70px" Visible="false" /></td>

                    <td><span class="more">
                        <asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" data-toggle="modal" data-target="#myModal" OnClientClick="getdata();" CausesValidation="false" />
                        <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" OnClick="OnDelete" CausesValidation="false" OnClientClick="return confirm('Do you want to delete this row?');" />
                    </span></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

我的问题是当我更新转发器内的任何记录时。更新记录,更新数据库表,获取的数据表显示更新。 但该页面未显示更新。刷新页面时,会反映更新。我不知道是什么导致了这个问题。用于更新的服务器代码是

protected void BindRepeater(int tid)
{
    try
    {

        cmd = new SqlCommand("SP_GetQuestions", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Tid", tid);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();

        sda.Fill(dt);
        //connection.Close();
        if (dt.Rows.Count > 0)
        {
            //adding qno to dt
            dt.Columns.Add("Qno", typeof(System.Int32));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Qno"] = i + 1;
            }
            rptQuestions.DataSource = dt;
            rptQuestions.DataBind();
        }
    }

    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

protected Question getData()
{
    Question question = new Question();
    question.questDesc = txtQuestionDesc.Text;
    question.ansdesc = null;
    question.ch1 = null;
    question.ch2 = null;
    question.ch3 = null;
    question.ch4 = null;
    question.correct = null;
    //question.isTrue = null;
    //question.qid = null;
    //question.qtype = null;
    question.qtype = Convert.ToInt32(ddlQType.Value);
    switch (question.qtype)
    {
        case 1:
            question.ansdesc = textareaAns.InnerText.ToString();
            break;
        case 2:
            question.ch1 = txtMulti1.Text;
            question.ch2 = txtMulti2.Text;
            question.ch3 = txtMulti3.Text;
            question.ch4 = txtMulti4.Text;
            question.correct = "";
            if (chkmulti1.Checked == true)
                question.correct += "1";
            if (chkmulti2.Checked == true)
                question.correct += "2";
            if (chkmulti3.Checked == true)
                question.correct += "3";
            if (chkmulti4.Checked == true)
                question.correct += "4";
            break;
        case 3:
            if (rbtrue.Checked == true)
                question.isTrue = true;
            if (rbfalse.Checked == true)
                question.isTrue = false;
            break;
    }
    return question;
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    Question question = getData();
    question.qid = Convert.ToInt32(txtQuestid.Text.ToString());
    cmd = new SqlCommand("SP_UpdateQuestion", connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@question", question.questDesc);
    cmd.Parameters.AddWithValue("@qid", question.qid);
    cmd.Parameters.AddWithValue("@ans", question.ansdesc);
    cmd.Parameters.AddWithValue("@ch1", question.ch1);
    cmd.Parameters.AddWithValue("@ch2", question.ch2);
    cmd.Parameters.AddWithValue("@ch3", question.ch3);
    cmd.Parameters.AddWithValue("@ch4", question.ch4);
    cmd.Parameters.AddWithValue("@correct", question.correct);
    cmd.Parameters.AddWithValue("@isTrue", question.isTrue);
    cmd.Parameters.AddWithValue("@qtype", question.qtype);
    try
    {
        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
        BindRepeater(Convert.ToInt32(Session["tid"]));
        Response.Write("<script>alert('Question updated successfully!');</script>");
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

请告诉我如何解决这个问题。我甚至将更新面板的updateMode属性保持为'conditional'但没有结果。

1 个答案:

答案 0 :(得分:0)

您是否检查过控制台是否有错误?因为您将Response.WriteUpdatePanel一起使用。这将给您以下错误

  

错误:Sys.WebForms.PageRequestManagerParserErrorException:The   无法解析从服务器收到的消息。

因此,请删除所有Response.Write代码,如果要显示JavaScript警报,请使用ScriptManager

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showAlert", "alert('Question updated successfully!');", true);