pass data from a selected dropdown list

Posted by Jopito under ASP.NET on 10/16/2013 | Points: 10 | Views : 567 | Status : [Member] | Replies : 2
Hellow pals,i am making a small project whereby this is how i intend to use it,

I have a dropdown with fields Items "AGENT,BILLER,CONSUMER" .If the item selected is consumer,then their is a textbox which will allow only entering of Ten numbers and if item selected is Agent,then the Textbox will only allow five numbers,
I am using C#

Thanks

Mark as answer if satisfied


Responses

Posted by: Allemahesh on: 10/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear JB,

You can use the below code to do this:-

<div>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="-- Select --" Text="-- Select --"></asp:ListItem>
<asp:ListItem Value="AGENT" Text="AGENT"></asp:ListItem>
<asp:ListItem Value="BILLER" Text="BILLER"></asp:ListItem>
<asp:ListItem Value="CONSUMER" Text="CONSUMER"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid Data"
ValidationExpression="^[0-9]+$" ControlToValidate="TextBox1" ValidationGroup="val"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="val" />
</div>


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{
if (DropDownList1.SelectedItem.Text.ToUpper() == "CONSUMER")
{
TextBox1.MaxLength = 10;
}
else if (DropDownList1.SelectedItem.Text.ToUpper() == "AGENT")
{
TextBox1.MaxLength = 5;
}
else
{
TextBox1.MaxLength = 100;
}
}


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 10/16/2013 [Member] Starter | Points: 25

Up
0
Down
the code validates the data entered in the textbox but doesnt validate the maximum length the items selected from the dropdown list.The dropdown items have bound them from database but not entered them from the properties section.What the code does it shows "Inalid Entry" if one enters letters,,,it accepts the numbers only,,,The problem is that for the other entries,if i select any of the items,it cannot validate its max length but still it adds the records.any changes you can assist futrher?


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response