| |

VerySource

 Forgot password?
 Register
Search
View: 633|Reply: 2

How do I get the ID of a record if I put a button in the GridView?

[Copy link]

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-9 19:30:01
| Show all posts |Read mode
Add the following code to the GridView

<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "12%" />
<ItemTemplate>
<asp: LinkButton ID = "Link1" OnCommand = "Link1_clink" runat = "server" Text = "Set as cover"> </ asp: LinkButton>
</ ItemTemplate>
</ asp: TemplateField>

Backstage is
 protected void Link1_clink (object sender, EventArgs e)
    {
     }

So how do I get the ID of the record that clicked this button in the background ??
Reply

Use magic Report

0

Threads

24

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-6-4 11:45:01
| Show all posts
Change to this:

<asp:LinkButton ID="Link1" CommandName="setcmd" CommandArgument='Bind the record ID here' runat="server" Text="Set as cover"></asp:LinkButton>

then
Written in GrivView's RowCommand incident
  if(e.CommandName=="setcmd")
{
  int Id=Convert.ToInt32(e.CommandArgument) //Get the ID of the record
 //Other codes
}
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-8 19:30:01
| Show all posts
Positive Solutions Upstairs
I just don't know how to pass 2 parameters with CommandArgument~~

About DataKeys usage
<asp:GridView ID="GV" DataKeyNames="ID,Name"....
<Columns>
  <asp:BoundField DataField="ID".../>
  <asp:BoundField DataField="ID".../>
  ...
</Columns>
</asp:GridView>

Code behind
DataKey DataKey = GV.DataKeys[i]; //i is the number of rows
DataKey[0] //ID
DataKey[1] //Name
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list