开发者

can we use both custom button and inbuilt button in datagridview

开发者 https://www.devze.com 2022-12-28 09:09 出处:网络
I am using Datagridview in asp.net. I have used custom buttons of up and down in the datagridview along with edit,delete and paging options.

I am using Datagridview in asp.net. I have used custom buttons of up and down in the datagridview along with edit,delete and paging options.

can we use both custom button and inbuilt button in datagridview

I am handling the up down buttons by raising events in rowcommand and the code is as below


    string command = e.CommandName;

    Response.Write(e.CommandArgument.ToString());       
    int index = Convert.ToInt32(e.CommandArgument.ToString());        
    int count = GridView1.Rows.Count;
    int keyValue = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
    string value = GridView1.Rows[index].Cells[4].Text;

    SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
    SqlCommand cmd = new SqlCommand();


    if (command == "up")
    {
        if (index > 0)
        {
            index = index - 1;
            int keyValue1 = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
            string value1 = GridView1.Rows[index].Cells[4].Text;
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE [category] SET [order_id] = '" + value + "' WHERE [category_id]=" + keyValue1 + ";UPDATE [category] SET [order_id] = '" + value1 + "' WHERE [category_id]=" + keyValue + ";";

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
    }
    else if (command == "down")
    {
        if (index < count - 1)
        {
            index = index + 1;
            int keyValue1 = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
            string value1 = GridView1.Rows[index].Cells[4].Text;
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE [category] SET [order_id] = '" + value + "' WHERE [category_id]=" + keyValue1 + ";UPDATE [category] SET [order_id] = '" + value1 + "' WHERE [category_id]=" + keyValue + ";";

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();               
        }
    }

    Response.Redirect("Default.aspx");

Designer file


<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="category_id" DataSourceID="SqlDataSource1" 
        AllowPaging="True" onrowcommand="GridView1_RowCommand" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        AllowSorting="True">

        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="category_id" HeaderText="category_id" 
                InsertVisible="False" ReadOnly="True" SortExpression="category_id" />
            <开发者_Python百科;asp:BoundField DataField="categoryname" HeaderText="categoryname" 
                SortExpression="categoryname" />
            <asp:BoundField DataField="navigation_url" HeaderText="navigation_url" 
                SortExpression="navigation_url" />
            <asp:BoundField DataField="order_id" HeaderText="order_id" 
                SortExpression="order_id" />
            <asp:ButtonField ButtonType="Image" CommandName="up" Text="up" 
                ImageUrl="~/images/up.png" />
            <asp:ButtonField ButtonType="Image" CommandName="down" 
                ImageUrl="~/images/down.png" Text="down" />
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSubmit"/>
                </ItemTemplate>
                </asp:TemplateField>
            <asp:ButtonField CommandName="edit" Text="Edit" />
        </Columns>

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:pp_cmsConnectionString %>" 
        DeleteCommand="DELETE FROM [category] WHERE [category_id] = @category_id" 
        InsertCommand="INSERT INTO [category] ([categoryname], [navigation_url], [order_id]) VALUES (@categoryname, @navigation_url, @order_id)" 
        SelectCommand="SELECT * FROM [category] order by order_id" 
        UpdateCommand="UPDATE [category] SET [categoryname] = @categoryname, [navigation_url] = @navigation_url, [order_id] = @order_id WHERE [category_id] = @category_id">
        <DeleteParameters>
            <asp:Parameter Name="category_id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="categoryname" Type="String" />
            <asp:Parameter Name="navigation_url" Type="String" />
            <asp:Parameter Name="order_id" Type="Decimal" />
            <asp:Parameter Name="category_id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="categoryname" Type="String" />
            <asp:Parameter Name="navigation_url" Type="String" />
            <asp:Parameter Name="order_id" Type="Decimal" />
        </InsertParameters>
    </asp:SqlDataSource>

</div>

After this my edit,delete and paging is not working bcoz of event conflicts. Can anyone plz help me on this, so that i will be able to use both custom buttons(up and down) and edit,delete and paging features.


I think that your problem is the Redirect at the end.
If the user clicks the standard edit / delete buttons, your OnRowCommand method will still be executed first, and probably that's why they stopped working.
It is not clear why you need the redirect, but you should make sure to call it only upon the execution of one of your custom commands.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号