顯示具有 CheckBoxList 標籤的文章。 顯示所有文章
顯示具有 CheckBoxList 標籤的文章。 顯示所有文章

2014年7月13日 星期日

[RESOLVED] calling Checkbox checked Event


I've a repeater and in repeater i've check box list..wheck is generated dynamically.


How i can call check box checked Even


here is code








<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:

<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]






<%-- --%>
runat="server">









Set cbl Autopostback="true"


CheckedChanged event is for CheckBox instead CheckBoxList


CheckBoxList is using SelectedIndexChanged


This sample for CheckBox


    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
RepeaterItem ri = (RepeaterItem)cb.NamingContainer;
HiddenField hdngrpOpid = (HiddenField)ri.FindControl("hdngrpOpid");
int idx = ri.ItemIndex;




}












<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:

<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]






<%-- --%>
runat="server" AutoPostBack="true" onselectedindexchanged="cbxControl_SelectedIndexChanged" >





On Code Behind
protected void cbxControl__SelectedIndexChanged(object sender, EventArgs e)
{

}


[RESOLVED] calling Checkbox checked Event


I've a repeater and in repeater i've check box list..wheck is generated dynamically.


How i can call check box checked Even


here is code








<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:

<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]






<%-- --%>
runat="server">









Set cbl Autopostback="true"


CheckedChanged event is for CheckBox instead CheckBoxList


CheckBoxList is using SelectedIndexChanged


This sample for CheckBox


    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
RepeaterItem ri = (RepeaterItem)cb.NamingContainer;
HiddenField hdngrpOpid = (HiddenField)ri.FindControl("hdngrpOpid");
int idx = ri.ItemIndex;




}












<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:

<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]






<%-- --%>
runat="server" AutoPostBack="true" onselectedindexchanged="cbxControl_SelectedIndexChanged" >





On Code Behind
protected void cbxControl__SelectedIndexChanged(object sender, EventArgs e)
{

}


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] Checkbox


i,ve a repeater


then datalist


and then checkbox,textbox and redaio button



                    runat="server">




<%#GetGroupName(Eval("MENU_MODIFIER_GROUP_NAME_TRANSLATION_ID").ToString().Trim())%>
[Free :
<%#GetFreeQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]   [max:
<%#GetMaxQuantity(Eval("MENU_MODIFIER_GROUP_ID").ToString().Trim())%>
]





<%-- ID="chkItemOptionalModifiers" OnSelectedIndexChanged="chkItemOptionalModifiers_SelectedIndexChanged"
runat="server">
--%>














LH
Full
RH







how i can get the valuse of textbox,checkbox,hiddenfiels.... in jquery..





hi nafees,


as you have used css-classes for both check-box and text-box, so, you can use these with jQuery selectors, like this,


$(".chkList").each( function(){ //your code } );
$(".spinnnerOptional").each( function(){ //your code } );

thanks


[RESOLVED] DetailsView allow Multi Select




<%#Eval("GroupName")%>


datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"
AppendDataBoundItems = "True">
select faith group...



datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"/>
ErrorMessage="Faith is required.">




I need to allow the user to MultiSelect within a DetailsView.


How can I acheive this?


Currently they can only select 1 item per entry as shown below.


 



for multi select i think you should replace DropDownList with CheckBoxList, so that user can simple tick the items he wants to select.


thanks 



Can you assist in replicating my original script to a CheckBoxList


Thanks



here is the updated code,



datatextfield="religion" runat="server"
AppendDataBoundItems = "True">
select faith group...


thanks







ijaz_asp



here is the updated code,


  id="relig" datasourceid="SqlDataSource1" AutoPostBack="true" datatextfield="religion" runat="server" AppendDataBoundItems = "True">  Value="0">select faith group...  

thanks







because the datasource contains over 30 available options, its over two page long... i think this option would work, if there was only a small options.


Can i set it as a scrollbar or something?



any assistance please



Hi,


So now you are worry about how to select the checkbox in many pages, please try to refer to the following articles:


#Maintaining States of Selected CheckBoxes in Different Pages:

http://www.codeproject.com/Articles/31849/Maintaining-States-of-Selected-CheckBoxes-in-Diffe .


#Select Checkboxes while Paging:

http://aspadvice.com/blogs/azamsharp/archive/2006/10/26/Select-All-Checkboxes-while-Paging-in-GridView.aspx .


Hope it can help you.


Best Regards,

Amy Peng 


 


[RESOLVED] DetailsView allow Multi Select




<%#Eval("GroupName")%>


datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"
AppendDataBoundItems = "True">
select faith group...



datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"/>
ErrorMessage="Faith is required.">




I need to allow the user to MultiSelect within a DetailsView.


How can I acheive this?


Currently they can only select 1 item per entry as shown below.


 



for multi select i think you should replace DropDownList with CheckBoxList, so that user can simple tick the items he wants to select.


thanks 



Can you assist in replicating my original script to a CheckBoxList


Thanks



here is the updated code,



datatextfield="religion" runat="server"
AppendDataBoundItems = "True">
select faith group...


thanks







ijaz_asp



here is the updated code,


  id="relig" datasourceid="SqlDataSource1" AutoPostBack="true" datatextfield="religion" runat="server" AppendDataBoundItems = "True">  Value="0">select faith group...  

thanks







because the datasource contains over 30 available options, its over two page long... i think this option would work, if there was only a small options.


Can i set it as a scrollbar or something?



any assistance please



Hi,


So now you are worry about how to select the checkbox in many pages, please try to refer to the following articles:


#Maintaining States of Selected CheckBoxes in Different Pages:

http://www.codeproject.com/Articles/31849/Maintaining-States-of-Selected-CheckBoxes-in-Diffe .


#Select Checkboxes while Paging:

http://aspadvice.com/blogs/azamsharp/archive/2006/10/26/Select-All-Checkboxes-while-Paging-in-GridView.aspx .


Hope it can help you.


Best Regards,

Amy Peng 


 


[RESOLVED] DetailsView allow Multi Select




<%#Eval("GroupName")%>


datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"
AppendDataBoundItems = "True">
select faith group...



datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"/>
ErrorMessage="Faith is required.">




I need to allow the user to MultiSelect within a DetailsView.


How can I acheive this?


Currently they can only select 1 item per entry as shown below.


 



for multi select i think you should replace DropDownList with CheckBoxList, so that user can simple tick the items he wants to select.


thanks 



Can you assist in replicating my original script to a CheckBoxList


Thanks



here is the updated code,



datatextfield="religion" runat="server"
AppendDataBoundItems = "True">
select faith group...


thanks







ijaz_asp



here is the updated code,


  id="relig" datasourceid="SqlDataSource1" AutoPostBack="true" datatextfield="religion" runat="server" AppendDataBoundItems = "True">  Value="0">select faith group...  

thanks







because the datasource contains over 30 available options, its over two page long... i think this option would work, if there was only a small options.


Can i set it as a scrollbar or something?



any assistance please



Hi,


So now you are worry about how to select the checkbox in many pages, please try to refer to the following articles:


#Maintaining States of Selected CheckBoxes in Different Pages:

http://www.codeproject.com/Articles/31849/Maintaining-States-of-Selected-CheckBoxes-in-Diffe .


#Select Checkboxes while Paging:

http://aspadvice.com/blogs/azamsharp/archive/2006/10/26/Select-All-Checkboxes-while-Paging-in-GridView.aspx .


Hope it can help you.


Best Regards,

Amy Peng 


 


[RESOLVED] DetailsView allow Multi Select




<%#Eval("GroupName")%>


datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"
AppendDataBoundItems = "True">
select faith group...



datatextfield="religion" DataValueField="religion"
SelectedValue='<%# Bind("DESCRIPTION") %>' runat="server"/>
ErrorMessage="Faith is required.">




I need to allow the user to MultiSelect within a DetailsView.


How can I acheive this?


Currently they can only select 1 item per entry as shown below.


 



for multi select i think you should replace DropDownList with CheckBoxList, so that user can simple tick the items he wants to select.


thanks 



Can you assist in replicating my original script to a CheckBoxList


Thanks



here is the updated code,



datatextfield="religion" runat="server"
AppendDataBoundItems = "True">
select faith group...


thanks







ijaz_asp



here is the updated code,


  id="relig" datasourceid="SqlDataSource1" AutoPostBack="true" datatextfield="religion" runat="server" AppendDataBoundItems = "True">  Value="0">select faith group...  

thanks







because the datasource contains over 30 available options, its over two page long... i think this option would work, if there was only a small options.


Can i set it as a scrollbar or something?



any assistance please



Hi,


So now you are worry about how to select the checkbox in many pages, please try to refer to the following articles:


#Maintaining States of Selected CheckBoxes in Different Pages:

http://www.codeproject.com/Articles/31849/Maintaining-States-of-Selected-CheckBoxes-in-Diffe .


#Select Checkboxes while Paging:

http://aspadvice.com/blogs/azamsharp/archive/2006/10/26/Select-All-Checkboxes-while-Paging-in-GridView.aspx .


Hope it can help you.


Best Regards,

Amy Peng 


 


[RESOLVED] How to Count Checked CheckBoxes in the page ?


Hi all,


i have an page and masterpage for it i want to count checked Checboxes i try this code


void get_chk()
{
foreach (CheckBox cb in Page.Controls)
{
if (cb.Checked)
{
count++;
}
}
}

and i that err ?


Unable to cast object of type 'ASP.master_page_site1_master' to type 'System.Web.UI.WebControls.CheckBox'.

i try and change my code to:


            foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

now no cast err - but it is not count any thing ( count still = 0 ) and i ( checked 3 CheckBoxes ) ........ !


and the if condetion is false ...... ?


if (cb is CheckBox && ((CheckBox)cb).Checked)

so how can i resolve that ..... !?



Use


ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");


where "ContentPlaceHolder1" is name of your place holder and then access its Controls collection.



Are you able to use a checkboxlist control instead? This will allow you to easily iterate through the cbl Items collection to determine which items are checked/selected. 





smirnov



Use


ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");


where "ContentPlaceHolder1" is name of your place holder and then access its Controls collection.






do you mean the code will be 



ContentPlaceHolder cp1 = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

foreach (CheckBox cb in cp1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

i try that not working .... !?







MetalAsp.Net



Are you able to use a checkboxlist control instead? This will allow you to easily iterate through the cbl Items collection to determine which items are checked/selected. 





no i can't use it ..... :(



Fair enough. Ok well if you can put all the checkboxes inside of a panel control you'll be able to iterate through the panel Controls collection. If not then you'll need to use recursion to drill down into all the controls, sub-controls, sun-sub-controls
etc. 





red_scorpion1


i try that not working .... !?


Where do you call that code?


I tried this


ContentPlaceHolder p = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
int count = 0;
foreach (Control cb in p.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

and it counts checkboxes.


I also noticed that in your original code you put count = 0 in the loop which was wrong.


P.S.


If you call it from master page, you should use this.FindControl.





smirnov



red_scorpion1
i try that not working .... !?

Where do you call that code?
I tried this
ContentPlaceHolder p = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
int count = 0;
foreach (Control cb in p.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

and it counts checkboxes.


I also noticed that in your original code you put count = 0 in the loop which was wrong.


P.S.


If you call it from master page, you should use this.FindControl.




i have tryed a new page with same master and the code works fine


in my first page i have add an updatepanal (id="UpdatePanel1") and the checkboxes on it  ?


i try this code


            ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");
int count = 0;
foreach (Control cb in p.Controls)
{
foreach (Control item in up.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}
}

but here when i run code the UP = null .... !


if checkboxes out of the updatepanal it works 


note: updatepanal in page not in masterpage




Master HTML:












Page HTML:
















so what i must do please ..............









red_scorpion1


in button in the page not in masterpage


Then you can use your original code, but int count = 0; must be outside of foreach loop, because you reset counter to 0 on every item.


foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}




smirnov



red_scorpion1
in button in the page not in masterpage

Then you can use your original code, but int count = 0; must be outside of foreach loop, because you reset counter to 0 on every item.
foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}




i have edit my prv reply i hope you see it ?



Why do you do this?


1) ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
2) UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");

1) if you call the code from content page and looking for controls in content page, you don't need to search for placeholders. This is required only if you call the code from master page and looking for the controls inside content page. See my example in one of my posts before.
2) If you call the code from content page you don't need to use FindControl, simply use id - UpdatePanel1


So, if I understand it correct you need


int count = 0;
foreach (Control cb in UpdatePanel1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
count++;
}




smirnov



Why do you do this?
1) ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
2) UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");

1) if you call the code from content page and looking for controls in content page, you don't need to search for placeholders. This is required only if you call the code from master page and looking for the controls inside content page. See my example in one of my posts before.
2) If you call the code from content page you don't need to use FindControl, simply use id - UpdatePanel1


So, if I understand it correct you need


int count = 0; foreach (Control cb in UpdatePanel1.Controls) { if (cb is CheckBox && ((CheckBox)cb).Checked) count++; }


i try this code but nothing ..... !!!!! - it is dont count any checkbox


        protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;
foreach (Control cb in UpdatePanel1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
count++;
}
}

and tel that is false 


if (cb is CheckBox && ((CheckBox)cb).Checked)

MY HTML of page that has the updatepanal:


<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="HR.WebForm2" %>














and thank you so much for trying help ............ ???









try


Control p = (Control)UpdatePanel1.Controls[0];

foreach (Control cb in p.Controls)

... 


or


foreach (Control cb in UpdatePanel1.ContentTemplateContainer.Controls)

{

    if (cb is CheckBox && ((CheckBox)cb).Checked)

         count++;

}





smirnov



try


Control p = (Control)UpdatePanel1.Controls[0];

foreach (Control cb in p.Controls)

... 


or


foreach (Control cb in UpdatePanel1.ContentTemplateContainer.Controls)

{

    if (cb is CheckBox && ((CheckBox)cb).Checked)

         count++;

}






Thank you so much ... finaly worked ..... Smile


[RESOLVED] checkbox in gridview


I have a 4 checkbox in gridview. Now I want to Store the value of checkbox in database.


Please help me and send the code


Waiting for your help


Thanks in Advance



Hi,


If you want the value of checkbox, you should use GetValue method in case if you are using a CheckboxField, 


http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxfield.aspx


or you can also try this,


http://www.mikesdotnetting.com/Article/53/Saving-a-user's-CheckBoxList-selection-and-re-populating-the-CheckBoxList-from-saved-data


Hope this helps!




try like this


string strname =String.Empty;


foreach(GridViewRow  gvrow in gvDetails.Rows)

{

CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");

if (chk != null & chk.Checked)

{

str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';

strname += gvrow.Cells[2].Text+',';

}

}



Hi Devendar,



Are you using CheckBox field or Itemtemplates for displaying the grid view checkboxes?



If it is item templates


Markup

----------



       

           

               

       " />

               


           


       








Codebehind

------------------

  List allC;

  protected void Page_Load(object sender, EventArgs e)

        {

           if (!IsPostBack)

            {

                gv.DataSource = ;

                gv.DataBind();

            }

      }

        protected void btnclicked(object sender, EventArgs e)

        {

             allC = new List();

            Request.Form.AllKeys.Where(p => p.Contains("cbx_")).ToList().ForEach(p => allC.Add(Request.Form[p]));

            String vals = String.Join(",", allC.ToArray());

           //You can now use vals for getting all the check box values

        }




[RESOLVED] Adding a textbox to checkboxlist when checked?


Hello everyone, I have a checkboxlist in my web application and I am trying to add a textbox dynamically when the item in the checkbox is checked. How can this be done? 


Thanks. 



Firstly, you'll need to ensure that you have the AutoPostBack property set to "true" on your CheckBox (or CheckBoxList) so that it will hit your server-side code when selected : 







A
B
C

Next, you'll need to map these events within your server-side code and within those events, you'll dynamically create a TextBox and add it to your existing form : 


protected void YourCheckBox_CheckedChanged(object sender, EventArgs e)
{
//Create a TextBox
TextBox yourDynamicTextbox = new TextBox();
yourDynamicTextbox.ID = "YourDynamicTextBox";
yourDynamicTextbox.Text = "Example";

//Add the Textbox to your existing form
Form.Controls.Add(yourDynamicTextbox);
}

This could likely be handled completely client-side through jQuery quite easily as well if you wanted to consider that option without performing a PostBack.





Hello Rion, I got his error using the suggested code:


System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Does it work for you? Just an FYI, I am working on web application and not windows application. 


Thanks




Hello Rion, I would like to add a little bit more information. Now I am using Checkboxlist, I want to add a textbox next the each checkbox that is checked. Thanks



I'll check back and get an example for you when I have some time later on this evening if you are still having issues by then.



        foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
TextBox tb = new TextBox { ID = li.Value };
form1.Controls.Add(tb);
}
}



Hello Oned_gk, your solution is very similar to Rion. I got the error like my previous post. Does it work for you?


Thanks



Yes, it's working. The problem you have code blocks there.


Try using panel


Panel1.Controls.Add(tb);





Oned_gk, I will try to do it again. One question, where will the textbox be added? bottom of the page? I would love to have the textbox next to checkbox when its' checked.


Thanks. 



Try using the following example which will actually append a TextBox into the Area where the text appear next to each Checkbox : 





A
B
C


along with the following : 


protected void YourCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
{
//Check through each of the items
foreach (ListItem checkbox in YourCheckBoxList.Items)
{
//If this particular item is checked
if (checkbox.Selected)
{
//Dynamically create a HTML Textbox
checkbox.Text = String.Format("{0}", checkbox.Value);
}
else
{
//Otherwise simply store the normal value
checkbox.Text = String.Format("{0}", checkbox.Value);
}
}
}

This will always result in the Textbox being placed directly next to the actual CheckBox elements within your list.





Hello Rion, sorry for late reply. I have tested your solution. What happened is when I can see that the textbox was added but when i click inside the textbox, there is another postback and I could never enter information into the textbox. Is there a way
I fix this? I would appreciate this if it can done on client side, then there are fewer postback.


Thanks.



This is because the actual





Add this will strip out the 'for' attributes and should allow you to click on the TextBoxes as intended.




[RESOLVED] writing checkboxlist values to a table


Hi All,


I have a checkboxlist to let users select values. I need to grab the selected values and write to a database.


What would be the best and most efficient way to do this? Thanks for your help in advance.


            RepeatDirection="Horizontal">









foreach (ListItem li in checkboxList1.Items)
{
// If the list item is selected
if (li.Selected)
{
// Retrieve the value of the selected list item
Response.Write("Value = " + li.Value + ", ");
}
}



You can use LINQ also.



Hi,


If you want to insert the values into same column as mutiple rows you can try with the below code


protected void Button1_Click(object sender, EventArgs e)
{
//String variable to hold the selected items values
String str = "";
//Looping through items in checkboxlist
for (int i = 0; i <= checkboxList1.Items.Count - 1; i++)
{
//Checking whether items are selected or not
if (checkboxList1.Items[i].Selected)
{
//Adding the first item value
if (str == "")
{
str = checkboxList1.Items[i].Text;
}
// Adding
else
{
str += "," + checkboxList1.Items[i].Text;

}
}
}
//Creating connection object
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
//Opening the connection
con.Open();
//Creating command object and passing values to query
//Change the query as per your need
SqlCommand cmd = new SqlCommand("Insert into YourTableName(YourColumnName) values('" + str + "')", con);
//Executing the query
cmd.ExecuteNonQuery();
}



Refer the below link for more details




If you want to insert the values into multiple column then you can use the below code


string updateQry = "UPDATE myTable SET col1 = @param1, col2 = @param2, col3 = @param3 WHERE ...";

using(SqlConnection con = new SqlConnection("connection string here"))
{
using(SqlCommand cmd = new SqlCommand(updateQry, con))
{
cmd.CommandType = CommandType.Text;
con.Open();

for (int i = 0; i < checkboxlist1.Items.Count; i++)
{
cmd.Paramters.AddWithValue("@param" + i.ToString(), checkboxlist1.Items[i].Selected);
}

cmd.ExecuteNonQuery();
}
}






[RESOLVED] DropDown CheckBox List


Dear Experts,


I am having a DropDownCheckBoxes in a webform.When I bind some Data to the DropDownCheckBoxes it Gets binded to the control.But the text of the item is not added.Only the the checkbox list is visible.I cant able to get the corresponding Item Text and Value.Please
Help me to solve this issue


The following is the sample of the code I am using ,


DropDownCheckBoxes1.DataSource = GetIdsAndNames("City");

DropDownCheckBoxes1.DataTextField = "CityName";

DropDownCheckBoxes1.DataValueField = "CityID";

DropDownCheckBoxes1.DataBind();



And the Script is








Dear Sekhar,


                  Thank You for your kind help.I solved the issue.The problem was with the Text font.Now after binding the data to the DropDownListCheckBoxes I specified the color to be Black.Now I can able to visualize the text of ListItem.Thank you again