简体   繁体   中英

ASP.NET Gridview Buttons

I have a Gridview with 3 columns (CompanyName, Volume, counter) I would like to have the counter column be all buttons and when a user clicks the button it increments the counter by 1. I'm not sure how to properly setup the update command. Here is what I have so far:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                SortExpression="CompanyName" />
            <asp:BoundField DataField="Volume" HeaderText="Volume" 
                SortExpression="Volume" />
            <asp:ButtonField DataTextField="counter" HeaderText="counter" 
            ButtonType="button"/>

        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LiquorStoreConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:LiquorStoreConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [CompanyName], [Volume], [counter] FROM [Company]">
    </asp:SqlDataSource>

All I need it to do is increment by 1 every time the button is pressed on that specific row.

You can try this:

... UpdateCommand="Update COMPANY SET counter=@Counter +1 WHERE CopmanyName=@Company"
<columns> 
    <asp:BoundField HeaderText="counter" DataField="Counter" />             
</columns>

Make sure you have the bound field names correct. I haven't tested to see if this + 1 works with a bound field. If it does not, then we'll have to use OnRowCommand to update the database manually, then you can re-bind the view and see updated counter. More importantly, I don't see a nice primary key for your Company so you can do the update... So my sample is using the CompanyName, and that is danger danger.

do you want to update the database? You can catch the button click event in gridview row command . Take a look at link given below http://msdn.microsoft.com/en-us/library/bb907626.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM