本文介绍了如何从ASP.NET中的数据库中检索选定的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 < asp:TemplateField HeaderText = Habbies > < ItemTemplate > <% - <%#Eval(Habbies)%> - %> < asp:Label ID = lblHabbies runat = 服务器 文本 =' <% #Eval( Habbies)%> ' > < / asp:标签 > < / ItemTemplate > < EditItemTemplate > < asp:CheckBoxList ID = chbxlist runat = 服务器 AutoPostBack = true 已启用 = true > < asp:ListItem > 演唱< / asp:ListItem > < asp:ListItem > 读取< / asp:ListItem > < asp:ListItem > watchingtv < / asp:ListItem > < ; asp:ListItem > 园艺< ; / asp:ListItem > < / asp:CheckBoxList > < / EditItemTemplate > < / asp:TemplateField > 这是我的gridview。 如何检索选中的复选框值来自数据库 asp.net 在这种情况下我需要一个Gridview ..如果我点击gridview中的编辑按钮自动绑定gridview中的复选框值来自DataBase .. ia m试试这个 protected void gridview1_RowDataBound( object sender,GridViewRowEventArgs e) { // if(e.Row.RowType == DataControlRowType.DataRow) // { // int genderValue =(int)DataBinder.Eval(e.Row .DataItem,Gender); // RadioButtonList rb =(RadioButtonList)e。 Row.FindControl(radioGender); // rb.Items.FindByValue(genderValue) .ToString())。Selected = true; // } // if(e.Row.RowType == DataControlRowType.DataRow&& gridview1.EditIndex == e.Row.RowIndex) // { // RadioButtonList rbGender =(RadioButtonList)e.Row.FindControl(rbGender); // string query =SELECT * FROM employee; // SqlCommand cmd = new SqlCommand(query,con); // SqlDataAdapter da = new SqlDataAdapter(cmd); // DataTable dt = new DataTable(); // da.Fill(dt); // rbGender.DataSour ce = dt; // rbGender.DataTextField =Gender; // rbGender.DataValueField =Name; // rbGender.DataBind(); // rbGender.Items.FindByValue((e.Row.FindControl(lblGender)as Label).Text).Selected = true; // } // string Habbies = string.Empty; // for(int i = 0;我< chbxlist.Items.Count; i ++) // { // if(chbxlist.Items [i] .Selected) // { // Habbies = chbxlist.Items [i] .Text; // 更新(Habbies, chbxlist.Items [i] .Selected); // } // } DataRowView dRowView =(DataRowView)e。 Row.DataItem; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState& DataControlRowState.Edit)> 0 ) { RadioButtonList rbGender =(RadioButtonList)e.Row.FindControl( rbGender); DropDownList dropdownStatus =(DropDownList)e.Row.FindControl( dropdownStatus); CheckBoxList chbxlist =(CheckBoxList)e.Row.FindControl( chbxlist); rbGender.SelectedValue = dRowView [ 5 ]。ToString(); dropdownStatus.SelectedValue = dRowView [ 7 ]。ToString(); chbxlist.SelectedValue = dRowView [ 6 ]。ToString(); } } 我的尝试: protected void gridview1_RowDataBound(object sender,GridViewRowEventArgs e) { // if(e.Row。 RowType == DataControlRowType.DataRow) // { // int genderValue =(int)DataBinder.Eval(e.Row.DataItem,Gender); // RadioButtonList rb =(RadioButtonList)e.Row.FindControl(radioGender); // rb.Items.FindByValue(genderValue.ToString())。选中= true; //} // if(e.Row.RowType == DataControlRowType.DataRow&& gridview1.EditIndex == e.Row.RowIndex ) // { // RadioButtonList rbGender =(RadioButtonList)e.Row.FindControl(rbGender); // string query =SELECT * FROM employee; // SqlCommand cmd = new SqlCommand(query,con); // SqlDataAdapter da = new SqlDataAdapter(cmd); // DataTable dt = new DataTable(); // da.Fill(dt); // rbGender.DataSource = dt; // rbGender.DataTextField =性别; // rbGender.DataValueField =名称; // rbGender.DataBind(); // rbGender.Items.FindByValue((e.Row.FindControl(lblGender)as Label).Text).Selected = true; //} //字符串Habbies = string.Empty; // for( int i = 0;我< chbxlist.Items.Count; i ++) // { // if(chbxlist.Items [i] .Selected) // { // Habbies = chbxlist.Items [i] .Text; //更新(Habbies,chbxlist.Items [i] .Selected); // } //} DataRowView dRowView =(DataRowView)e.Row.DataItem; if( e.Row.RowType == DataControlRowType.DataRow) { if((e.Row.RowState& DataControlRowState.Edit)> 0) { RadioButtonList rbGender =(RadioButtonList)e.Row.FindControl(rbGender); DropDownList dropdownStatus =(DropDownList)e.Row.FindControl(dropdownStatus); CheckBoxList chbxlist =(CheckBoxList)e.Row.FindControl(chbxlist); rbGender.SelectedValue = dRowView [5] .ToString(); dropdownStatus.SelectedValue = dRowView [7] .ToString(); chbxlist.SelectedValue = dRowView [6] .ToString(); } } 解决方案 试试这个 < asp :CheckBox Id = CheckBox1 Runat = 服务器 已检查 =' <% #Convert.ToBoolean(Eval( DBTblColumnName))%> ' / > <asp:TemplateField HeaderText="Habbies"> <ItemTemplate> <%-- <%#Eval("Habbies") %>--%> <asp:Label ID="lblHabbies" runat="server" Text='<%# Eval("Habbies")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:CheckBoxList ID="chbxlist" runat="server" AutoPostBack="true" Enabled="true"> <asp:ListItem>singing</asp:ListItem> <asp:ListItem>reading</asp:ListItem> <asp:ListItem>watchingtv</asp:ListItem> <asp:ListItem>gardening</asp:ListItem> </asp:CheckBoxList> </EditItemTemplate> </asp:TemplateField>this my gridview.How to retrieve selected checkbox values from database in asp.netin this condition I need a Gridview ..if i am clicking edit button in gridview automatically bind the checkboxs values in gridview from DataBase ..ia m trying this protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e) { //if (e.Row.RowType == DataControlRowType.DataRow) //{ // int genderValue = (int)DataBinder.Eval(e.Row.DataItem, "Gender"); // RadioButtonList rb = (RadioButtonList)e.Row.FindControl("radioGender"); // rb.Items.FindByValue(genderValue.ToString()).Selected = true; //} //if (e.Row.RowType == DataControlRowType.DataRow && gridview1.EditIndex == e.Row.RowIndex) //{ // RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender"); // string query = "SELECT * FROM employee"; // SqlCommand cmd = new SqlCommand(query,con); // SqlDataAdapter da = new SqlDataAdapter(cmd); // DataTable dt = new DataTable(); // da.Fill(dt); // rbGender.DataSource = dt; // rbGender.DataTextField = "Gender"; // rbGender.DataValueField = "Name"; // rbGender.DataBind(); // rbGender.Items.FindByValue((e.Row.FindControl("lblGender") as Label).Text).Selected = true; //} //string Habbies = string.Empty; //for (int i = 0; i < chbxlist.Items.Count; i++) //{ // if (chbxlist.Items[i].Selected) // { // Habbies = chbxlist.Items[i].Text; // Update(Habbies, chbxlist.Items[i].Selected); // } //} DataRowView dRowView = (DataRowView)e.Row.DataItem; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender"); DropDownList dropdownStatus = (DropDownList)e.Row.FindControl("dropdownStatus"); CheckBoxList chbxlist = (CheckBoxList)e.Row.FindControl("chbxlist"); rbGender.SelectedValue = dRowView[5].ToString(); dropdownStatus.SelectedValue = dRowView[7].ToString(); chbxlist.SelectedValue = dRowView[6].ToString(); } }What I have tried:protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e) { //if (e.Row.RowType == DataControlRowType.DataRow) //{ // int genderValue = (int)DataBinder.Eval(e.Row.DataItem, "Gender"); // RadioButtonList rb = (RadioButtonList)e.Row.FindControl("radioGender"); // rb.Items.FindByValue(genderValue.ToString()).Selected = true; //} //if (e.Row.RowType == DataControlRowType.DataRow && gridview1.EditIndex == e.Row.RowIndex) //{ // RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender"); // string query = "SELECT * FROM employee"; // SqlCommand cmd = new SqlCommand(query,con); // SqlDataAdapter da = new SqlDataAdapter(cmd); // DataTable dt = new DataTable(); // da.Fill(dt); // rbGender.DataSource = dt; // rbGender.DataTextField = "Gender"; // rbGender.DataValueField = "Name"; // rbGender.DataBind(); // rbGender.Items.FindByValue((e.Row.FindControl("lblGender") as Label).Text).Selected = true; //} //string Habbies = string.Empty; //for (int i = 0; i < chbxlist.Items.Count; i++) //{ // if (chbxlist.Items[i].Selected) // { // Habbies = chbxlist.Items[i].Text; // Update(Habbies, chbxlist.Items[i].Selected); // } //} DataRowView dRowView = (DataRowView)e.Row.DataItem; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender"); DropDownList dropdownStatus = (DropDownList)e.Row.FindControl("dropdownStatus"); CheckBoxList chbxlist = (CheckBoxList)e.Row.FindControl("chbxlist"); rbGender.SelectedValue = dRowView[5].ToString(); dropdownStatus.SelectedValue = dRowView[7].ToString(); chbxlist.SelectedValue = dRowView[6].ToString(); } } 解决方案 Hi,try this<asp:CheckBox Id="CheckBox1" Runat="Server" Checked='<%# Convert.ToBoolean(Eval("DBTblColumnName"))%>'/> 这篇关于如何从ASP.NET中的数据库中检索选定的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 09:23