2

Hi all I have a GridView (GridView1) I have added a column and insert a button:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" DataSourceID="ObjectDataSource2" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" Width="798px">
            <Columns>
                .....
                <asp:ButtonField ButtonType="Button" CommandName="cmdFlag" Text="Flag" />
            </Columns>
        </asp:GridView>

Basically on click of the button I want to run a SQL Update but cant seem to click the button to enter C# and add the query. I can get in the C# for the page but unsure what to write for the method.

Heres the C# Code:

 void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmdFlag")
        {
        con.Open();

        cmd = new SqlCommand("UPDATE Comments SET Flagged = '" + "Yes" + "'", con); 

        cmd.ExecuteNonQuery();


        }

    }

Its doing nothing though. Basically I need it to look at the row and if the flagged button is clicked, update the comment to "Yes" under flagged.

4 Answers 4

2

You won't get a direct event. You need to write code on the RowCommand event.

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName=="cmdFlag")
        {
        //Write code update database
    }
}

Also you need to modify the GridView control. Add event like this.

onrowcommand="GridView1_RowCommand"

You can get more info here

Sign up to request clarification or add additional context in comments.

3 Comments

Hiya Thanks for that I have added it but its not doing what I want. Can you look at my post again I have updated it with more info.
Thanks mate it now does something but updates all the rows I think I need a WHERE statement just not to sure how to write it in terms of where that row only any ideas? I have a field for Comment_ID if that helps.
You need to get the value by Row.Cells[] or you can do a find control.
0

If you want to add the ID to use it in your WHERE statement you can put it into CommandArgument like

<asp:ButtonField ButtonType="Button" CommandName="cmdFlag" Text="Flag" CommandArgument='<%# Eval("Comment_ID") %>' />

then in the code you can get it from

var id = e.CommandArgument.ToString();

Update: just updated the code

2 Comments

Hiya I added the code you put but I get the error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
try to replace CommandArgument='<%# Eval("Comment_ID") %>' with CommandArgument="Comment_ID"
0

Register the event-handler in your code-behind file by doing this

GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);

and add this in you aspx page

OnRowCommand="GridView1_RowCommand"

Comments

0

You have forgot to add OnRowCommand="GridView1_RowCommand" on your Gridview, that's why it is not firing the RowCommand Event.

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="ObjectDataSource2" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" Width="798px">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.