更新面板上的死按钮?

时间:2011-05-02 01:04:16

标签: c# asp.net sql-server ajax visual-studio

使用asp.net和C#,我正在建立一个训练练习项目,我已经明确禁止使用参数化。明确。由我的老板。这是我的第一个项目,他决定重点关注其他事情。话虽这么说,我的问题是我有一个添加产品页面,显示更新面板,输入表单在下拉列表中选择后可见。这个功能很好。但是,产品表单上的提交按钮(应该使用户输入表单不可见,将数据插入数据库,显示另一个更新面板可见,并使用相同的数据填充标签)已经失效。它没有做任何事情。我的源代码和代码分别在下面。再次,明确禁止参数化。

<asp:UpdatePanel ID="UdpEPL" runat="server" UpdateMode="Conditional" Visible="False">
    <ContentTemplate>
        <asp:Label ID="LblEplEntity" runat="server" Text="Type Of Entity"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplEntity" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplTotalEmpl" runat="server" Text="Total Number of Employees"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplTotalEmpl" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplCalEmpl" runat="server" Text="Employees in California"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplCalEmpl" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplMichEmpl" runat="server" Text="Employees in Michigan"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplMichEmpl" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplNyEmpl" runat="server" Text="Employees in New York"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplNyEmpl" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplNjEmpl" runat="server" Text="Employees in New Jersey"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplNjEmpl" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplPrimEx" runat="server" Text="Primary/Excess"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplPrimEx" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplLim" runat="server" Text="Limit"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplLim" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplEplSir" runat="server" Text="EPL SIR"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplEplSir" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplPrem" runat="server" Text="Premium"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplPrem" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplWage" runat="server" Text="Wage &amp; Hour Sublimit"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplWage" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="LblEplInvestCost" runat="server" Text="Sublimit for Investigative Costs"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TbEplInvestCost" runat="server"></asp:TextBox>

    <br />
    <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="BtnEplSubmit" runat="server" Text="Submit" 
             />


    </ContentTemplate>
</asp:UpdatePanel>
<!--This is where the view only panel starts! Hope it works. -->
<asp:UpdatePanel ID="UdpEplShow" runat="server" UpdateMode="Conditional" Visible="False">
    <ContentTemplate>              
    <asp:Label ID="LblEplViewEntity" runat="server" Text="Type of Entity"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowEntity" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewTotalEmpl" runat="server" Text="Total Number of Employees"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowTotalEmpl" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewCalEmpl" runat="server" Text="Employees in California"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowCalEmpl" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewMichEmpl" runat="server" Text="Employees in Michigan"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowMichEmpl" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewNyEmpl" runat="server" Text="Employees in New York"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowNyEmpl" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewNjEmpl" runat="server" Text="Employees in New Jersey"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowNjEmpl" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewPrimEx" runat="server" Text="Primary/Excess"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowPrimEx" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewLim" runat="server" Text="Limit"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowLim" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewEplSir" runat="server" Text="EPL SIR"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowSir" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewPrem" runat="server" Text="Premium"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowPrem" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewWage" runat="server" Text="Wage &amp; Hour Sublimit"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowWage" runat="server"></asp:Label>
    <br />
    <asp:Label ID="LblEplViewInvestCost" runat="server" Text="Sublimit for Investigative Costs"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:Label ID="LblEplShowInvestCost" runat="server"></asp:Label>


    </ContentTemplate>
</asp:UpdatePanel>


protected void BtnEplSubmit_Click(object sender, EventArgs e)
{
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    string EplQuery = "INSERT INTO EPL (Entity, Employees, CA, MI, NY, NJ, Primex, EplLim, EplSir, Premium, Wage, Sublim) VALUES ('" + TbEplEntity + "', '" + TbEplTotalEmpl + "', '" + TbEplCalEmpl + "', '" + TbEplMichEmpl + "', '" + TbEplNyEmpl + "', '" + TbEplNjEmpl + "', '" + TbEplPrimEx + "', '" + TbEplLim + "', '" + TbEplEplSir + "', '" + TbEplPrem + "', '" + TbEplWage + "', '" + TbEplInvestCost + "')";
    string EplIdQuery = "SELECT SCOPE_IDENTITY() AS LastInsertedInstanceId";

    using (SqlConnection EplConn = new SqlConnection(connectionString))
    {
        EplConn.Open();
        SqlCommand EplCmd = new SqlCommand(EplQuery, EplConn);
        SqlCommand EplIdCmd = new SqlCommand(EplIdQuery, EplConn);
        using (EplCmd)
        using (EplIdCmd)
        {
            EplCmd.ExecuteNonQuery();
            SqlDataReader EplDr = EplIdCmd.ExecuteReader();
            EplDr.Read();
            int lastInsertedInstanceId = Convert.ToInt32(EplDr[0]);


        }

        string x = Request.QueryString["InstanceId"];
        string EplShowQuery = "SELECT Entity, Employees, CA, MI, NY, NJ, Primex, EplLim, EplSir, Premium, Wage, Sublim FROM EPL WHERE InstanceId =" + x;
        using (SqlCommand EplShowCmd = new SqlCommand(EplShowQuery, EplConn))
        {
            SqlDataReader EplDr = EplShowCmd.ExecuteReader();
            EplDr.Read();
            LblEplShowEntity.Text = EplDr.GetString(0);
            LblEplShowTotalEmpl.Text = EplDr.GetInt32(1).ToString();
            LblEplShowCalEmpl.Text = EplDr.GetInt32(2).ToString();
            LblEplShowMichEmpl.Text = EplDr.GetInt32(3).ToString();
            LblEplShowNyEmpl.Text = EplDr.GetInt32(4).ToString();
            LblEplShowNjEmpl.Text = EplDr.GetInt32(5).ToString();
            LblEplShowPrimEx.Text = EplDr.GetInt32(6).ToString();
            LblEplShowLim.Text = EplDr.GetInt32(7).ToString();
            LblEplShowSir.Text = EplDr.GetInt32(8).ToString();
            LblEplShowPrem.Text = EplDr.GetInt32(9).ToString();
            LblEplShowWage.Text = EplDr.GetInt32(10).ToString();
            LblEplShowInvestCost.Text = EplDr.GetInt32(11).ToString();

        }
    }
    if (TbEplInvestCost.Text != null)
    {

        //something about these next two lines seems unsafe, like they should be conditional, but I don't know why. Ask. 
        UdpEPL.Visible = false;
        UdpEplShow.Visible = true;
    }
}

2 个答案:

答案 0 :(得分:1)

我可能会说明显而易见的,但是在您提供的代码示例中,按钮没有指定OnClick事件处理程序。

除此之外,您可能必须在UpdatePanel中显式添加同步触发器,或在UpdatePanel中指定ChildrenAsTriggers =“false”。

答案 1 :(得分:0)

在您的按钮组AutoPostBack="true"