ListView with下拉列表&复选框

时间:2016-05-11 06:38:37

标签: c# asp.net listview

我正在开发一个asp.net Web应用程序,其中我有Listview with drop-down&复选框选项。

所有检查的项目都需要插入SQL表中。

但是,在插入数据库之前,如果选中复选框,则需要在下拉列表上进行验证以确保下拉列表已选择值(选定索引不应为0)

请帮助我 下面是我的aspx

<asp:ListView ID="myListView" runat="server" OnItemDataBound="myListView_ItemDataBound" OnItemCommand="myListView_ItemCommand"   >
        <ItemTemplate>
            <asp:Label runat="server" id="lblmemberid" Text='<%# Bind("memberid") %>'>></asp:Label>
            <asp:Label runat="server" id="lblName" Text='<%# Bind("name") %>'>></asp:Label>
            <asp:Label runat="server" id="lblAge" Text='<%# Bind("Age") %>'>></asp:Label>
            <asp:DropDownList runat="server" ID="ddlStatus"   AutoPostBack="true" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" ></asp:DropDownList>
            <asp:CheckBox runat="server" CommandName="Select" OnCommand="myListView_ItemCommand" AutoPostBack="true" ID="chkSelect" />
            <br />

        </ItemTemplate>

    </asp:ListView>

以下是我的代码

public void bindData()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Persist Security Info=False;User ID=sa;Initial Catalog=Subh_Vivah;Data Source=.;password=adarkar";
            //con.Open();
            string memberid = Request.QueryString["memberid"];
            string sqlQuery = "SELECT TOP 10  Name , memberid , age FROM Member_Details  ";
            SqlDataAdapter ada = new SqlDataAdapter(sqlQuery, con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            if (ds.Tables[0].Rows.Count != 0)
            {

                myListView.DataSource = ds;
                myListView.DataBind();
            }

        }

        protected void myListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            List<string> status = new List<string>();
            status.Add("SELECT");
            status.Add("YES");
            status.Add("NO");

            DropDownList ddl = e.Item.FindControl("ddlStatus") as DropDownList;
            ddl.DataSource = status;
            ddl.DataBind();
            ddl.SelectedIndexChanged += new EventHandler(ddlStatus_SelectedIndexChanged);

        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)myListView.FindControl("chkSelect");



            DropDownList ddl = (DropDownList)sender;
            if (ddl.SelectedValue == "SELECT")
            {
                chk.Checked = false;
                chk.Enabled = false; 
            }


        }

0 个答案:

没有答案