1

同じユーザーの複数のチェックボックスをチェックする必要がある場合に、データベースから値を取得してチェックボックスリストを事前設定する際に問題があります。これは、ユーザーが登録時に行ったデータベースに従って、このページの読み込み時に複数のチェックボックスがオンになるようにしたい更新プロファイル ページです。だから、それを行う方法を教えてください私は何度も試しましたが、最後の値だけがチェックされ、他のすべてはチェックされません。1 人のユーザーが複数の趣味を持つことができます。したがって、チェックボックスリストは趣味用であり、更新ページが読み込まれると、複数の趣味が既にチェックされているので、趣味を更新して追加することもできます。私が行った更新部分は、チェックボックスリストに、ユーザーが以前に指定したすべての値が事前に入力されていることを望んでいます。前もって感謝します。

これは私の update.aspx です

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table class="style1">
        <tr>
            <td class="style3">
                <asp:Label ID="Label2" runat="server" Text="Login Name"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="txtlogin" runat="server" ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label4" runat="server" Text="EmailId"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="txtemail" runat="server" ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                    ControlToValidate="txtemail" Display="Dynamic" ErrorMessage="Invalid" 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label5" runat="server" Text="Address"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="txtadd" runat="server" TextMode="MultiLine" Width="142px" 
                    ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style6">
                <asp:Label ID="Label6" runat="server" Text="City"></asp:Label>
            </td>
            <td colspan="2" class="style7">
                <asp:DropDownList ID="ddlcity" runat="server" Height="21px" Width="142px" 
                    Enabled="False">
                </asp:DropDownList>
            </td>
            <td class="style7">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label7" runat="server" Text="Pincode"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="txtpin" runat="server" ReadOnly="True" ></asp:TextBox>
            </td>
            <td>
                <asp:RegularExpressionValidator ID="regexpin" runat="server" 
                    ControlToValidate="txtpin" ErrorMessage="Numbers only(six digits)" 
                    ValidationExpression="[0-9]{6}"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label8" runat="server" Text="PaymentMode"></asp:Label>
            </td>
            <td colspan="2">
                <asp:RadioButtonList ID="rbtnlstpay" runat="server" Height="28px" Width="225px" 
                    Enabled="False">
                </asp:RadioButtonList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label9" runat="server" Text="Hobbies"></asp:Label>
            </td>
            <td colspan="2">
                <asp:CheckBoxList ID="cblsthobbies" runat="server" Width="128px" 
                    Enabled="False">
                </asp:CheckBoxList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td class="style5" colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Button ID="btnedit" runat="server" Text="Edit" 
                    onclick="btnedit_Click" />
            </td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td class="style2" colspan="2">
                <asp:Label ID="lblresult" runat="server"></asp:Label>
            </td>
            <td class="style2">
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td class="style2" colspan="2">
                &nbsp;</td>
            <td class="style2">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td colspan="2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

これは私の update.aspx.cs です

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Update : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = @"Data Source=.;Initial Catalog=UserRecords;Integrated Security=True";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "select * from CityMaster;select * from HobbyMaster;select * from PaymentModeMaster;select * from Users where LoginId = @Loginid;select * from UsersHobbies where LoginId = @Loginid";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@Loginid",Session["loginid"].ToString());
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                ddlcity.Items.Add(new ListItem(dr["CityName"].ToString(), dr["CityId"].ToString()));
            }

            dr.NextResult();

            while (dr.Read())
            {
                cblsthobbies.Items.Add(new ListItem(dr["Hobbyname"].ToString(), dr["HobbyId"].ToString()));
            }

            dr.NextResult();

            while (dr.Read())
            {
                rbtnlstpay.Items.Add(new ListItem(dr["PaymentModename"].ToString(), dr["PaymentModeId"].ToString()));
            }

            dr.NextResult();

            while (dr.Read())
            {
                txtlogin.Text = dr["LoginId"].ToString();
                txtemail.Text = dr["EmailId"].ToString();
                txtadd.Text = dr["Address"].ToString();
                txtpin.Text = dr["Pincode"].ToString();
                ddlcity.SelectedValue = dr["CityId"].ToString();
                rbtnlstpay.SelectedValue = dr["PaymentModeId"].ToString();
            }

            dr.NextResult();

            while (dr.Read())
            {
                for(int i = 0; i < dr.FieldCount; i++)
                {
                    cblsthobbies.SelectedValue = dr["HobbyId"].ToString();
                    //cblsthobbies.SelectedValue = dr.GetValue(1).ToString();
                }
            }
            cn.Close();
        }
    }

    protected void btnedit_Click(object sender, EventArgs e)
    {
        txtemail.ReadOnly = false;
        txtadd.ReadOnly = false;
        ddlcity.Enabled = true;
        txtpin.ReadOnly = false;
        rbtnlstpay.Enabled = true;
        cblsthobbies.Enabled = true;
        btnedit.Text = "Update";
    }
}
4

3 に答える 3

2

これは、チェック済みの状態を設定しようとしている場所のようです:

for(int i = 0; i < dr.FieldCount; i++)
{
    cblsthobbies.SelectedValue = dr["HobbyId"].ToString();
    //cblsthobbies.SelectedValue = dr.GetValue(1).ToString();
}

ただし、これは の個々のアイテムのチェック状態を設定していませんCheckBoxListSelectedValueこれは、リスト自体の全体を設定するだけです。このような値は 1 つしかないため、ループを反復するたびに前の値が上書きされます。したがって、最後の 1 つだけが残ります。

チェック済みの状態を設定するには、最後のレコード セットの各レコードの個別のチェック ボックスを見つける必要があります。おそらく次のようなものです:

for(int i = 0; i < dr.FieldCount; i++)
{
    cblsthobbies.Items.FindByValue(dr["HobbyId"].ToString()).Selected = true;
}
于 2013-06-28T18:27:18.553 に答える
0

cblsthobbies.Items コレクションをループし、各項目の Selected プロパティ値を次のように設定します。

foreach (var i in cblsthobbies.Items) 
{
     i.Selected = i.SelectedValue == dr["HobbyId"].ToString();
}
于 2013-06-28T18:24:42.810 に答える