ASP.Net DropDownList selected value - asp.net

I have a feeling I'm missing something really obvious, I'm not able to capture the selected value of my DropDownList; the value renaubs the first item on the list. I have set the DropListList autopostback property to true. I have a SelectedIndexChangedEvent which is pasted below. This is NOT on the master page.
protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
r_city = ddlRestCity.SelectedValue.ToString();
}
}
Here is the DropDownList control:
<asp:DropDownList ID="ddlRestCity" runat="server"
Width="100px" AutoPostBack="True"
onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>
Thanx in advance for your help!

My off the cuff guess is you are maybe re-populating the list on a post back and that is causing the selected index to get reset.

Where is your DataBind() call? Are you checking !IsPostBack before the call? For example:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
ddlRestCity.DataSource = ...;
ddlRestCity.DataBind();
}
}
Explanation: If you don't check for !IsPostBack before DataBind(), the list will re-populate before SelectedIndexChanged is fired (because Page.Load fires before child events such as SelectedIndexChanged). When SelectedIndexChanged is then fired, the "selected item" is now the first item in the newly-populated list.

What is r_city?
If it's a textbox, then you need to do something like r_city.text = ...
Also -- you might consider removing your postback check. Usually, that's most useful in the page.onload event, and usually, you're checking for if NOT ispostback...

Related

How can I get selected item in DropDownList ASP control in Page_Load method?

I have DropDownList inside my ASP page.
When selection changed postback accured and the Page_Load method fired.
I need to get selected item(selectedValue and selectedIndex) in Page_Load method.
I know that I can use selectedIndexChanged event handler, but in my case it is not
suitable solution because of incorrect architecture.
Any idea how to get selected item in DropDownList control in Page_Load method.
With incorrect architecture is it useful to write
var selectedValue = Request.Params[drpDownList.UniqueID];
You should be OK as long as you check for postback -
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var index = ddlDropDown.SelectedIndex;
// do stuff
}
}
I'm assuming that the control isn't dynamically created.

Devexpress GridLookup autopostback false causing page load

I am using ASPxGridLookup control and I set AutoPostBack="false" for that control, but when I changed the value the normal page life cycle is getting executed what is the solution for this.
<dx:ASPxGridLookup ID="ASPxGridLookup1" runat="server" KeyFieldName="ID" AutoPostBack="false">
</dx:ASPxGridLookup>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dtLookup = new DataTable();
dtLookup.Columns.Add("ID");
dtLookup.Columns.Add("Name");
dtLookup.Rows.Add("1", "Dorababu");
dtLookup.Rows.Add("2", "Vivekh");
ASPxGridLookup1.DataSource = dtLookup;
ASPxGridLookup1.DataBind();
}
}
If you set AutoPostBack to false, changing ASPxGridLookup value initiates callback, not postback. Callback is a special kind of postback that skips some of lifecycle events and doesn't update ViewState.
DevEx: Discussion about Page.IsPostBack and Page.IsCallback values
DevEx: The Concept of Callbacks
Difference between a Postback and a Callback

Why ispostback not working with dropdown box

I want to keep selected item after page reload:
Excerpt from .aspx:
<asp:DropDownList ID="MyDropDown" runat="server" AutoPostBack="true"
onselectedindexchanged="MyDropDown_SelectedIndexChanged">
</asp:DropDownList>
Exerpt from .cs in page_load
if (!IsPostBack)
{
PopulateDropDownList();
}
with
private void PopulateDropDownList()
{
MyDropDown.Items.Add("1");
MyDropDown.Items.Add("2");
}
protected void MyDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl);
}
Response.Redirect refresh the page and you will loose view state that will have selected index. You can put the selected index in session before redirecting.
protected void MyDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
Session["MyDropDownSelectedIndex"] = MyDropDown.SelectedIndex.ToString();
Response.Redirect(Request.RawUrl);
}
You need to populate the drop down list in the Page init event. If you do that during Page load event the view state cannot be restored correctly (because the drop down list is not populated before Page load event) so the on selected index changed event cannot be fired.
EDIT: you may want to cache the data which populate the drop down list to save some around trip to the database. I think you do not need to redirect in the on selected index changed event too.

Raise ItemDataBound Event from Repeater Within User Control to Page

I have a user control that contains a repeater. We have added some paging and sorting into the user control and it all works well.
What I need now is a nice way to catch the OnItemDataBound event of the inner repeater and then bubble it up to be accessible directly from the user control from within the page.
We have tried catching it and then declaring it in the user control but it just won't work. Has anyone ever tried this before and if so could I see some code to suggest how it might look.
Many Thanks
Try something like this:
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
</asp:Repeater>
Then subscribe to the event, and publish another event with the same data
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
OnInnerRepeaterItemDataBound(sender,e);
}
public event EventHandler<RepeaterItemEventArgs> InnerRepeaterItemDataBound;
public void OnInnerRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (InnerRepeaterItemDataBound != null)
InnerRepeaterItemDataBound(sender, e);
}
That should do it, now you can subscribe to the user control Event InnerRepeaterItemDataBound that would be fired when your inner Repeater1_ItemDataBound fire.

How do I get the whole object from a ListView?

I have a asp:ListView control that I bind with a List<CustomObject>.
When Editing records in this ListView control, I can always get the Unique Id of record being edited by using:
int id = Convert.ToInt32(lstView1.DataKeys[e.NewEditIndex].Value);
Is it possible to get the whole object <CustomObject> that is being edited, using any of the ListView properties?
I just figured that out,
We can get the object being edited using following code-
protected void lstView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem objCurrentItem = (ListViewDataItem)e.Item;
**CustomObject obj = (CustomObject)objCurrentItem.DataItem;**
if (objCurrentItem.DisplayIndex == lstView1.EditIndex)
{
TextBox txtTitle = (TextBox)objCurrentItem.FindControl("txtTitle");
txtTitle.Text = obj.Title;
}
}
Here is the answer to your comment to my question:
Yes, the reason it is null in itemcommand and works fine in itemdatabound is that the location itemcommand is not correct for reading this value. You will always get the DataItem null in ItemCommand, no matter what you do. The reason lies in the control life cycle. The control gets initialized, created and then only does any other event related to the control can fire. During control creation the CreateControlHierarchy is called which then uses the DataBind event to create and databind the child controls. At that time the DataItem is live and is not null. Before that and after that it is always null, because its role lies only for that much time span.
By the way the DataItem you are looking at is the item from the related datasource that is being used to databind the listview. The datasource is used only during databinding, hence the DataItem is available only during Item Databound.
Hope this helps !
When you click on the edit for a given item in the listview the ItemCommand event gets fired. The arguments for that event tell that you can get the list item for which that event was fired. You will have to typecast that item properly to get the information you require. The itemcommand event looks like this
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
you have e.Item to use from the ListViewCommandEventArgs.
You item updating you don't have the object available for modifying. You only have the collection of the properties and their values in the new values and old values collections that you get from event arguments. I suppose you can edit the properties of the item over there. Its more or less similar to editing the object itself, because eventually these property values will get transferred to the object using reflection.
<asp:ListView runat="server" ID="list" OnItemCommand="listVideo_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="btDelVideo" runat="server" Text="Delete" OnClientClick="return confirm('Confirm delete ?');" CommandArgument='<%# Eval("KeyID") %>' CommandName="DELETE" />
<asp:LinkButton ID="btEditVideo" runat="server" Text="Edit" CommandArgument='<%# Eval("KeyID") %>' CommandName="EDIT" />
</ItemTemplate>
</asp:ListView>
protected void list_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int videoId = (int)e.CommandArgument;
switch (e.CommandName)
{
case "DELETE":
//Implement Delete event
goto default;
case "EDIT":
//Implement Edit event
goto default;
default:
//Rebind listview
break;
}
}
}

Resources