开发者

Updating Panels asynchronously_

开发者 https://www.devze.com 2023-01-04 06:24 出处:网络
I want to update panels ( panel1 and panel2) asynchronously. However when the page loads, if i try to开发者_Python百科 edit a row and update it, panel1\'s RequiredFieldValidator gives an error message

I want to update panels ( panel1 and panel2) asynchronously. However when the page loads, if i try to开发者_Python百科 edit a row and update it, panel1's RequiredFieldValidator gives an error message * and row's update link does not work!. After adding a company from panel1, update link starts to work. How can i solve the problem?

<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();
    }
}

}


You can set CausesValidation="false" on the submit controls to prevent this control from validating if this is what you wish

When you say the updatelink doesn't work, what do you mean? If you put a breakpoint on the Submit_Click function, is it called?

0

精彩评论

暂无评论...
验证码 换一张
取 消