将列表框中的值添加到数据库

时间:2013-11-16 06:26:15

标签: c# asp.net .net checkbox listbox

我遇到的问题是listbox的值不会添加到数据库中。

我有一个复选框列表和列表框,首先我要在列表框中添加所有选中的复选框值,它成功运行,然后我想将列表框的数据从复选框列表添加到按钮点击事件的数据库,它不起作用所以如何解决这个问题。

<div id="contentwrapper" class="contentwrapper">

            <div id="validation" class="subcontent">

                    <form class="stdform stdform2" style="border-top:solid 1px #ddd">

                        <p>
                            <label>Hotel Name</label>
                            <span class="field">
                                <asp:DropDownList ID="ddlHotel" runat="server">
                                </asp:DropDownList>
                            </span>
                        </p>

                        <p>
                            <fieldset class="fieldset">
                            <legend class="legend">Facilities</legend>
                                <div>
                                    <asp:CheckBoxList ID="cblFacility" runat="server" DataTextField="FacilityName" DataValueField="FacilityID" TextAlign="Right" RepeatColumns="5">
                                    </asp:CheckBoxList>

                                    <div class="clear">
                                    </div>
                                </div>

                            </fieldset>
                        </p>
                        <p  class="stdformbutton">
                            <asp:Button ID="btnAdd" runat="server" CssClass="radius2" Text="Add" onclick="btnAdd_Click" />
                        </p>                            
                    </form>                        
            </div><!--subcontent-->

        </div><!--contentwrapper-->            
        <div id="Div1" class="contentwrapper">              
            <div id="Div2" class="subcontent">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                    <form class="stdform stdform" style="border-top:solid 1px #ddd">
                        <p>                             
                            <span class="field">
                                <asp:ListBox ID="lstFacility" runat="server" SelectionMode="Multiple"></asp:ListBox><br />
                            </span>
                        </p>                            
                        <p  class="stdformbutton">
                            <asp:Button ID="btnSubmit" runat="server" CssClass="submit radius2" Text="Submit" onclick="btnSubmit_Click" />
                        </p>
                    </form>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
            </div><!--subcontent-->            
        </div>

AND .cs文件是:

protected void Page_Load(object sender, EventArgs e)
    {
        string myConnectionString = "my connection string";

        if (Session["admin"] != null)
        {
            lblEmail.Text = Session["adminEmail"].ToString();
            lblAdmin.Text = "Wel Come " + Session["admin"].ToString();
            lblAdmin1.Text = "Wel Come " + Session["admin"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

        if (!Page.IsPostBack)
        {
            if (Session["hotelID"] != null)
            {
                ddlHotel.SelectedValue = Session["hotelID"].ToString();
            }
            ddlHotel.DataSource = dalMST_Hotel.SelectAll(myConnectionString);
            ddlHotel.DataTextField = "HotelName";
            ddlHotel.DataValueField = "HotelID";
            ddlHotel.DataBind();
            ddlHotel.Items.Insert(0, "Select Hotel");

            BindData();
        }
    }


    private void BindData()
    {
        string myConnectionString = "my connection string";    
        cblFacility.DataSource = dalMST_Facility.SelectAll(myConnectionString);
        cblFacility.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string s1 = string.Empty;
        lstFacility.Items.Clear();
        foreach (ListItem item in this.cblFacility.Items)
        {
            if (item.Selected)
            {
                lstFacility.Items.Add(item);
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string myConnectionString = "my connection string";

        Page.Validate();
        if (Page.IsValid)
        {
            DataTable dt = dalMST_FacilityTran.SelectAll(myConnectionString);
            int cnt = dt.Rows.Count;
            entMST_FacilityTran.HotelID = Convert.ToInt32(ddlHotel.SelectedValue);
            entMST_FacilityTran.FacilityID = 0;

            entMST_FacilityTran.Created = DateTime.Now;
            entMST_FacilityTran.Modified = DateTime.Now;

            #region Insert,Update

            for (int i = 0; i < lstFacility.Items.Count; i++)
            {
                int flag = 0;
                for (int j = 0; j < cnt; j++)
                {
                    int hotelid = Convert.ToInt32(dt.Rows[j][2].ToString());
                    int facilityid = Convert.ToInt32(dt.Rows[j][1].ToString());


                    if (lstFacility.Items[i].Selected)
                    {
                        entMST_FacilityTran.FacilityID = Convert.ToInt32(lstFacility.Items[i].Value);

                        if (entMST_FacilityTran.HotelID == hotelid && entMST_FacilityTran.FacilityID == facilityid)
                        {
                            flag = 1;
                            break;
                        }
                        else
                        {
                            flag = 0;
                        }
                    }
                }
                if (flag == 0)
                {
                    if (dalMST_FacilityTran.Insert(entMST_FacilityTran, myConnectionString))
                    {
                        //txtFacility.Text = "";
                        //Response.Redirect("AddFacility.aspx");

                        //return;
                    }
                }
            }
            Response.Redirect("AddRoomCategory.aspx");


            #endregion
        }
    }

2 个答案:

答案 0 :(得分:1)

首先您必须删除Nested For Loop

 String lstName;

 for (int i= 0; i< listBoxEmployeeName.Items.Count;i++)
    {

    lstName=listBoxEmployeeName.Items[i].Text;//Here your value stored in lstName
    //here continue you insert query  

    }

答案 1 :(得分:1)

存在与更新面板相关的问题。

使用以下代码:

 <Triggers>
      <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
      <asp:PostBackTrigger ControlID="btnSubmit" /> 
 </Triggers>

因此提交按钮cilck事件将会触发。

由于