过滤的Gridview会编辑正确的行

时间:2012-09-13 09:55:55

标签: c# asp.net gridview sqldatasource

我有一个适用于SQLDataSource的gridview。编辑,删除按钮工作正常 但是,当我搜索任何记录并尝试编辑该记录时,gridview会在编辑模式中打开第一行 我不知道我做错了什么。

这是我的代码

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_id"
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." AllowPaging="True" 
        AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" 
        PageSize="5" Width="873px" onrowediting="GridView1_RowEditing" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                        CommandName="Update" Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit" ></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Select" Text="Select"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Customer_id" HeaderText="Customer_id" ReadOnly="True"
                SortExpression="Customer_id" InsertVisible="False" />
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:BoundField DataField="Customer_Type" HeaderText="Customer_Type" SortExpression="Customer_Type" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
            Font-Bold="True" Font-Italic="True" Font-Overline="True" Font-Size="Large" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

Code Behind看起来像这样:

protected void Page_Load(object sender, EventArgs e)
{
    string vs = (string)ViewState["buttonClicked"];
    string isEditing = (string)ViewState["isEditing"];
    if (IsPostBack)
    {
        if (vs == "False")
        {
            RadioButtonListChanged();
            GridView1.DataBind();
        }
        else if (vs == "True")
        {
            btnSearch_Click(sender, e);
            GridView1.DataBind();
        }
    }
}

protected void RadioButton1_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonListChanged();

}

private void RadioButtonListChanged()
{
    ViewState["buttonClicked"] = "False";

    string sqlString;

    if (RadioButton1.SelectedItem.Text != "All")
    {
        sqlString = "Select * from customers where status='True' and  customer_type = '" + RadioButton1.SelectedValue.ToString() + "' order by customer_name";
    }
    else
    {
        sqlString = "Select * from customers where  status='True' order by customer_name";
    }

    SqlDataSource1.SelectCommand = sqlString;
    SqlDataSource1.DataBind();

}

protected void btnSearch_Click(object sender, EventArgs e)
{

    ViewState["buttonClicked"] = "True";

    string sqlString;
    sqlString = "Select * from customers where status='True' and customer_name like '%" + txtCustomerName.Text + "%' order by customer_type, customer_name";
    SqlDataSource1.SelectCommand = sqlString;
    SqlDataSource1.DataBind();

}

3 个答案:

答案 0 :(得分:1)

您在每个帖子中再次绑定网格,因此每次都将当前行索引设置为零,这会导致第一行被编辑。 将代码更改为:

protected void Page_Load(object sender, EventArgs e)
{
    ...
    if (!IsPostBack)
    ...
}

答案 1 :(得分:1)

我在这里找到了解决方案 Gridview Selects Wrong Row For Editing

在page_load事件中

if (IsPostBack)
{
    SqlDataSource1.SelectCommand = (string)Session["sqlString"];
    SqlDataSource1.DataBind();
}

当我在搜索时,我在按钮事件中执行此操作

Session["sqlString"] = sqlString;

这并不简单:)

答案 2 :(得分:1)

您需要稍微修改一下代码:

protected void Page_Load(object sender, EventArgs e)
{
    string vs = (string)ViewState["buttonClicked"];
    string isEditing = (string)ViewState["isEditing"];
    if (IsPostBack)
    {
        if (vs == "False")
        {
            RadioButtonListChanged();
            GridView1.DataBind();
        }
        else if (vs == "True")
        {
            btnSearch_Click(sender, e);
            GridView1.DataBind();
        }
    }
}

要:

    protected void Page_Load(object sender, EventArgs e)
        {
            string vs = (string)ViewState["buttonClicked"];
            string isEditing = (string)ViewState["isEditing"];
            if (**!IsPostBack**)                     
                **^^^**
 /*just bind gridview when page is not 
        postback this will not bind oyur gridview on your every request*/
                if (vs == "False")
                {
                    RadioButtonListChanged();
                    GridView1.DataBind();
                }
                else if (vs == "True")
                {
                    btnSearch_Click(sender, e);
                    GridView1.DataBind();
                }
            }

    }
相关问题