Show text boxes depending on drop down box selection - c#

I want to show multiple text boxes depending on the number a user selects from a dropdown box. Example below:
So whatever number is selected in the drop down the page refreshes and displays that number of text boxes. I need to go up to 20 fields. Is there a way to do this in C#, or maybe with the Ajax Control Toolkit?
ASPX
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>

sure there is.
use:
int ctrlCount=Convert.ToInt32(DropDownList1.SelectedItem.Value);
int ctrlTopPos=30;
lbl_name.Text="Name:";
for(int i=0;i<ctrlCount;i++)
{
Label lbl_name=new Label();
TextBox txt_cur=new TextBox();
txt_cur.Top=ctrlTopPos+(i*30);
lbl_name.top=ctrlTopPos+(i*30);
txt_cur.left=lbl_name.Width+30;
Panel1.Controls.Add(lbl_name);
Panel1.Controls.Add(txt_cur);
}
Create an asp:panel and name it Panel1.
Put the given code inside the SelectedIndexChanged event of your dropdownlist.
set the autopostback property of your dropdownlist to true.
it will work.
hope that helps.

try this
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<div>
<asp:PlaceHolder id="ContentPlaceHolder1" runat="server" />
</div>
protected void NumAccountsList_SelectedIndexChanged(object sender, EventArgs e)
{
ContentPlaceHolder1.Controls.Clear();
for(i=0; i<Convert.ToInt32(EmpNameList.SelectedItem.Value); i++)
{
TextBox tx= new TextBox();
tx.ID="tx"+i;
ContentPlaceHolder1.Controls.Add(tx);
ContentPlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}

Related

Clicked on Textbox it should select the radiobutton in c#

I have a Radiobuttonlist. In items i was adding Specify your own value with One textbox. I f i clicked on that text-box radio-button should select b y default.
<td style="text-align:left" class="contract_value_bg" width="50%">
<asp:RadioButtonList ID="rblDocumentstType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblDocumentstType_SelectedIndexChanged" RepeatColumns="1">
</asp:RadioButtonList>
<asp:TextBox ID="txtRFP" runat="server" AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120" />
</td>
enter image description here
You want to set focus on the textbox if "specify your own value" was clicked in the RadioButtonList? Or you want to select that item in the RadioButtonList if the user clicked into the TextBox?
This is for the former case:
protected void rblDocumentstType_SelectedIndexChanged(Object sender, EventArgs e)
{
RadioButtonList rblDocumentstType = (RadioButtonList) sender;
if(rblDocumentstType.SelectedIndex == 1)
{
txtRFP.Focus();
}
}
If you want to select the second RadioButtonList item if the TextBox got focus you should do that at client-side by handling the onfocus event with javascript (or jQuery):
<asp:TextBox ID="txtRFP" runat="server" onfocus="selectSpecifyYourOwn()" AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120" />
function selectSpecifyYourOwn() {
var rbID = '<%=rblDocumentstType.ClientID %>';
var rb = document.getElementById(rbID);
var items = rb.getElementsByTagName("input");
items[1].checked = true;
}

asp:label change visibility after hiding it

I've got an asp:Label and a asp:DropDownList that I want to be able to switch back and forth between visible and invisible when clicking on some buttons. Right now, my code looks like
aspx file
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>
</asp:Label>
<asp:Button Text="ALL" ID="AllTabButton" CssClass="tabButton" runat="server" OnClick="AllTab_Click" />
<asp:Button Text="Arrived" ID="ArrivedTabButton" CssClass="tabButton" runat="server" OnClick="ArrivedTab_Click" />
code behind
protected void AllTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButtonClicked";
ArrivedTabButton.CssClass = "tabButton";
statusFilter.Visible = true;
statusFilterLabel.Visible = true;
}
protected void ArrivedTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButton";
ArrivedTabButton.CssClass = "tabButtonClicked";
statusFilter.Visible = false;
statusFilterLabel.Visible = false;
}
The only problem is that if I try to set Visible=true after setting Visible=false it would give me an error Unable to find control with id 'statusFilter' that is associated with the Label 'statusFilterLabel'.
I tried doing some other things instead of using Visible, like setting the style: statusFilter.Style.Add("display", "block") and setting the cssclass: statusFilter.CssClass = "displayBlock"but the resulting error always showed up.
An asp:Panel would work, but I'm avoiding using that because I want my asp:Label and asp:DropDownList to line up with several other labels and dropdownlists; putting in a panel would make them not line up properly.
I'm guessing there is something I'm missing, something I just don't get, but I can't seem to figure out what that is. If anybody has any clue as to what's happening, I would really appreciate the help!
It's not able to always find the control on postback because it's a child of statusFilter. Move the input field outside of the label:
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
</asp:Label>
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>

Update panel with textbox values when button clicked

I am still learning the basics of C# so any help would be appreciated. I have a series of asp:TextBox's. In the code behind, I am getting the value of these boxes. Is there a way to have a panel hidden until a user clicks submit then have the values display?
Here is the HTML for one of the boxes and the panel:
<asp:TextBox ID="txtTitle" runat="server></asp:TextBox>
<asp:Panel ID="PDFPanel" runat="server"></asp:Panel>
The button:
<asp:Button ID="btn_Submit" runat="server" Text="Button" OnClick="btnSubmit"/>
and the code-behind for it:
string Title = txtTitle.Text;
public void btnSubmit(Object sender, EventArgs e)
{
}
There are about 50 fields, so I am not showing all of it but if I can get direction on one I can replicate for the rest. Please let me know if I need to show any additional code
I am sorry if this is simple, but like I said, I am still an entry level developer. Thanks in advance!
Unless I've misunderstood what you're asking, this should be fairly simple.
<asp:Panel ID="PDFPanel" runat="server" Visible="False">
<div>
<asp:Literal id="litTitle" runat="server" />
</div>
</asp:Panel>
then in your click method:
litTitle.Text = txtTitle.Text;
PDFPanel.Visible = true;
Set the Panel's visibility to false by default
<asp:Panel ID="PDFPanel" runat="server" Visible="false">
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
</asp:Panel>
then on the Button's click event set the visibility to true
public void btnSubmit(Object sender, EventArgs e)
{
PDFPanel.Visible = true;
// do something else...
}

SelectedIndexChange not firing in asp.net

I'm using dropdownlist in asp.net which contains 10,20,50 as values.
I'm using gridview to display dataretrieve from table based on the value selected in dropdownlist.
Example, when I select 20, the gridview should display only 20 rows.
I'm using the following coding;
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName .Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
I've 30 rows in table.
My problem is, when I select 50, it shows all rows. But when I select 10, the SelectedIndex function is not firing.
At the same time, after selecting 50, when I select 20, the selectedindex is firing.
What is the problem?
Here is the updates .aspx page coding:
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" >
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
Problem : as your items order is 10,20,50...etc., it is cleared that 10 is at first location and when you select the 10 as first selection Index will not get changed.
Reason: IndexChanged Event only fires when SelectedItem Index is changed.
ut when you select some other item 20 or 50 and then select 10 it definitely fires.
Solution :
Add a Default item to DropDownList as --Select Item-- so that whenever user selects item 10 it fires the event as Selected Index is changed.
Try This:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="28px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="201px">
<asp:ListItem>-Select Item-</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>50</asp:ListItem>
</asp:DropDownList>
The Problem is in not understanding when this event can be fired.. it can fired only when the index of the dropdownlist changed and that didn't happened in your case, because the first element when the dropdownlist rendered is 10.
In my humble opinion, the optimum solution according to that issue is rendering the dropdownlist in the page with a primer default selection like that :
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" AppendDataBoundItems="true">
<asp:ListItem Value="0">--Choose--</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName.Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
Add ViewStateMode="Enabled" to the DropDownList it should work.
I got information from here quite often.
I just want to share my experience this time and hope it might help someone someday.
I have a DropDownList in my GridView's PagerTemplate.
I used the example code from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(VS.80).aspx
I used it in many of my pages. All works fine except one.
The OnSelectedIndexChanged event just isn't fired in that one page.
Later I set the "EnableViewState" of that GridView to false, it works finally.

Use checkBox and textBox in DataList

I have created a DataList in asp.net -
<asp:DataList runat="server" ID="pTextBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
This creates checkBoxes and textBoxes based on the string values passed through from a webService.
How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??
You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
DataListItem item = (DataListItem) chk.NamingContainer;
TextBox txt = (TextBox) item.FindControl("profileTextBox");
this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}
By the way, this approach works with any web-databound control(Repeater, GridView, etc.)

Categories