By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Maintaining checkbox state after page load inside a repeater control

P: 12
Hi
I have a small problem in maintaining the state of a check box. Please do me a favour by telling me the procedure how to do that.

My requirement is that

"I have to map some roles with that of the users of the project. I have used checkboxes for selecting the roles that a particular user has.

For example, an adminstrator has all roles in an organisation. Similarly an Employee has limited roles.

Here let us take administrator and Employee as users. So to assign roles for each of the user I used check boxes. It works fine.

Now my problem is that if at all I require to modify the roles for a particular user as and when I click the users name the chek boxes corresponding to the role names should be checked.

Not only that I also require the code for databinding the control so that if I select a user from the dropdown list for the first time, the options selected for the precious user should be unchecked.

What I mean by this is,
for the first time if I checked a certain roles for the administartor and immediately I selected the user Employee to assign some roles to the employee.
Then all the check boxes should be unchecked.

Hope I explained my problem clearly. If there is any problem in understanding please let me know.

I am sending my code for checkbox checked state and inserting them into the database. I am using oracle database.
And also Iam sending the HTNL.

Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim MyCheckBox As CheckBox = CType(e.Item.FindControl("chk"), CheckBox)
MyCheckBox.Attributes("ROLE_ID") = e.Item.DataItem("ROLE_ID").ToString()
End If
End Sub
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim rptItem As RepeaterItemCollection = rpt.Items
Dim cb As CheckBox
con.Open()
Dim cmd As New OracleCommand("update TSDB_USER_ROLE set IS_DELETED='Y' where USER_ID='" + ddluser.SelectedValue + "'", con)
cmd.ExecuteNonQuery()
'con.Close()
For Each item As RepeaterItem In rptItem
cb = CType(item.FindControl("chk"), CheckBox)
If cb.Checked Then
HttpContext.Current.Trace.Write("CheckBox Value", cb.Attributes("ROLE_ID"))
Dim str = cb.Attributes("ROLE_ID")
'con.Open()
Dim cmd1 As New OracleCommand("Insert into TSDB_USER_ROLE(USER_ROLE_ID,USER_ID,ROLE_ID,UPDATE DBY) values(SQDB_USER_ROLES.nextval,'" + ddluser.SelectedValue + "','" + str + "',2)", con)
cmd1.ExecuteNonQuery()
End If
Next
con.Close()
End Sub

The above is the code that I used for checking the check boxes and inserting them into the database.

<table id="tbl" width="300" border="1" align="center" cellpadding="1" cellspacing="0" class="greyborder">
<tr>
<td align="center" colspan="2" bgcolor="#dddddd"><p><strong>USER ROLES</strong></p></td>
</tr>
<tr>
<td style="width: 80px"><p>User Name</p></td>
<td><p><asp:DropDownList ID="ddluser" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource1" DataTextField="LOGIN_NAME" DataValueField="USER_ID"></asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SUPPLIERDB %>"
ProviderName="<%$ ConnectionStrings:SUPPLIERDB.ProviderName %>" SelectCommand='SELECT "USER_ID", "LOGIN_NAME" FROM "TSDB_USERMASTER"'>
</asp:SqlDataSource>
&nbsp;&nbsp;
</p></td>
</tr>
<tr>
<td align="center" colspan="2"><p><strong>ROLES</strong></p></td>
</tr>
<tr>
<td>
<asp:Repeater ID="rpt" runat="server" DataSourceID="SqlDataSource3" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<tr>
<td align="center"><p><%#Container.DataItem("ROLE_NAME ") %></p></td>
<td align="center"><asp:CheckBox ID="chk" runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:SUPPLIERDB %>"
ProviderName="<%$ ConnectionStrings:SUPPLIERDB.ProviderName %>" SelectCommand='SELECT "ROLE_ID","ROLE_NAME" FROM "TSDB_ROLES"'>
</asp:SqlDataSource>
&nbsp;&nbsp;


</td>


This is the HTML that I have used. I googled it out for two days. Even then there is no progress. So I am using this form.

Please help me in doing the work.
Thanks in advance for any help provided.


Regards,
Mahathi.
Aug 4 '07 #1
Share this Question
Share on Google+
3 Replies


kenobewan
Expert 2.5K+
P: 4,871
Have you tried setting viewstate to true?
Aug 4 '07 #2

P: 12
Hi
Thank you for the immediate reply.
Where should I set the View State property to true?

Regards
Mahathi.
Aug 4 '07 #3

P: 12
Hi
I could get the values of the check boxes that are inserted in the database using a datareader.

The code used by me is as follows:

Dim cmd As New OracleCommand("Select USER_ID,ROLE_ID from TSDB_USER_ROLE where USER_ID='" + ddluser.SelectedValue + "' and IS_DELETED='N'", con)
Dim dr As OracleDataReader = cmd.ExecuteReader()
While dr.Read
Dim str = dr.Item("ROLE_ID").ToString()
Response.Write(str)
End While

Now please let me know based on these values how to make the checkboxes checked for a particular user in the form. That is the checkboxes that are checked should only be checked.
Thanks in advance for any help provided.

Regards
Mahathi.
Aug 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.