neI have a DropDownList with a list of value.
I need in my code USE the value selected by the User in the DropDownList AS INT DataType (numeric).
At the moment I am using this code below (CONVERTING DATATYOES).
Script work just fine.
But I would like to know if exists an alternative way to do it.
Any ideas it is very appreciate. Thanks for your time.
<asp:DropDownList ID="uxPageSizeUsersSelector" runat="server"
AutoPostBack="True"
onselectedindexchanged="uxPageSizeUsersSelector_SelectedIndexChanged">
<asp:ListItem Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
int myPageSize = Convert.ToInt16(uxPageSizeUsersSelector.SelectedValue.ToString());
PageSize = myPageSize;
You can use the try parse method. This will make sure that your value is an int, and if not, allow you to handle the problem instead of throwing an exception:
int myPageSize;
if(int.TryParse(uxPageSizeUsersSelector.SelectedValue, out myPageSize))
{
//SUCCESS
}
else
{
///handle problem here.
}
You can do it for Int16 DateTime etc. too.
Related
I am attempting to get a <div> to appear when a specific ListItem is selected.
In my code behind I have:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (reportedBefore.SelectedItem.Text=="yes")
{
reportedBeforePanel.Visible = true;
}
else
{
reportedBeforePanel.Visible = false;
}
}
I referred to this article here initially, which stated I needed a few things:
You need to Enable the AutoPostBack of the dropdownlist for raising the OnSelectedIndexChanged event on server side.
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged
Admittedly, I did not have an AutoPostBack before. After adding it, I am afraid for some reason the requested div still does not show.
<asp:DropDownList ID="reportedBefore" CssClass="larger-drop-2" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="Unsure" Value="Unsure"></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="reportedBeforePanel" runat="server" Visible="false">
<div id="showDiv">
<label for="yesDetails">
Please provide details
</label>
<asp:TextBox ID="yesDetails" CssClass="third-w-form" runat="server"/>
</div>
</asp:Panel>
Would someone be so kind to help me out here?
The problem is in the following if-condition:
reportedBefore.SelectedItem.Text=="yes"
By this, you are doing a case-sensitive string comparison (this is the default in .NET), but the values in your dropdownlist are written in a different way ("Yes" vs. "yes").
In order to fix this, either perform a case-insensitive string comparison
string.Compare(reportedBefore.SelectedItem.Text, "yes", true) == 0
or change the casing in the if-statement.
C# is case sensitive, so it's "Yes" not "yes":
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text == "Yes";
Alternatievely you can use this:
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
I thought this would be easy and straightforward, but I seem to be having an issue with it.
Basically, I have this in a formview:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
And I want to replace it with this:
<asp:DropDownList ID="StatusDropdown" runat="server">
<asp:ListItem Selected="True" Value="A">Active</asp:ListItem>
<asp:ListItem Value="I">Inactive</asp:ListItem>
</asp:DropDownList>
I'm not exactly sure how to bind this like the Textbox is bound.
Is there a straightforward answer?
Codebehind is not a hack, actually it's best practise since you have full control and compiler support (shows errors at compile time and avoids careless mistakes)
For example (assuming that the DropDownList is in the EditItemTemplate):
private void FormView1_DataBound(object sender, System.EventArgs e)
{
switch (FormView1.CurrentMode)
{
case FormViewMode.ReadOnly:
break;
case FormViewMode.Edit:
// note that the DataSource might be a different type
DataRowView drv = (DataRowView)FormView1.DataSource;
DropDownList StatusDropdown = (DropDownList)FormView1.FindControl("StatusDropdown");
// you can also add the ListItems programmatcially via Items.Add
StatusDropdown.DataSource = getAllStatus(); // a sample method that returns the datasource
StatusDropdown.DataTextField = "Status";
StatusDropdown.DataValueField = "StatusID";
StatusDropdown.DataBind();
StatusDropdown.SelectedValue = drv["StatusID"].ToString();
break;
case FormViewMode.Insert:
break;
}
}
Bind it to the SelectedValue property as so:
<asp:DropDownList ID="StatusDropdown" runat="server" SelectedValue='<%# Bind("Status") %>'>
The value in status has to match the value of a drop down list item.
I'm having a very hard time figuring out what I'm doing wrong here. In my Edit Item Template I have the following code for my drop down list:
<asp:DropDownList ID="dd_is_active" runat="server" AppendDataBoundItems="true"
DataValueField="Enabled">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") %>' />
Here is my aspx.cs code:
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
e.Values["SUB_last_modified_date"] = DateTime.Now.ToString();
e.Values["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.Values["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active")).SelectedValue;
e.Values["Category_ID"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_category")).SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["SUB_last_modified_date"] = DateTime.Now.ToString();
e.NewValues["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.NewValues["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
}
It seems something is either missing from my .cs code or I have the values of 1 and 0 bound incorrectly in the html code. This exact same code works for the Insert Item Template, but the Update (or Edit Item Template) is not working correctly.
When I try to edit an item in my table I get an error stating the input string is in an incorrect format. I know it's trying to bind the Text of "Yes" or "No" but I need to ind to the Values of either "0" or "1". Any help is greatly appreciated!
I think your syntax is wrong for HiddenField value.
Instead of this
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") '>' />
It should be
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled")%>' />
I have a dropdownlist1 that has has 6 collection items including Select One. What I want is this ddl is set to Selectedvalue = null. But what I am getting is my ddl always selecting Select One as its initial value. My ddl properties for Select One is selected:false. How to set this ddl to initial selected value = null?
<asp:DropDownList ID="DropDownList1" runat="server" Visible="False" Width="146px">
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
if (DropDownList1.SelectedValue == null)
{
txtProductName.Text = "";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}
You can add an 'Empty' value item:
<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
Then you would check for
if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="(Select a State)" Value="" />
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
SelectedValue of a dropdownlist would never be null. It could be empty string, but it can never be null...
It would be better to set the Items in this fashion
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
Also SelectedValue of the Dropdown is a string property so better check the same using string.IsNullOrEmpty as it cannot be null, leave the value blank where you want to consider it as null and repeat the same values in the Text and Value part for others
Hera add a default value in the lsit...
<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True">Select One</asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
but try something like seleted item instead of selectedvalue beacuse u have'nt defined values in the list items..
if (string.IsNullOrEmpty(DropDownList1.SeletedItem))
For textbox also
txtProductName = DropDownList1.SeletedItem).ToString();
Use CancelSelectOnNullParameter = False on the SqlDataSource
Make the select value="" that is empty and pass this to your stored procedure like this
dt = ta.GetData(
String.IsNullOrEmpty(DropDownList1.SelectedValue)? null : DropDownList1.SelectedValue,
String.IsNullOrEmpty(DropDownList2.SelectedValue) ? null : DropDownList2.SelectedValue,
String.IsNullOrEmpty(DropDownList3.SelectedValue) ? null : DropDownList3.SelectedValue, null);