点击按钮获取GridView所有行的LevelID(数据库行ID)
Got you covered! Here's how to implement the btnButton_Click event to show every corresponding LevelID from your GridView in a popup, plus some extra context to help you understand how it works:
Step 1: Update the C# Code-behind
Replace your empty btnButton_Click method with this complete implementation:
protected void btnButton_Click(object sender, EventArgs e) { // Use StringBuilder to efficiently gather all LevelIDs (better for performance with many rows) StringBuilder levelIdList = new StringBuilder(); levelIdList.AppendLine("All GridView Row LevelIDs:"); levelIdList.AppendLine("---------------------------"); // Loop through each row in the GridView foreach (GridViewRow row in gvKar.Rows) { // Only process actual data rows (skip header/footer rows) if (row.RowType == DataControlRowType.DataRow) { // Pull the LevelID directly from DataKeys (since you set DataKeyNames="LevelID" already) int levelId = Convert.ToInt32(gvKar.DataKeys[row.RowIndex].Value); levelIdList.AppendLine($"Row {row.RowIndex + 1}: LevelID = {levelId}"); } } // Inject a client-side alert script to show the collected IDs // We escape single quotes to avoid JavaScript syntax errors string safeAlertText = levelIdList.ToString().Replace("'", "\\'"); ClientScript.RegisterStartupScript(this.GetType(), "ShowLevelIDs", $"alert('{safeAlertText}');", true); }
Key Details:
- DataKeys Usage: Since you already configured
DataKeyNames="LevelID"on your GridView, we don't need to dig into row controls to get the ID — this is the safest and cleanest way to access backend-only values. - StringBuilder: Avoids the performance hit of repeated string concatenation, which matters if your GridView has lots of rows.
- Client-side Alert: Uses
RegisterStartupScriptto trigger a browser popup (server-sideMessageBoxwould only show on the server, which isn't useful for users). The quote escaping ensures the alert works even if your text has single quotes.
Step 2: Quick ASPX Fix
Your button is missing the explicit OnClick hook to connect it to the code-behind method. Update it to this (you can also remove the redundant autopostback="True" since server buttons auto-postback by default):
<asp:Button ID="btnButton" runat="server" Text="Show" OnClick="btnButton_Click"/>
Alternative: Show Individual Popups per Row
If you'd prefer a separate popup for each row's LevelID instead of a single list, use this variation of the click event:
protected void btnButton_Click(object sender, EventArgs e) { StringBuilder popupScripts = new StringBuilder(); foreach (GridViewRow row in gvKar.Rows) { if (row.RowType == DataControlRowType.DataRow) { int levelId = Convert.ToInt32(gvKar.DataKeys[row.RowIndex].Value); popupScripts.AppendLine($"alert('Row {row.RowIndex + 1} LevelID: {levelId}');"); } } ClientScript.RegisterStartupScript(this.GetType(), "ShowLevelIDs", popupScripts.ToString(), true); }
This will trigger a sequence of alerts, one for each data row in the GridView.
内容的提问来源于stack exchange,提问作者freej17

