Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
I have text box and dropdownlist in my page.TextBox is the usercontrol.
here i have to switch the textbox's required and cssclass property based on the selection of drop down list.


ie,
if I select "1" from drop down list, I have to change the textbox as required field and corresponding cssclass
if I select any other value except 1 ,here I want to change textbox as normal TextBox and corresponding cssclass


Thanks in advance,
Posted

in source code
design viiew


<asp:dropdownlist id="vv" runat="server" autopostback="True" forecolor="#FF3399" xmlns:asp="#unknown">

<asp:listitem value="0" text="1" style="color:Red;">
<asp:listitem value="1" text="2" style="color:Green;">
<asp:listitem value="2" text="3" style="color:Maroon;">
<asp:listitem value="3" text="4" style="color:Blue;">





<asp:textbox id="tt" runat="server" xmlns:asp="#unknown">









and server side put this code

in vb.net implement to c#

Protected Sub vv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles vv.SelectedIndexChanged
tt.Text = vv.SelectedItem.ToString()
Dim al As ListItem = vv.SelectedItem
Dim s As String = al.Attributes("style").ToString.Substring(6)
s = s.Substring(0, s.Length - 1)
tt.ForeColor = Drawing.ColorTranslator.FromHtml(s)
End Sub
 
Share this answer
 
Comments
Deepthi Aravind 2-Jun-11 8:29am    
I think you dont understand my requirement..
please read the question carefully...
Try this,

In .aspx page,

<asp:dropdownlist id="ddlTxt" runat="server" onclick="ddlfn()" autopostback="true">
<asp:listitem value="0" text="1">

<asp:listitem value="1" text="2">

<asp:listitem value="2" text="3">

<asp:listitem value="3" text="4">


<asp:textbox id="txtDynamic" runat="server" xmlns:asp="#unknown">

<script language="javascript" type="text/javascript">
function ddlfn()
{
var keyValue =document.getElementById('<%=ddlTxt.ClientID %>').options[document.getElementById('<%=ddlTxt.ClientID%>').selectedIndex].value;
if(keyValue==0)
document.getElementById('<%=txtDynamic.ClientID %>').value="1";
else
document.getElementById('<%=txtDynamic.ClientID %>').value="2,3,4";
}
</script>

........
@Nidhish
 
Share this answer
 
v2
Comments
Deepthi Aravind 3-Jun-11 3:20am    
Nidish..
Please read the question..I need to set the textbox properties( required or not and cssclass) based on the selected value of dropdown
try this,
In .aspx page,

function CheckMandatory()
{
var keyValue =document.getElementById('&lt;%=ddlDynamic.ClientID %&gt;').options[document.getElementById('&lt;%=ddlDynamic.ClientID%&gt;').selectedIndex].value;
if(keyValue==0)
{
if(document.getElementById('&lt;%=txtDynamic.ClientID %&gt;').value=="")
alert('Please enter a value');
}
}


<asp:dropdownlist id="ddlDynamic" runat="server" onselectedindexchanged="ddlDynamic_OnSelectedIndexChanged" autopostback="true" xmlns:asp="#unknown">
<asp:listitem value="0" text="1">

<asp:listitem value="1" text="2">

<asp:listitem value="2" text="3">

<asp:listitem value="3" text="4">


<asp:textbox id="txtDynamic" runat="server" cssclass="style1" xmlns:asp="#unknown">
<asp:button id="btnDynamic" runat="server" text="Submit" onclientclick="return CheckMandatory()" xmlns:asp="#unknown">
--------------------------------------------------------------------------
In .aspx.cs page,
protected void ddlDynamic_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (ddlDynamic.SelectedIndex == 0)
txtDynamic.CssClass="style1";
else
txtDynamic.CssClass = "style2";
}
 
Share this answer
 
Comments
Deepthi Aravind 6-Jun-11 0:50am    
I tried it..bt its not wrking..

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