943,590 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 61403
  • ASP.NET RSS
Oct 24th, 2005
0

how to get value of all selected checkbox?

Expand Post »
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
Click image for larger version

Name:	rights_forum.gif
Views:	1826
Size:	21.1 KB
ID:	1476  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frmsasp is offline Offline
8 posts
since Sep 2005
Nov 9th, 2005
0

Re: how to get value of all selected checkbox?

Quote 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.

Quote ...
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.
Quote ...
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
Reputation Points: 10
Solved Threads: 1
Light Poster
kedar_challa is offline Offline
47 posts
since Nov 2005
May 5th, 2008
0

Re: how to get value of all selected checkbox?

I have the same problem. Can you tell me exactly how to use the above code snippet
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaunavon is offline Offline
1 posts
since Jan 2008
May 6th, 2008
1

Re: how to get value of all selected checkbox?

The method you are using is depreciated and you should create your controls dynamically using the built in methods.

E.g.
ASP.NET Syntax (Toggle Plain Text)
  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)

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Oct 20th, 2008
0

Re: how to get value of all selected checkbox?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frankhong is offline Offline
1 posts
since Oct 2008
Nov 18th, 2008
0

Re: how to get value of all selected checkbox?

Reputation Points: 10
Solved Threads: 9
Junior Poster
sierrainfo is offline Offline
144 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: insert data Queries.
Next Thread in ASP.NET Forum Timeline: credit card validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC