1

我有一个适用于 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>

后面的代码如下所示:

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

}
4

3 回答 3

1

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

protected void Page_Load(object sender, EventArgs e)
{
    ...
    if (!IsPostBack)
    ...
}
于 2012-09-13T10:17:24.493 回答
1

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

在 page_load 事件中

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

当我搜索时,我在按钮事件中做到了这一点

Session["sqlString"] = sqlString;

这并不简单:)

于 2012-09-13T11:12:46.747 回答
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();
                }
            }

    }
于 2012-09-13T11:57:54.170 回答