2010-06-23 61 views
0

我想異步更新面板(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(); 
    } 
} 

}

回答

0

您可以設置在提交控件的CausesValidation = 「假」,以防止驗證這種控制,如果這是你希望的東西

當你說updatelink不起作用時,你是什麼意思?如果你在Submit_Click函數上放置一個斷點,它是否被調用?