You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击按钮获取GridView所有行的LevelID(数据库行ID)

Solution to Display All LevelIDs in a Popup on Button Click

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 RegisterStartupScript to trigger a browser popup (server-side MessageBox would 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:29