how to get value of all selected checkbox?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 8
Reputation: frmsasp is an unknown quantity at this point 
Solved Threads: 0
frmsasp frmsasp is offline Offline
Newbie Poster

how to get value of all selected checkbox?

 
0
  #1
Oct 24th, 2005
Hello there,

I am using ASP.NET with C# and facing below problem. I am not able to get values of selected checkboxes which are generated dynamically.
Pls have a look at the image attached with the message...

Waiting for your reply....

Thanks,
Paresh
Attached Thumbnails
rights_forum.gif  
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 47
Reputation: kedar_challa is an unknown quantity at this point 
Solved Threads: 1
kedar_challa's Avatar
kedar_challa kedar_challa is offline Offline
Light Poster

Re: how to get value of all selected checkbox?

 
0
  #2
Nov 9th, 2005
Originally Posted by frmsasp
Hello there,

I am using ASP.NET with C# and facing below problem. I am not able to get values of selected checkboxes which are generated dynamically.
Pls have a look at the image attached with the message...

Waiting for your reply....

Thanks,
Paresh
Hi Paresh,

I dont know how you are adding the dynamic controls at the runtime. But while you are create the new Checkbox Object to add to the form, append the same object to a static collection object like Hash Table.

protected static Hashtable mRoleCheckBoxList=new Hashtable();
mRoleCheckBoxList.Add(RoleName, NewCheckBox);

Here mRoleCheckBoxList is a global static object of a HashTable.
now when ever you did a postback to the page u can get the values using following.
IDictionaryEnumerator RoleCheckBoxEnumerator = mRoleCheckBoxList.GetEnumerator();
while ( RoleCheckBoxEnumerator.MoveNext() )
{
HtmlInputCheckBox RoleCheckBox=(HtmlInputCheckBox)
RoleCheckBoxEnumerator.Value;
string BoxRoleName=(string)RoleCheckBox.Value;
if(RoleName.Equals(BoxRoleName))
{
RoleCheckBox.Checked=true;
break;
}
}
Thanks,
Kedar
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 1
Reputation: shaunavon is an unknown quantity at this point 
Solved Threads: 0
shaunavon shaunavon is offline Offline
Newbie Poster

Re: how to get value of all selected checkbox?

 
0
  #3
May 5th, 2008
I have the same problem. Can you tell me exactly how to use the above code snippet
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: how to get value of all selected checkbox?

 
1
  #4
May 6th, 2008
The method you are using is depreciated and you should create your controls dynamically using the built in methods.

E.g.
  1. HtmlInputCheckBox cb = new HtmlInputCheckBox();
  2. cb.id = "someid";
  3. cb.value = "your value";
  4.  
  5. /* Panel1 being a panel or some sort of placeholder on the page */
  6. panel1.Controls.Add(cb);

When creating dynamic controls to access there variables on post back you need to re-initialise them.

Essentially recreating them (it sounds stupid)

Here is an example for you.

I see your using C# and im afraid that this is in VB.net from an old project. But its not very difficult to convert.

Just Create a new Website add this as a page and run it. (Again make sure its vb.net)

  1. <%@ Page Language="VB" Strict="true" %>
  2.  
  3. <script runat="server">
  4.  
  5. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  6. Dim enuView As IEnumerator = ViewState.Keys.GetEnumerator
  7. While enuView.MoveNext
  8. Dim strKey As String = enuView.Current.ToString
  9. If strKey.StartsWith("txt~", StringComparison.OrdinalIgnoreCase) Then
  10. createTextBox(strKey, True)
  11. ElseIf strKey.StartsWith("ddl~", StringComparison.OrdinalIgnoreCase) Then
  12. createDropDownList(strKey, True)
  13. End If
  14. End While
  15. End Sub
  16.  
  17. Protected Sub btnAddTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  18. Dim strId As String = "txt~" & Format(Now, "HHmmss")
  19. createTextBox(strId, False)
  20. End Sub
  21.  
  22. Protected Sub btnAddDropDownList_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  23. Dim strId As String = "ddl~" & Format(Now, "HHmmss")
  24. createDropDownList(strId, False)
  25. End Sub
  26.  
  27. Private Sub createTextBox(ByVal strId As String, ByVal isReturning As Boolean)
  28. Dim txtBox As New TextBox
  29.  
  30. txtBox.ID = strId ' Set ID before if returning
  31. plcDynamicContent.Controls.Add(txtBox) ' Add to page before changing values else they are not held in viewstate
  32.  
  33. If Not isReturning Then ' Only set defaults if is new textbox
  34. With txtBox
  35. .Text = "Added at " & Format(Now, "HH:mm:ss")
  36. End With
  37. End If
  38.  
  39. ViewState(strId) = strId ' Store the ID of the control in viewstate
  40. End Sub
  41.  
  42. Private Sub createDropDownList(ByVal strId As String, ByVal isReturning As Boolean)
  43. Dim ddlList As New DropDownList
  44.  
  45. ddlList.ID = strId
  46. plcDynamicContent.Controls.Add(ddlList) ' Add to page before changing values else they are not held in viewstate
  47.  
  48. If Not isReturning Then ' Only set defaults if is new textbox
  49. With ddlList
  50. .Items.Add(New ListItem("Added at " & Format(Now.AddHours(-1), "HH:mm:ss"), "1")) ' Add some items
  51. .Items.Add(New ListItem("Added at " & Format(Now, "HH:mm:ss"), "2"))
  52. .Items.Add(New ListItem("Added at " & Format(Now.AddHours(1), "HH:mm:ss"), "3"))
  53. .SelectedValue = "2" ' set the selected value
  54. End With
  55. End If
  56.  
  57. ViewState(strId) = strId ' Store the ID of the control in viewstate
  58. End Sub
  59.  
  60. </script>
  61.  
  62. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  63.  
  64. <html xmlns="http://www.w3.org/1999/xhtml" >
  65.  
  66. <head id="Head1" runat="server">
  67.  
  68. <title>Dynamic Controls</title>
  69.  
  70. </head>
  71.  
  72. <body>
  73.  
  74. <form id="formDynamic" runat="server">
  75.  
  76. <asp:scriptmanager id="scriptmanager" runat="server"></asp:scriptmanager>
  77.  
  78. <asp:panel id="holder" runat="server">
  79.  
  80. <table align="center" width="700">
  81.  
  82. <tr>
  83. <td class="header" colspan="2"><h1><a href="../Default.aspx">ASP.net Testing</a>&nbsp;&gt;&gt;&nbsp;Dynamic Controls</h1></td>
  84. </tr>
  85.  
  86. <tr>
  87. <td colspan="2">
  88. <p>Date: 22/04/2008</p>
  89. <p>Example showing the correct way of creating dynamic controls at runtime and maintaining their viewstate between postbacks.</p>
  90. </td>
  91. </tr>
  92.  
  93. <tr>
  94. <td><p>Select From the Following:</p></td>
  95. <td><asp:button id="btnAddTextBox" runat="server" cssclass="input" text="Add Text Box" onclick="btnAddTextBox_Click" />&nbsp;<asp:button id="btnAddDropDownList" runat="server" cssclass="input" text="Add Dropdown List" onclick="btnAddDropDownList_Click" /></td>
  96. </tr>
  97.  
  98. <tr>
  99. <td colspan="2">
  100. <div style="overflow:scroll;">
  101. <asp:placeholder id="plcDynamicContent" runat="server"></asp:placeholder>
  102. </div>
  103. </td>
  104. </tr>
  105.  
  106. </table>
  107.  
  108. </asp:panel>
  109.  
  110. </form>
  111.  
  112. </body>
  113.  
  114. </html>
  115.  

I understand this will take time to implement as this is completely different from the way yours is currently generating them. But this is the reccomended approach.



P.S.
If you are stuck with having to create your controls through a label then why simply precede all checkbox controls name="" attributtes with something you can pickup on when looping e.g. "Dynamic~[CONTROLNAME]".

I.e.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: frankhong is an unknown quantity at this point 
Solved Threads: 0
frankhong frankhong is offline Offline
Newbie Poster

Re: how to get value of all selected checkbox?

 
0
  #5
Oct 20th, 2008
Hi

At first when you create the controls make sure you have added

checkboxtemp.Attributes.Add("runat", "server")

add this code in the buttonclick event
For Each _id As String In Page.Request.Form.AllKeys()
If _id.IndexOf("D_") >= 0 Then 'Just to judge is this the control created by code
End If
Next

Hope this can help, however it works fine in my project
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 144
Reputation: sierrainfo is an unknown quantity at this point 
Solved Threads: 9
sierrainfo sierrainfo is offline Offline
Junior Poster

Re: how to get value of all selected checkbox?

 
0
  #6
Nov 18th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC