Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to visible textbox and label based on the selection of dropdownlist value...


XML
<asp:Repeater ID="RepterDetails" runat="server"
       onitemcommand="RepterDetails_ItemCommand">
<ItemTemplate>
 <asp:DropDownList ID="marital" runat="server" class="feedback_textfield"   AutoPostBack="true" OnSelectedIndexChanged="ddlExtended_SelectedIndexChanged">
     <%--  <asp:ListItem>Select</asp:ListItem>--%>
        <asp:ListItem>unmarried</asp:ListItem>
        <asp:ListItem>married</asp:ListItem></asp:DropDownList>

based on the selection of dropdownlist value inside repeater,
i want to visible/invisible textboxes..
XML
<td width="93" align="left" valign="middle"><asp:Label ID="lblwife" runat="server" Text="Wife Name" ></asp:Label></td>
    <td width="12" align="center" valign="middle"><asp:Label ID="Label2" runat="server" Text=":" ></asp:Label></td>
    <td width="150" align="left" valign="middle"><asp:Label ID="lbl_wife" runat="server" Text='<%# Bind("wifename")%>' Font-Bold="true"></asp:Label>
        <asp:TextBox ID="txtwife" runat="server" class="feedback_textfield" Text='<%# Bind("wifename")%>' Visible="false"></asp:TextBox></td>
    </tr>

I have tried like this..
protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e)
        {
         DropDownList marital = (DropDownList)RepterDetails.FindControl("marital");
         TextBox txtwife = (TextBox)RepterDetails.FindControl("txtwife");
         if (marital.SelectedValue == "married")
            {
                txtwife.Visible = true;
            }
            else
            {
                txtwife.Visible = false;
            }
        }

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRepeater();
}
 
}
 
private void BindRepeater()
{
SqlConnection SqlCnn = new SqlConnection(s1);
SqlCommand SqlCmd = new SqlCommand("select * from registration", SqlCnn);
SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
DataSet ds = new DataSet();
SqlAd1.Fill(ds, "registration");
RepterDetails.DataSource = ds;
RepterDetails.DataBind();
}

While selecting Dropdown list value, I cant able to use the selected value..
Bcoz Its returning null Value..

Error:
Object reference not set to an instance of an object.

if (marital.SelectedValue == "married")
..

can anyone suggest me how to access dropdownlist selected value inside repeater..


Thanks in Advance
Posted
Updated 22-May-14 23:48pm
v3
Comments
Sunasara Imdadhusen 22-May-14 8:39am    
@Priya, can you please provide code snippet of Page_Load()
Sunasara Imdadhusen 23-May-14 5:48am    
please use Improve Question link to add or update with code, do not past code in comment area.

Hello Priya,

Can you please try this solution.
C#
protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e) {
 DropDownList MyDropDown= (DropDownList)sender;
 string txtwife = MyDropDown.SelectedValue;
}
 
Share this answer
 
protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in RepterDetails.Items)
        {
            
            DropDownList drp = item.FindControl("marital") as DropDownList;
            TextBox txtwife = item.FindControl("txtwife") as TextBox;
            
            string wife = drp.SelectedValue;
            if (wife == "unmarried")
            {
                txtwife.Visible = false;
            }
            else
            {
                txtwife.Visible = true;
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900