How to get asp.net dropdownlist to allow selections - c#

I have a dropdownlist on a webform that I fill from a sql query, I then want to be able to select individual items in the dropdown and have corresponding fields from a datatable fill textboxes on the form
Problem is rowSel is returning 0 and the dropdown won't let me select any other item it always snaps back to the first itenm in the list.
Thought this might have something to do with autopostback being set to true, but if I set it to false that causes otheer problems, Not sure what else to try Im a winforms person and very new to asp.net
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
int rowSel = ddClients.SelectedIndex;
txtClient.Text = dsShow.Rows[rowSel["ClientsTableFieldA"].ToString();
}
It should allow me to select a value from the drop down then populate some textboxes with fields from the datatable.

You could try:
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Value.ToString();
}

protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Text;
}
As suggested by B. Seberle
DDL items have fields value and text so it depends how you bound the ddl to SQL datasource.
place break point on txtClient.Text = ddClients.SelectedItem.Text see if item list is empty.
it should not be necessary but you can force a ddClient.databind() in page_load if(!Page.IsPostback).
however ddClients_SelectedIndexChanged will only trigger on postback.

Related

why my radcombobox is empty after postback?

I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element

How to get dropdownlist selected value in Page_Init event c#

I tried below code in Page_Init() but i am getting null values. How to get Dropdownlist selected values in Page_Init().
protected void Page_Init(object sender, EventArgs e)
{
string test1 = Request.Form[ddlProjectResource.Text];
string test2 = Request.Form[ddlProjectResource.SelectedValue];
}
This will give you the SelectedValue of ddlProjectResource:
Request.Form[ddlProjectResource.UniqueID];
If you list is not populated from an external data-source then you should be able to use:
(DropDownList)page.FindControl(ddlProjectResource.UniqueID).SelectedItem;
Saying that the first solution is more performant as it only has to scan through the collection of values in the Form collection instead of scan through the whole html page.

Get original object from DataRow in GridView C#

I try to display a collection (IEnumerable) of objects (generated via Linq to Sql). Therefore I bind the Gridviews DataSource property to the generated output of my Linq to SQL method. In the SelectedIndexChanged event of the GridView I try to convert the selected rows DataItem back to my original object but end up with a null value instead.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
RlDataContext dc = new RlDataContext();
this.dgvReports.DataSource = dc.GetReports(1);
this.dgvReports.DataBind();
}
protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dgvReports.SelectedIndex >= 0)
{
Report rpt = (Report)this.dgvReports.SelectedRow.DataItem;
}
}
The return type of GetReports is ISingleResult<Report>
Use a bindingsource between your datagridview and your list. When a selection is made is datagridview use the bindingsource's Current property to get you the right item from the list.
This is how I always get my information from a datagridview
string variabel = yourDataGridView["Columnname"].ToString();
You can also use this in a loop then it will be:
string variabel = yourDataGridView["RowNumber"].Cells["Columnname"].Value.ToString();
I hope this helps!

Changing the FormView record while in insert mode

I have 2 ObjectDataSource's - 1 for the GridView and 1 for the FormView.
I enabled selection on the GridView and all is well; the page loads, you select a record, and the FormView populates with the record.
However, once the FormView is in Insert mode, when I select a record on the GridView, it is not changing back to display the record I selected.
Where could I handle this in the code, as I don't see how to access the "Enable Selection" on the GridView in code behind?
What I have tried
protected void grdSearchGroup_SelectedIndexChanged(object sender, EventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
protected void grdSearchGroup_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
I think the simplest way to solve your problem is to handle the GridView's SelectedIndexChanging method, and set the FormView back to read-only mode (that way you can see the records details in the FormView like you want).
protected void yourGridView_SelectedIndexChanging(Object sender, EventArgs e)
{
yourFormView.ChangeMode(FormViewMode.ReadOnly);
}
Note that you must call the FormView's ChangeMode method in order to update the mode. Just setting the DefaultMode property is not enough.

finding value of dynamically created combobox

I'm using VS2010,C#, I have a table that its data should be created dynamically (from an SQL server table), I have to add a combobox (with 3 items) to one of the columns, this combo box is also created dynamically, then I give each combo a unique ID, it has autopost back set to off and also enableviewstate and viewstatemode to true and enabled, when users changes values for some combo boxes (each row has a combox), and then presses the submit button, I want to have current state of my comboboxes but their selectedindex is 0 so I cannot use them, what should I do? what are my options? (I find each combobox using FindControl and unique ID of the combobox)
thanks
Please find below answer for your above questions
First of all you need to register onchange event of combobox in Javascript while create dynamic combobox.
Put one hidden field on page
And then put the code in onchange event, set the value in hidden field using Clientid from onchange event and then get the value of hidden field from server side.
you can use postback.. here is a sample code snippet.. if you want to work on postback then then follow this.. else you can follow the approach as Rahul told you..
public partial class DynamicCombo : System.Web.UI.Page
{
DropDownList list;
protected void Page_Init(object sender, EventArgs e)
{
Table table = CreateHtmlTable();
list = new DropDownList();
list.AutoPostBack = true;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.ID = "cbo";
list.Items.Add(new ListItem("value1", "1"));
list.Items.Add(new ListItem("value2", "2"));
list.Items.Add(new ListItem("value3", "3"));
table.Rows[0].Cells[0].Controls.Add(list);
pnl.Controls.Add(table);
}
private void list_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("<script>alert(\"" + list.SelectedIndex + "\");</script>");
}
protected void Page_Load(object sender, EventArgs e)
{
}
private Table CreateHtmlTable()
{
Table table = new Table();
table.Rows.Add(new TableRow());
table.Rows[0].Cells.AddRange(new TableCell[] { new TableCell(),
new TableCell(),
new TableCell()});
return table;
}
}

Categories