【问题标题】:Filtered Gridview does edit correct row过滤后的 Gridview 确实编辑了正确的行
【发布时间】:2012-09-13 09:55:55
【问题描述】:

我有一个可以很好地与 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();

}

【问题讨论】:

  • 离题,但您的代码容易受到SQL Injection 攻击。始终使用SqlParameters
  • 它是一个内网应用,所以没关系

标签: c# asp.net gridview sqldatasource


【解决方案1】:

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

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

【讨论】:

  • 不,不起作用。我先搜索一条记录,然后单击编辑按钮,但它不会编辑该记录,它会将我带回第一条记录。
  • 这也是因为您在搜索处理程序中再次绑定了网格。
  • 通过设置gridView.EditIndex = -1取消点击搜索后的编辑。在搜索带来新行之后让网格处于编辑模式是没有意义的。
【解决方案2】:

你需要稍微修改你的代码:

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

    }

【讨论】:

  • 我也试过了,但没有奏效。反正我自己有答案。谢谢你。
  • 很高兴你得到了解决方案。
【解决方案3】:

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

在 page_load 事件中

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

当我搜索时,我在按钮事件中这样做了

Session["sqlString"] = sqlString;

这并不简单:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多