Maintaining checkbox state after page load inside a repeater control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mahathi
    New Member
    • Jul 2006
    • 12

    Maintaining checkbox state after page load inside a repeater control

    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_ItemDataBou nd(ByVal sender As Object, ByVal e As RepeaterItemEve ntArgs)
    If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType = ListItemType.Al ternatingItem Then
    Dim MyCheckBox As CheckBox = CType(e.Item.Fi ndControl("chk" ), CheckBox)
    MyCheckBox.Attr ibutes("ROLE_ID ") = e.Item.DataItem ("ROLE_ID").ToS tring()
    End If
    End Sub
    Protected Sub btnUpdate_Click (ByVal sender As Object, ByVal e As System.EventArg s) Handles btnUpdate.Click
    Dim rptItem As RepeaterItemCol lection = 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.Selecte dValue + "'", con)
    cmd.ExecuteNonQ uery()
    'con.Close()
    For Each item As RepeaterItem In rptItem
    cb = CType(item.Find Control("chk"), CheckBox)
    If cb.Checked Then
    HttpContext.Cur rent.Trace.Writ e("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,US ER_ID,ROLE_ID,U PDATEDBY) values(SQDB_USE R_ROLES.nextval ,'" + ddluser.Selecte dValue + "','" + str + "',2)", con)
    cmd1.ExecuteNon Query()
    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="greybord er">
    <tr>
    <td align="center" colspan="2" bgcolor="#ddddd d"><p><strong>U SER ROLES</strong></p></td>
    </tr>
    <tr>
    <td style="width: 80px"><p>User Name</p></td>
    <td><p><asp:Dro pDownList ID="ddluser" runat="server" AutoPostBack="t rue" DataSourceID="S qlDataSource1" DataTextField=" LOGIN_NAME" DataValueField= "USER_ID"></asp:DropDownLis t><asp:SqlDataS ource ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:SUPPLIERDB %>"
    ProviderName="< %$ ConnectionStrin gs:SUPPLIERDB.P roviderName %>" SelectCommand=' SELECT "USER_ID", "LOGIN_NAME " FROM "TSDB_USERMASTE R"'>
    </asp:SqlDataSour ce>
    &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="S qlDataSource3" OnItemDataBound ="rpt_ItemDataB ound">
    <ItemTemplate >
    <tr>
    <td align="center"> <p><%#Container .DataItem("ROLE _NAME") %></p></td>
    <td align="center"> <asp:CheckBox ID="chk" runat="server" AutoPostBack="t rue" />
    </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSou rce ID="SqlDataSour ce3" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:SUPPLIERDB %>"
    ProviderName="< %$ ConnectionStrin gs:SUPPLIERDB.P roviderName %>" SelectCommand=' SELECT "ROLE_ID","ROLE _NAME" FROM "TSDB_ROLES "'>
    </asp:SqlDataSour ce>
    &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.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Have you tried setting viewstate to true?

    Comment

    • Mahathi
      New Member
      • Jul 2006
      • 12

      #3
      Hi
      Thank you for the immediate reply.
      Where should I set the View State property to true?

      Regards
      Mahathi.

      Comment

      • Mahathi
        New Member
        • Jul 2006
        • 12

        #4
        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.Selecte dValue + "' and IS_DELETED='N'" , con)
        Dim dr As OracleDataReade r = cmd.ExecuteRead er()
        While dr.Read
        Dim str = dr.Item("ROLE_I D").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.

        Comment

        Working...