How to bind dropdownlist using asp.net c#? - asp.net

<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10">
<asp:ListItem Text="100" Value="100">
<asp:ListItem Text="1000" Value="1000">
<asp:DropDownList/>
I have bind from codebehind file but I can't see same.
Please help me.

You need to use some sort of datasource to bind. You are just showing static bound data here.

In markup:
<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="1000" Value="1000" />
</asp:DropDownList>
In code-behind:
ddlSampleSize.Items.Add(new ListItem("10", "10"));
ddlSampleSize.Items.Add(new ListItem("100", "100"));
ddlSampleSize.Items.Add(new ListItem("1000", "1000"));

Related

How to add ajax combobox properly in asp.net webform application

Right now i'm trying to use combobox in the application i'm working by following this page.
But when i run the page, only the dropdown is working but the auto suggestion feature is not working and also the text field is not editable. So how to do it?
Here is the code i'm using:
<cc1:ComboBox ID="ComboBox1" runat="server"
AutoPostBack="True"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append" >
<asp:ListItem Text="Mild" Value="0" />
<asp:ListItem Text="Medium" Value="1" />
<asp:ListItem Text="Hot" Value="2" />
</cc1:ComboBox>
Change your DropDownStyle to Simple or DropDown and you'll get the desired result, here is the updated Code:
<cc1:ComboBox
ID="ComboBox1"
runat="server"
AutoPostBack="True"
DropDownStyle="Simple"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append">
<asp:ListItem Text="Mild" Value="0" />
<asp:ListItem Text="Medium" Value="1" />
<asp:ListItem Text="Hot" Value="2" />
</cc1:ComboBox>

HTML5 "required" attribute on asp:DropDownList

Let's say I have this DDL on my page:
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
In this one, the required field will work without problem, if you don't select a value, then it will block the submit.
But what if the initial value is 0?
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
Now the "required" attribute doesn't work anymore.
What I want to do is put a pattern in the value to only accept values greater than zero, but I'm not familiar with RegExp, and I don't know if the HTML "pattern" attribute will do what I want.
Can someone enlighten me?
I would rather use a solution that uses pure HTML, but if a minimal programming is required, then I don't have choice...
If you just want to write mark up the RequiredFieldValidator should work
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqSenha" runat="server" SetFocusOnError="true" InitialValue="0" ErrorMessage="*" ControlToValidate="senha" />

Why won't my function execute (ASP.NET)?

Basically I have a aspx page function that will not seem to activate. I've created a function that's supposed to run on selectedindexchange of a dropdownmenu.
<asp:TableCell>Credit Card Type: <asp:DropDownList
id="DropDownCredit"
OnSelectedIndexChanged="creditType"
Runat="server">
<asp:ListItem Text="Select" Value="Empty" />
<asp:ListItem Text="Visa" Value="Visa" />
<asp:ListItem Text="Mastercard" Value="Mastercard" />
<asp:ListItem Text="Discover" Value="Discover" />
<asp:ListItem Text="American Express" Value="American Express" />
</asp:DropDownList>
There as you can see I added my function name to run when they select something. Now my function itself is extremely simple.
Protected Sub creditType()
If DropDownCredit.SelectedIndex.ToString.StartsWith("A") Then
TextBoxCardNumberAmerican.Enabled = True
Else
TextBoxCardNumberOthers.Enabled = True
End If
End Sub
By the way I have set the two textboxes mentioned in the above function to disabled inititally, so only if one is correct then it is enabled. I am going to guess that the string isn't being interpreted properly. Any help would be great thankyou.
Two things: Your creditYpe method has the wrong signature it should be:
Protected Sub creditType(Object o, EventArgs e)
..and add the AutoPostBack="true" property to your asp control
Credit Card Type: <asp:DropDownList
id="DropDownCredit"
OnSelectedIndexChanged="creditType"
AutoPostBack="true"
Runat="server">
<asp:ListItem Text="Select" Value="Empty" />
<asp:ListItem Text="Visa" Value="Visa" />
<asp:ListItem Text="Mastercard" Value="Mastercard" />
<asp:ListItem Text="Discover" Value="Discover" />
<asp:ListItem Text="American Express" Value="American Express" />
</asp:DropDownList>
you should add the AutoPostBack="True" property to the dropdown
only then the server side sub will be executed
Please set the
AutoPostBack="True"
On the dropdown. Like this:
<asp:DropDownList id="DropDownCredit"
OnSelectedIndexChanged="creditType" AutoPostBack="True" Runat="server">
The the event will fire

Keep file name in FileUpload control after postBack

I have got problem with FileUpload control. I have this one, two drop down list, text box and button. If I select in first dropDownList "Yes" second one become disable and set value on NO (In second ddl I have two option YES or NO and in first one as well) however if I select NO in first dropDownList I posible to choose both option in second dropDownList. First ddl change second one on postBack using selectedIndexChanged evet and when it happends I loose file name in UploadFile control which I set before.
Code sample:
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:DropDownListID="ddlSymmetry"runat="server" AutoPostBack="true"
onselectedindexchanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0"/>
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2"/>
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
What should I do to keep file name in UploadFile control during changes selected options in drop down lists?
Try this, I added the label so you can see that the postback of the onselectedindexchange only affects the dropdown and not the file upload control, hope this helps.
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlSymmetry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2" />
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
<asp:Label runat="server" ID="msgFromList" />
</ContentTemplate>
</asp:UpdatePanel>
protected void ddlSymmetry_SelectedIndexChanged(Object sender, EventArgs e)
{
msgFromList.Text = ddlSymmetry.SelectedItem.Value.ToString();
}
First thing keep in ur mind that is FileUpload Control Will became empty if any post back event occur on ur web page. So best solution is to put ur file upload control after all controll that can cause a post back like drop down list.

Databinding of RadioButtonList using SelectedValue...possible?

I am databinding a GridView to an object datasource. The gridview contains a TemplateField which contains a RadioButtonList with ListItems defined inline.
I want to be able to databind the SelectedValue of the RadioButtonList to the same underlying table as the other grid columns, but it doesn't work!
Do I have my syntax wrong, or is this impossible and requires looping code to individually select the proper item in each row?
<llblgenpro:LLBLGenProDataSource ID="llbComputerApplication" DataContainerType="EntityCollection" runat="server"></llblgenpro:LLBLGenProDataSource>
<asp:GridView ID="gridComputerApps" DataSourceID="llbComputerApplication" runat="server" AutoGenerateColumns="False"
EmptyDataText ="NO APPLICATIONS FOUND FOR THIS COMPUTER."
DataKeyNames="ComputerID, ApplicationID" EnableViewState="False"
style="border-style:dotted;border-width:thin"
>
<Columns>
<asp:BoundField DataField="ApplicationID" HeaderText="Application ID" SortExpression="ApplicationID" Visible="True" />
<asp:TemplateField HeaderText="Application Name"><ItemTemplate><%#Eval("Application.ApplicationName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Normalized Name"><ItemTemplate><%#Eval("Application.NormalizedAppName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Notes"><ItemTemplate><%#Eval("Application.NormalizedNotes")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>' ID="rblRequirementOption" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="Need Now" Text="Need Now"></asp:ListItem>
<asp:ListItem Value="Need Someday" Text="Need Someday"></asp:ListItem>
<asp:ListItem Value="Do Not Need" Text="Do Not Need"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NormalizedNotes" HeaderText="Notes" Visible="False" />
</Columns>
</asp:GridView>
What you have should work. Are you getting an error? Here's a working example copied from my current project. I'm binding to a nullable bit field - so have a hidden list item to accept the nulls.
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
I also experienced this problem (nothing selected in radiobuttonlist) when binding against boolean values in MS SQL:
radDefault.Items.Add(new ListItem("Yes", "true"));
radDefault.Items.Add(new ListItem("No", "false"));
In my case, the solution was to capitalize the first letter of the true/false values, then the radiobuttonlistworked as expected:
radDefault.Items.Add(new ListItem("Yes", "True"));
radDefault.Items.Add(new ListItem("No", "False"));
Or, declaratively:
<asp:RadioButtonList runat="server" ID="radDefault" SelectedValue='<%# Bind("DB_FIELD") %>'>
<asp:ListItem Value="False" Text="No" />
<asp:ListItem Value="True" Text="Yes" />
</asp:RadioButtonList>
I didn't like the idea of using css to hide an item. Instead, I found this solution to add a blank item but remove it in the code behind.
<asp:RadioButtonList ID="MyRadioButtonList" runat="server"
SelectedValue='<%# Bind("Blah") %>'
OnDataBound="MyRadioButtonList_DataBound">
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>
<asp:ListItem Value="C"></asp:ListItem>
</asp:RadioButtonList>
protected void MyRadioButtonList_DataBound(object sender, EventArgs e)
{
RadioButtonList list = (RadioButtonList)sender;
ListItem blank = list.Items.FindByValue("");
if (blank != null)
list.Items.Remove(blank);
}
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" selected="true" style="display: none" />
</asp:RadioButtonList>
It work me .....
gnanasekar.s vilangulathur

Resources