私はASP.NETを初めて使っていますが、クエリ結果からチェックボックスを作成する必要があります。これまでのところ私が持っているものがあります。ASP.NET C#DataTableからチェックボックスを作成する
DataTable dtLocations = new DataTable();
dtLocations.Columns.Add("ID");
dtLocations.Columns.Add("VALUE");
// Pull locations and add to our datatable
string strConnection = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
using (SqlConnection dbConn = new SqlConnection(strConnection))
{
SqlDataAdapter dbAdapter = new SqlDataAdapter();
SqlCommand dbCommand = new SqlCommand();
dbConn.Open();
dbCommand.CommandText = @"SELECT location_id, location_desc FROM admin_locations WHERE enabled = 'Y'";
dbCommand.Connection = dbConn;
SqlDataReader dbReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (dbReader.HasRows) {
while (dbReader.Read()) {
dtLocations.Rows.Add(new object[] { dbReader["location_id"].ToString(), dbReader["location_desc"].ToString() });
}
}
}
return dtLocations;
私は、クラスレベルのvarが
public DataTable dtLocations = new DataTable();
として定義されている:私のコードで
は、背後にある...私はそうのようにその結果からのDataTableを作成し、次に必要とクエリを引っ張ります私は私のaspxファイルに
protected void Page_Load(object sender, EventArgs e)
{
if (!(IsPostBack))
{
dtLocations = createLocationsDataTable();
}
}
その後、私のPage_Loadの中で、上記の方法を使用して移入
(ないタラeの背後にある)私はこれをやってみて、言うまでもなく動作しないチェックボックスを作成しようとしています。
誰かがあなたがASP.NET C#でこれを行う方法を教えてもらえますか?また、ページ上のボタンをクリックしたときに、コード内でチェックされている値の値を取得できるようにする必要があります。
このようなCheckBoxListを試してみると、ここでコードブロックを使用することはできません。
<asp:CheckBoxList ID="message_locations" runat="server">
<% foreach (DataRow row in dtLocations.Rows) {%>
<asp:ListItem>TEST</asp:ListItem>
<% } %>
</asp:CheckBoxList>
に直接のDataTableをバインドすることができます。 – VDWWD
@VDWWD - これを行うよい例はありますか?私はこのhttps://asp-net-example.blogspot.com/2008/10/checkboxlist-control-example.htmlを参照していますが、チェックボックスのラベルとしてキーVALUEとしてIDを使用していません - すべてのテキストです。 – Phil
私のCheckBoxListを前面のaspxファイルに作成してListItemを作成し、コードの後ろにコードを追加します – Phil