简体   繁体   中英

How to Visible textbox from datalist

I want to visible and invisible my textbox and label from my datalist. My datalist is fulfill with checkboxes. so, i mean i have many checkbox that integrated to database.

I have 2 main data. 1 as the Header and 1 as the subHeader, every subHeader has many checkboxes called access. every access has id_access(i save it into HiddenField 'id_access') and every Header has id_jenis (i save it into HiddenField 'id_jenis_access'). if the access (checkbox text) show 'Others' (id_access = 'ACT5') and the Header show 'Others' (id_jenis = 'JO1') then the label Reaseon and Discribtion with their textbox will show up. but i have problem.

this is my form

<asp:DataList ID="DataListTest" runat="server" OnPreRender="PreTes">
        <ItemTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
        <td>
        <asp:Label ID="lblHeader" runat="server"></asp:Label>  <!-- Telephone, Bussines System -->                                    
        </td>

        </tr>
        <tr>
        <td>
        <asp:Label ID="lblsubheader" runat="server" /></td>    <!-- Oracle, EPM, CRM (Muncul di Form) -->
        </tr>

        <tr>
        <td>
        <asp:HiddenField ID="id_jenis_access" runat="server" Value='<%Eval(idJenisAccess) %>' /> <!-- Output = JT1, JO1, JBS1 (ID_JENIS) -->
        <asp:HiddenField ID="subhd" runat="server" Value='<%# Eval("sub_jenis") %>' /> <!-- Oracle, EPM, CRM (nama sub jenis) --> 
        <asp:HiddenField ID="id_access" runat="server" Value='<%# Eval("id_access") %>' /><!-- Output = ACT5, ACBS1 -->
        <asp:HiddenField ID="hd" runat="server" Value='<%# Eval("nama_jenis") %>' /><!-- Output = Telephone, Bussines System -->
        </td>
        </tr>

        <tr>
        <td>
        <asp:CheckBox ID="cbCountryName" runat="server"  Text='<%# Eval("nama_access") %>' /> <!-- Output = Local, Handphone, Project -->
        <asp:TextBox ID="testme" runat="server" Text="" Visible="false" /> <!-- Output = Textbox for all -->            
       </td>
        </tr>

        <tr>
        <td><asp:Label ID="lblReason" runat="server" Visible="false" text="Reason : "/>
        <asp:TextBox ID="txtReason" Text="Reason" runat="server" Visible="false" />    
        </td>                   
        </tr>

        <tr>
        <td>
        <asp:Label ID="lblDescription" runat="server" Visible="false" text="Description : "/>
        <asp:TextBox ID="txtDescription" runat="server" Text="" Visible="false" />
        </td>
        </tr>
   </table>
        </ItemTemplate>
    </asp:DataList>

this is my code to call all my data from database

 private void ShowDataList()
{
    conn.Open();
    string sql = "Select access.id_access as 'id_access', access.nama_access, jenis_access.id_jenis_access as 'idJenisAccess' , "+
                 "jenis_access.nama_jenis_access as 'nama_jenis', sub_jenis.nama_sub_jenis as 'sub_jenis',sub_jenis.id_sub_jenis "+
                 "FROM access LEFT JOIN detil_access ON access.id_access = detil_access.id_access "+
                 "LEFT JOIN jenis_access ON detil_access.id_jenis_access = jenis_access.id_jenis_access "+
                 "LEFT JOIN sub_jenis ON detil_access.id_sub_jenis = sub_jenis.id_sub_jenis "+
                 "ORDER BY jenis_access.id_jenis_access";
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    dt = new DataTable();
    adp.Fill(dt);
    DataListTest.DataSource = dt;
    DataListTest.DataBind();
}

My problem is my lblReason, txtReason, lblDescribtion, and txtDescription is cannot show. this is my code on PreRender datalist, i called it PreTes.

protected void PreTes(object sender, EventArgs e)
{
    string temp = ""; 
    string subtemp ="";
    foreach (DataListItem item in DataListTest.Items)
    {
        Label objLabel = (Label)item.FindControl("lblHeader");
        Label subjenis = (Label)item.FindControl("lblsubheader");
        TextBox t = (TextBox)item.FindControl("testme");
        CheckBox objName = (CheckBox)item.FindControl("cbCountryName"); // Internet, User Folder, etc (the Access)
        HiddenField objHD = (HiddenField)item.FindControl("hd"); // Telephone, Busines System
        HiddenField subobjHD = (HiddenField)item.FindControl("subhd"); // Oracle, CRM , EPM
        HiddenField id_access = (HiddenField)item.FindControl("id_access"); // ACT5, ACBS1, etc (code Access)
        HiddenField id_jenis = (HiddenField)item.FindControl("hdIdJenisAccess"); // JTO1, JBS1, etc (code Jenis)            
        TextBox tr = (TextBox)item.FindControl("txtReason");
        TextBox td = (TextBox)item.FindControl("txtDescription");
        Label lblReason = (Label)item.FindControl("lblReason");
        Label lblDescription = (Label)item.FindControl("lblDescription");

        if (temp != objHD.Value)
        {
            temp = objHD.Value;
            objLabel.Text = temp + "<br/>";
        }
        if (subtemp != subobjHD.Value)
        {
            subtemp = subobjHD.Value;
            subjenis.Text = subtemp + "<br/>";
        }

        if (id_access.Value == "ACT5" && id_jenis.Value == "JO1")
        {
            lblDescription.Visible = true;
            td.Visible = true;
            lblReason.Visible = true;
            tr.Visible = true;
        }

    }

So, What should I do ?

FYI:i'm a newbie here and also c# programmer

after : HiddenField id_access = (HiddenField)item.FindControl("id_access");

add; var value_id_access = id_access.Text;

some things for other values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM