方法未在AsyncPostBack触发器中使用udpatePanel进入

时间:2017-07-05 00:21:09

标签: c# asp.net asp.net-ajax updatepanel

我已经完成了这项工作,但我收回了这个项目,我很难理解为什么现在asp updatepanel没有进行部分更新。我希望这取决于下拉列表中的选择:" incType",下一个下拉列表:" incidentName"显示从sql查询获得的一些listitems。它通过做一个糟糕的回发来工作,但是我希望它能够以AsyncPostBack的方式工作,很少有单词不刷新页面,只有那部分。

前端

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>

<asp:UpdatePanel ID="updatePanelIncidentNam" runat="server"
    UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
        <!-- Select input-->
        <div class="form-group">
            <label class="col-md-4 control-label">Category Incident</label>

            <div class="col-xs-6 col-md-4 selectContainer">
                <div class="input-group inputGroupContainer">
                    <span class="input-group-addon"><i class="glyphicon 
                        glyphicon-user"></i></span>
                    <asp:DropDownList CssClass="form-control selectPicker" 
                        name="incType" ID="incType" runat="server" 
                        OnSelectedIndexChanged="incType_SelectedIndexChanged" 
                        AutoPostBack="true">
                    </asp:DropDownList>
                </div>
            </div>
        </div>

        <!-- Select input-->
        <div class="form-group">
            <label class="col-md-4 control-label">Incident Name</label>

            <div class="col-xs-6 col-md-4 selectContainer">
                <div class="input-group inputGroupContainer">
                    <span class="input-group-addon"><i class="glyphicon 
                        glyphicon-user"></i></span>
                    <asp:DropDownList CssClass="form-control selectPicker" 
                        name="incidentName" ID="incidentName" 
                        runat="server" AutoPostBack="false">
                    </asp:DropDownList>
                </div>
             </div>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="incType" 
            EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

背后的代码

protected void incType_SelectedIndexChanged(object sender, EventArgs e)
{
    incidentName.Items.Clear();
    incType.Items[0].Attributes.Add("disabled", "disabled");

    string ConnectionString3 = 
        ConfigurationManager.ConnectionStrings["incidentDB"].ConnectionString;
    SqlConnection pmConn3 = new SqlConnection(ConnectionString3);
    pmConn3.Open();

    string typeSelected = incType.SelectedValue;

    try
    {
        //querying after index changed from category incident changes
        //querying for site and building data
        string sqlStatement = "SELECT type FROM [dbo].[udt_I&AInvestigation_IncidentType] WHERE category=@category";
        SqlCommand cmd = new SqlCommand(sqlStatement, pmConn3);
        cmd.Parameters.AddWithValue("@category", typeSelected);

        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("type", typeof(string));
        dt.Load(reader);

        reader.Close();

        incidentName.Items.Add(new ListItem("Please select incident name", " "));
        //incidentName.Items[0].Attributes.Add("disabled", "disabled");

        foreach (DataRow row in dt.Rows)
        {
            incidentName.Items.Add(new ListItem(row[0].ToString(), row[0].ToString()));
        }

        incidentName.Items[0].Attributes.Add("disabled", "disabled");
        incidentName.Items[0].Selected = true;

        pmConn3.Close();
    }
    catch (Exception ex)
    {
        var message = new JavaScriptSerializer().Serialize(ex.Message.ToString());
        //Response.Write(message);
        var script = string.Format("alert({0});", message);
        ScriptManager.RegisterClientScriptBlock(this, GetType(), "", script, true);
    }
    finally
    {
        pmConn3.Close();
    }
}

我复制了以防从第一个下拉列表中选择listitem时触发的方法。任何有帮助的人都会受到赞赏,因为我不明白它缺少什么

0 个答案:

没有答案