异步更新面板_

时间:2010-06-23 12:01:09

标签: c# asp.net ajax

我想异步更新面板(panel1和panel2)。但是当页面加载时,如果我尝试编辑一行并更新它,则panel1的RequiredFieldValidator会给出错误消息*并且行的更新链接不起作用!从panel1添加公司后,更新链接开始工作。我该如何解决这个问题?

    

<asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Conditional" >
    <ContentTemplate>
        <div id="div1">   
            <table>
                <tr>
                   <td><b>Company Name</b></td>
                   <td>
                   <asp:TextBox ID="company" runat="server" ></asp:TextBox>    
                   </td>
                   <td>
                   <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="company" ErrorMessage="*"
                   Display="Dynamic" ></asp:RequiredFieldValidator> 
                   </td>
                </tr>
                <tr>
                   <td></td>
                   <td>
                   <asp:Button ID="submitbut" runat="server" Text="Submit" onclick="submit_Click" />
                   </td>
               </tr>    
            </table>
       </div>                
    </ContentTemplate>   
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="submitbut" EventName="Click"/>
    </Triggers>
</asp:UpdatePanel>

<asp:UpdatePanel ID="updatepanel2" runat="server" UpdateMode="Always" >
    <ContentTemplate>    
        <div id="div3">     
            <asp:GridView ID="grid1" runat="server" DataSourceID="sqlSource1" DataKeyNames="id"
            AutoGenerateColumns="false" 
            BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
            CellPadding="3" ForeColor="Black" GridLines="Vertical">
                <Columns>
                    <asp:CommandField ShowSelectButton="true" ButtonType="Image" SelectImageUrl="~/Images/select.gif" />
                    <asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="~/Images/delete.gif" />
                    <asp:CommandField ShowEditButton="true" ButtonType="Image" EditImageUrl="~/Images/edit.gif" UpdateImageUrl="~/Images/update.png" CancelImageUrl="~/Images/delete.gif" />
                    <asp:BoundField DataField="id" HeaderText="Company ID" />
                    <asp:BoundField DataField="name" HeaderText="Company Name" />        
                </Columns>    
                <FooterStyle BackColor="#CCCCCC" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#CCCCCC" />
            </asp:GridView>

            <asp:SqlDataSource ID="sqlSource1" runat="server" 
             ConnectionString="<%$ ConnectionStrings:BTConnectionString %>" 
             ProviderName="System.Data.SqlClient"
             SelectCommand="SELECT * FROM Company"
             UpdateCommand="UPDATE Company SET name = @name WHERE ( id = @id )"
             DeleteCommand="DELETE FROM Company WHERE ( id = @id )" >
             <UpdateParameters>
                 <asp:Parameter Name="name" Type="String" />
                 <asp:Parameter Name="id" Type="Int32" />
             </UpdateParameters>
             <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
             </DeleteParameters>   
             </asp:SqlDataSource>
        </div>   
    </ContentTemplate> 
</asp:UpdatePanel>       

protected void Page_Load(object sender, EventArgs e)
{

}


protected bool IsatDatabase(string companyName)
{
    SqlApplication con = new SqlApplication();
    Int32 count = 0;

    try
    {
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM BT.dbo.Company WHERE ( name = @com )",
                                        con.GetConnection());
        cmd.Parameters.AddWithValue("@com", companyName);
        con.OpenSqlConnection();

        count = (Int32)cmd.ExecuteScalar();

        if (count == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    catch (Exception err)
    {
        System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
        return true;
    }
    finally
    {
        con.CloseSqlConnection();
    }
}



protected void submit_Click(object sender, EventArgs e)
{
    SqlApplication con = new SqlApplication();
    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con.GetConnection();
        cmd.CommandText = "INSERT INTO BT.dbo.Company VALUES ( @com )";

        cmd.Parameters.AddWithValue("@com", company.Text);
        con.OpenSqlConnection();

        if (!IsatDatabase(company.Text))
        {
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Close();
            grid1.DataBind();
        }
    }
    catch (Exception err)
    {
        System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
    }
    finally
    {
        con.CloseSqlConnection();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以在提交控件上设置CausesValidation =“false”,以防止此控件验证是否符合您的要求

当你说更新链接不起作用时,你的意思是什么?如果在Submit_Click函数上放置一个断点,它是否被调用?