You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

ASP.NET C#中用PlaceHolder构建复选框列表并获取选中值的问题

Absolutely Feasible! Server-Side Only Solution (No JavaScript Required)

Since you can’t use client-side JS and are building your table dynamically via PlaceHolder, we can rely entirely on ASP.NET Web Forms’ native form submission mechanism to capture selected checkboxes when the ASP button is clicked. Here’s how to implement this:

Step 1: Update Your Dynamic Checkbox Code

First, adjust the C# code that generates your checkboxes. You need two key changes:

  • Use a consistent name attribute for all checkboxes (so we can fetch all selected values server-side in one go)
  • Set the value attribute to the specific row identifier you need to retrieve (like rowGuid or rowId—something that uniquely identifies the row)

Replace your existing checkbox line with this:

// Use a unified name "chkSelectedRows" and set value to your row's unique ID
htmlTotal.Append(@"<input type=""checkbox"" name=""chkSelectedRows"" value=""" + rowGuid + @""" />");

Note: I used rowGuid here because it’s a unique value from your dataset—swap it with whichever field you need to capture for each selected row (e.g., rowId).

Step 2: Add an ASP.NET Button to Your Content Page

Make sure you have an <asp:Button> placed inside the asp:Content block of your TEST.aspx page (this maps to the content placeholder in your master page):

<asp:Content ID="MainContent" ContentPlaceHolderID="YourMasterPageContentPlaceholderID" runat="Server">
    <!-- Your existing tabs, table, and PlaceHolder code here -->
    <asp:Button ID="btnProcessSelected" runat="server" Text="Process Selected Rows" OnClick="btnProcessSelected_Click" />
</asp:Content>

Don’t forget to replace YourMasterPageContentPlaceholderID with the actual ID of the content placeholder from your HorizMenu.Master.

Step 3: Handle the Button Click in Code-Behind

In your TEST.aspx.cs file, add the click event handler to retrieve all selected checkbox values and process them:

protected void btnProcessSelected_Click(object sender, EventArgs e)
{
    // Fetch all selected values from the form submission
    string[] selectedRowIdentifiers = Request.Form.GetValues("chkSelectedRows");

    if (selectedRowIdentifiers != null && selectedRowIdentifiers.Length > 0)
    {
        foreach (string rowId in selectedRowIdentifiers)
        {
            // Perform your desired logic here with each selected row's ID
            // Examples: Fetch full row data, send notifications, update records, etc.
            // For testing, you can output the selected IDs:
            Response.Write($"Selected Row GUID: {rowId}<br/>");
        }
    }
    else
    {
        Response.Write("No rows were selected.");
    }
}

How This Works

When you click the ASP.NET button, the entire form submits to the server. The Request.Form.GetValues() method pulls all values from checkboxes with the name chkSelectedRows—this gives you an array of all the unique row identifiers you selected. You can then use these identifiers to do whatever processing you need.

Key Tips

  • Avoid nulls: You’re already handling null values for rowGuid in your existing code, so you don’t have to worry about empty checkbox values.
  • No JS dependencies: This entire flow is server-side, so it bypasses your master page’s restriction on inline JavaScript completely.

内容的提问来源于stack exchange,提问作者Kevin Henzel

火山引擎 最新活动