2013-10-07 44 views
0

我已經通過查詢字符串向本頁發送了值。之後,我無法在modechanging事件上獲取編輯模式。當我點擊編輯按鈕時,它只是回傳而沒有任何反應。如果點擊編輯第二次它給出錯誤:更新FormView asp.net中的問題。

(Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.) 

請告訴我我錯在哪裏。

源代碼 -

<asp:FormView ID="formview1" runat="server" AllowPaging="true" Caption="FireBrigade" DataKeyNames="FireBrigadeID" OnModeChanging="formview1_ModeChanging" 
     OnPageIndexChanging="formview1_PageIndexChanging"> 
     <ItemTemplate> 
      FireBrigade ID :<asp:Label ID="lblFID" runat="server" Text='<%# Eval("FireBrigadeID") %>'></asp:Label><br /> 
      Name :<asp:Label ID="Label3" runat="server" Text='<%# Eval("FBName") %>'></asp:Label><br /> 
      LatLong:<asp:Label ID="Label1" runat="server" Text='<%# Eval("LatLng") %>'></asp:Label><br /> 
      Address: <asp:Label ID="Label2" runat="server" Text='<%# Eval("Address") %>'></asp:Label><br /> 
      Contact: <asp:Label ID="Label4" runat="server" Text='<%# Eval("ContactNumber") %>'></asp:Label><br /> 
      <asp:LinkButton ID="EditButton" Text="Edit" CommandName="Edit" RunAt="server"/> 
     </ItemTemplate> 
     <EditItemTemplate> 
      FireBrigade ID :<asp:TextBox ID="txtFID" runat="server" Text='<%# Bind("FireBrigadeID") %>'></asp:TextBox><br /> 
      Name :<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("FBName") %>'></asp:TextBox> 
      LatLong:<asp:TextBox ID="txtlatlong" runat="server" Text='<%# Bind("LatLng") %>'></asp:TextBox><br /> 
      Address: <asp:DropDownList ID="ddlAddress" runat="server" OnDataBound="ddlAddress_DataBound" AppendDataBoundItems="true"> 
         <asp:ListItem Text="Select" Value="0"></asp:ListItem> 
         </asp:DropDownList> <br /> 
      Contact: <asp:TextBox ID="txtcontact" runat="server" Text='<%# Bind("ContactNumber") %>'></asp:TextBox><br /> 
      <asp:LinkButton ID="UpdateButton" 
       Text="Update" 
       CommandName="Update" 
       runat="server" /> 
      &nbsp; 
        <asp:LinkButton ID="CancelUpdateButton" 
         Text="Cancel" 
         CommandName="Cancel" 
         runat="server" /> 
     </EditItemTemplate> 
    </asp:FormView> 

C#代碼 -

public partial class Fifthpage : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      lblshow.Text = Request.QueryString["q"].ToString(); 
Dataset ds = new Dataset();     
ds = bind(); 
      da.Fill(ds); 
      formview1.DataSource = ds; 
      formview1.DataBind(); 
     } 
    } 
    public DataSet bind() 
    { 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = "Data Source=SYSTEM-PC;Initial Catalog=DB;Integrated Security=True"; 
     SqlCommand cmd = new SqlCommand("select * from FireBrigade",con); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     return ds; 
    } 
    protected void ddlAddress_DataBound(object sender, EventArgs e) 
    { 
     DataSet ds = new DataSet(); 
     ds = bind(); 
     List<string> ls = new List<string>(); 
     foreach (ListItem lst in ds.Tables[0].Rows) 
     { 
      //lst.Value = ds.Tables[0].Rows[0]["Address"].ToString(); 
      ls.Add(ds.Tables[0].Rows[0]["Address"].ToString()); 
     } 
     DropDownList ddladd = (DropDownList)formview1.FindControl("ddlAddress"); 
     ddladd.DataSource = ls; 
    } 

    protected void formview1_ModeChanging(object sender, FormViewModeEventArgs e) 
    { 
     formview1.ChangeMode(e.NewMode); 
     bind(); 
    } 

    protected void formview1_PageIndexChanging(object sender, FormViewPageEventArgs e) 
    { 
     formview1.PageIndex = e.NewPageIndex; 
     bind(); 
    } 
} 
+0

嘗試綁定Page_Init中的formview。在我看來,你在頁面生命週期中做得太晚了 – Robert

+0

你是否動態添加任何控件? –

回答

1

你應該編輯後重新分配FormView控件的數據源,所以只要改變你的代碼如下

protected void formview1_ModeChanging(object sender, FormViewModeEventArgs e) 
{ 
    formview1.ChangeMode(e.NewMode); 
    formview1.DataSource = bind(); 
    formview1.DataBind(); 
} 
+0

答案是正確的,並可能爲我工作的內容.... Thankyou –