asp .net gridview搜索和编辑

时间:2014-11-19 20:03:37

标签: .net gridview

我在搜索后编辑行时遇到问题。当我找到我的用户并单击gridview中的编辑按钮时,它会转到我的第一个记录但不是编辑模式。在我的搜索按钮上第二次碰到后,我在编辑模式下记录了我的记录。我已尝试使用与此链接相关的GridViewRow.RowState属性来解决此问题:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowstate%28v=vs.110%29.aspx。当我点击编辑它没有,但在第二次点击后我有错误"无法加载viewstate" 我试图通过在页面上添加EnableViewState =" true"来解决它,但是我总是遇到同样的错误。我的BindData()方法如下所示:

private void BindData()
        {
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString))
            {
                string display = "SELECT * FROM User";
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = display;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
            }

并且搜索方法如下所示:

 protected void DisplayUser(string strSearch)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString);

        SqlCommand cmd = new SqlCommand("SearchUsers", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@SearchUser", strSearch);
        cmd.Connection.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

我也尝试将方法DisplayUser而不是void更改为DataSet,但我仍然遇到了同样的错误。 我的StoredProcedure看起来像这样:

ALTER PROCEDURE dbo.SearchUsers

@SearchUser varchar(50) 如     选择 *     来自[用户]     WHERE([User] .name LIKE'%' + @ SearchUser +'%' OR [User] .surname LIKE'%'     + @SearchUser +'%'或者[用户] .role LIKE'%' + @SearchUser +'%'或[用户] .block     喜欢'%' + @ SearchUser +'%')

我的网格像这样松散:

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="id_user" onRowCancelingEdit="GridView1_RowCancelingEdit" onRowEditing="GridView1_RowEditing" onRowDataBound="GridView1OnRowDataBound" OnRowUpdating="GridView1_RowUpdating" onRowDeleting="GridView1_RowDeleting" onselectedindexchanged="GV_SelectedIndexChanged" >
    <Columns>
    <asp:TemplateField HeaderText="LP" Visible="false" SortExpression="id_user"  HeaderStyle-Width="5px">
    <ItemTemplate>
        <asp:Label ID="id_uzytkownika" runat="server" Text='<%# Eval("id_user")%>'></asp:Label>
     </ItemTemplate>
   </asp:TemplateField>
  <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" ReadOnly="true" />
  <asp:BoundField DataField="surname" HeaderText="Surname" SortExpression="surname" ReadOnly="true" />
   <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" ReadOnly="true" />
   <asp:BoundField DataField="login" HeaderText="Login" SortExpression="login" ReadOnly="true" />
      <asp:TemplateField HeaderText="Password" HeaderStyle-Width="80px" SortExpression="password">
    <ItemTemplate>
        <asp:Label ID="LblPass" runat="server" Text='<%#Eval("password")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="TxtPass" runat="server" Text='<%#Eval("password")%>'></asp:TextBox>
    </EditItemTemplate>
   </asp:TemplateField>
    <asp:TemplateField HeaderText="Role" >
                <ItemTemplate>
                    <asp:Label ID="lblRole" runat="server" Text='<%#Eval("role") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DrpDwnRole" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
    <asp:TemplateField HeaderText="Block" HeaderStyle-Width="10%">
                <ItemTemplate>
                    <asp:Label ID="lblBlock" runat="server" Text='<%#Eval("block") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="ChckBxBlock" Text="Block" runat="server"></asp:CheckBox>
                </EditItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%">
                <ItemTemplate>
                    <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                    <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
                </EditItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Delete" HeaderStyle-Width="5%">
                <ItemTemplate>
                    <asp:LinkButton ID="btnDelete" Text="Usuń" runat="server" CommandName="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>

有人可以给我一些建议吗?

RowEditing - 它现在是,但是当我想使用行stte时,它看起来像问题开头的链接。

   protected void GridViewKierUzytkownik_RowEditing(object sender, GridViewEditEventArgs e)
    {
     GridView1.EditIndex = e.NewEditIndex;
    BindData();

    }
  1. 我正在使用VS2010和MVC 4 C#。
  2. 您正在谈论编辑,取消和保存按钮?我正在使用它们来编辑数据,更新和取消更新。 gridview的基础操作。

0 个答案:

没有答案