checkbox list to generate one textbox - c#

So i have this checkbox list and im trying to generate a textbox when the MISC checkbox is checked.
<asp:CheckBoxList ID="chbxEquipmnt" runat="server"RepeatColumns="4" RepeatDirection="Horizontal" ValidationGroup="Equipment" OnSelectedIndexChanged="chbxEquipmnt_SelectedIndexChanged">
<asp:ListItem Value="Laptop">Laptop</asp:ListItem>
<asp:ListItem Value=" Label Printer">Label Printer</asp:ListItem>
<asp:ListItem Value="Printer">Printer</asp:ListItem>
<asp:ListItem Value="Fax Line">Fax Line</asp:ListItem>
<asp:ListItem Value="PC">PC</asp:ListItem>
<asp:ListItem Value="MFD">MFD</asp:ListItem>
<asp:ListItem Value="Phone Line">Phone Line</asp:ListItem>
<asp:ListItem Value="Misc">Misc</asp:ListItem>
</asp:CheckBoxList>
ON my Cs page I have.........
protected void chbxEquipmnt_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbxEquipmnt.SelectedValue == "Misc")
{
TextBox txt = new TextBox();
txt.ID = "txtMiscCheckBox";
Page.Form.Controls.Add(txt);
}
}
debugging i have tried with Postback which continuously on returns the first check box that was checked...for example i checked laptop instead of Misc first, the value in the debugger always show me laptop. Im not sure if a simple loop to go thru the all the button clicks would help.....I'm fresh out of school and this is a career change for me so thanks for the patience

I would change the approach: don't create the Textbox at run time.
Have it created at design time, hide it by default (Visible property), then show it when Misc option is selected.
You will run into less issues that way. Creating controls in runtime require some extra effort : dealing with ViewState, re-creating the control on PostBack, etc.

Related

How to retrieve value from DropDownList in List View?

I'm trying to retrieve the value in my dropdownlist that is in my list View.
This is my dropdownlist:
<asp:DropDownList ID="ddlSize" runat="server" CssClass="ddl" AppendDataBoundItems="true" >
<asp:ListItem Value="S">Small +RM5.00</asp:ListItem>
<asp:ListItem Value="M">Medium +RM10.00</asp:ListItem>
<asp:ListItem Value="L">Large +RM15.00</asp:ListItem>
<asp:ListItem Value="X">Xtra-Large +RM20.00</asp:ListItem>
</asp:DropDownList>
This is the current way which I used to retrieve the value of my dropdownlist.
protected void prodList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
size = (e.Item.FindControl("ddlSize") as DropDownList).SelectedValue;
}
By using the method above and got an error Object reference not set to an instance of an object which means im passing a null value.
According to the article, OnItemCommand is only triggered if you have a button inside your ListView Control clicked, are you sure that you had the button already, if yes, I think it should work because I've replicated your issue on my machine?
Article

how to show selected text in a dropdownlist?

I have a static drop-down list control in asp.net page and I have two different string (based on a result which I am fetching through database) now I want to make selected text of the the current input and the indexing and values remains same.
<asp:DropDownList ID="ddl_room_type" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_room_type_SelectedIndexChanged">
<asp:ListItem Selected="True">Select Type</asp:ListItem>
<asp:ListItem Text="Deluxe" Value="2500"></asp:ListItem>
<asp:ListItem Text="Super Deluxe" Value="3000"></asp:ListItem>
<asp:ListItem Text="Luxury" Value="5000"></asp:ListItem>
<asp:ListItem Text="Special Room" Value="10000"></asp:ListItem>
</asp:DropDownList>
and I have a string Text from Database : lets say Special Room. Now I want that drop-down list i.e, ddl_room_type should automatically bind with the same text and value.
C# Code which I am using:
string luxury = "Special Room"; //Value coming form Database
ddl_room_type.SelectedItem.Text = luxury;
If I do with this code.. it is just selected the first text as : "Special Room"
Alternately you can try this
string luxury = "Special Room";
ddl_room_type.ClearSelection();
ddl_room_type.Items.FindByText(luxury).Selected = true;
Hope it will be useful
Get your values from the database at first for both Text and Value for loading the dropdownlist. So that you don't have to set it once the user makes the selection.

Selected Index doesn't change

I have an ASP.NET DropDownList control, with a onSelectedIndexChanged event. I also have the AutoPostBack="true" that many have said would fix the problem. However I don't think that is where the problem lays... My Html code and C# code are below for reference. The thing is the code works, but only when I press the enter key while editing the drop down box. If I simply click on an object in the drop down then the event will not fire. If I change the selected item so the "selected" text in the drop down says "ASP" and I then inspect the element using the browser I see that the Selected="True" part of the ListItem is still on the first item... It doesn't change in there. It changes with an enter key but not with a mouse click. Any help is welcome and much appreciated.
HTML:
<div class="ui-widget">
<asp:DropDownList id="Select1" OnSelectedIndexChanged="Select1_SomethingChange" runat="server" AutoPostBack="true">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Select one...">Select one...</asp:ListItem>
<asp:ListItem Value="ActionScript">ActionScript</asp:ListItem>
<asp:ListItem Value="AppleScript">AppleScript</asp:ListItem>
<asp:ListItem Value="Asp">Asp</asp:ListItem>
<asp:ListItem Value="BASIC">BASIC</asp:ListItem>
</asp:DropDownList>
</div>
C#:
protected void Select1_SomethingChange(object sender, EventArgs e)
{
//something is meant to happen here
}
It may be caused by data binding your dropdownlist in Page_Load method.
Please, surround it (data binding) with
if(!IsPostBack){
// data binding.
}
Hope, it help!
AutoPostBack="true"
maybe you miss this option...
Your code works fine, there could be something in code which changes the implementation. I have debug your code and it's showing the selected item in output window. Please verify if there is some javascript code which is causing issue to call dropdown selectedIndexChanged event.
protected void Select1_SomethingChange(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Debug.WriteLine(ddl.SelectedItem.Text);
}

Changing dropdownlist selection causes full postback. (it's more interesting than it sounds)

Here's the scenario.
I have a search page with a TextBox that allows someone to type in a search term and press enter. (Which fires TextChanged). I have a DropDownList that specifies the kind of search that will be performed. It is defined in the markup as follows:
<asp:DropDownList ID="lstSearchType" runat="server" AutoPostBack="false">
<asp:ListItem Value="0">Last, First</asp:ListItem>
<asp:ListItem Value="1">Last</asp:ListItem>
<asp:ListItem Value="2">First</asp:ListItem>
<asp:ListItem Value="3">Liberty ID</asp:ListItem>
<asp:ListItem Value="4">E-mail</asp:ListItem>
<asp:ListItem Value="5">Telephone</asp:ListItem>
<asp:ListItem Value="6">Birthday (exact m/d/yyyy)</asp:ListItem>
<asp:ListItem Value="7">SSN (exact ###-##-####)</asp:ListItem>
<asp:ListItem Value="8">Address</asp:ListItem>
</asp:DropDownList>
As you can see, AutoPostBack is set to false, and there is no event hookup.
Pressing enter fires the OnTextChanged event for the TextBox, which performs a search and updates a GridView in an UpdatePanel. This UpdatePanel has its UpdateMode set to conditional and has one trigger: the TextChanged event of the search TextBox.
It's very simple.
And it works beautifully, almost.
Whenever I change the search type, the very next search does a full postback. All subsequent searches do partial postbacks (as desired) unless I change the search type again.
There is one exception to this rule: if I load the page and immediately change the search type, it doesn't do a full postback. So the first change of the DropDownList before any postback (full or partial) does not trigger a full postback.
Full Disclosure:
I'm doing a lot of JavaScript to change the appearance of the gridview during async requests. I don't detail it here because it seems unrelated. This problem only occurs when a DropDownList with no JavaScript wired up is changed.
Any ideas?
This is driving me crazy. Everything else is working.
Thanks in advance,
Clif
I figured it out. The problem was that the DropDownList was not in an UpdatePanel. It had no way to get the value without doing a full postback. The TextBox was immune to this because of the TextChanged event wiring.

About RadioButton CheckedChanged event

Hi there im using a single TextBox for querying differents types of chossings, for example when i select the first RadioButton (Last Name) I search by Client last name, when i choose second RadioButton (Doc. Code) I search by code and so on, please how can i manage or handle exceptions when for example if user choose "Search by Date" and send a string type?
Im using C# 3.5 - Asp.net
I would like to make it with regular expressions and add that in the RadioButton event, so when users changes radio, he could type just some characters in optionA, other more in Option B, and just dates in OptionC ... (regular expression)
Thanks in advances
if you are using the asp web control radiobuttonlist then you can make lots of changes when their is a postback. you can set the attribute to SelectIndexChanged,so whenever their is a change it cause a postback and then you can do whatever from their(verifications).
ex:
<asp:radioButtonList
id="radio1" runat="server"
autoPostBack="true"
cellSpacing="20"
repeatColumns="3"
repeatDirection="horizontal"
RepeatLayout="table"
textAlign="right"
OnSelectedIndexChanged="radio_SelectedIndexChanged">
<asp:ListItem text="10pt" value="itsMe"/>
<asp:ListItem text="14pt" value="itsYou"/>
<asp:ListItem text="16pt" value="Neither"/>
</asp:radioButtonList>
on the server you should have
protected void radio_SelectedIndexChanged(object sender, EventArgs e)
{
//do whatever you want by calling the name of the radio id
//example
if(radio1.SelectedItem.Value=="(whatever you want to test)"
}

Categories