reset selectedvalue when textbox change - c#

I have a textbox and a dropdownlist
the textbox has
<asp:TextBox ID="TxtInizioPeriodo" runat="server"
ontextchanged="InizioPeriodo_TextChanged" AutoPostBack="true" Width="100"></asp:TextBox>
i want if it changes the dropdownlist return to default value selected
i try to pur in page load this:
SelectDestinazione[i].SelectedValue = "";
but it now works.
how can i do?
thanks

In your code you have mentioned like
Array of Dropdownlist ie.,
SelectDestinazione[i].SelectedValue = "";
in the above code "i" selects dropdownlist object from the array of dropdownlists.
Kindly check that one. If its is the single dropdownlist box then use the below solution.
Copy & paste the below code in the server side during the text content change event.
protected void InizioPeriodo_TextChanged(object sender, EventArgs e)
{
SelectDestinazione.SelectedIndex = -1;
}

I assume SelectDestinazione is your dropdownlist
SelectDestinazione.clearSelection();

Related

Can't update using FindControl

I have a TextBox within my EditItemTemplate in my listview_car:
<EditItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Bind("photo1") %>' Visibile="true">
</asp:TextBox>
</EditItemTemplate>
Now in the code I have this in my ItemUpdating event:
protected void listview_car_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var txt1 = listview_car.Items[0].FindControl("txt1") as TextBox;
txt1.Text = "newImage";
}
Now I have debugged it and the value that is showing from my DB is correct, then when I set it from the code using txt1.Text = "newImage"; it shows it has updated the textbox in the Auto's however it does not update in the DB, but the strange thing is when I type in the textbox and click the edit button it updates but doesn't update when I set the string in the code?
Does anyone know what I am doing wrong?
When ItemUpdating() is called all values were already collected in the NewValues collection. To modify any of values you should modify e.NewValues
protected void listview_car_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["key"] = "newImage";
}
where "key" is a name of the key (not a name of the control) which is usually a column name. You could also use an index 0,1,2...
In listview_car_ItemUpdating, first update the database, then call whatever method you used to bind your controls to the database when the page first loaded. This will retrieve the new value of photo1 from the database and bind it to txt1.

Checkboxlist loop is not working

I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work
Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.

get value on edititemtemplate from codebehind

I have a detailsview where I get couple of data from membership profile and I display it on detailsview...this works fine:
<ItemTemplate>
<asp:label ID="FirstName" runat="server" />
</ItemTemplate>
But when I click the edit button, nothing shows up on the field. This is what I am doing on Edit template:
I call ItemUpdating like this:
protected void DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
//I get my memberprofle here
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
Label labelfName = DetailsView1.FindControl("FirstName") as Label;
labelfName.Text = memberp.fName;
}
Should I be using Itemupdated instead? Or is there another method that I should call when the edit button is clicked that will populate the firstname field on edit? Also, the reason I am keeping it as "LABEL"(usually it would be textbox) on edit mode is that this field has to be read only.
The event ItemUpdating is not fired when you enter on edit mode. You must use DataBound event to set properly required text value.
If neccesary, you can ask CurrentMode property of DetailsView to know if you are editing or displaying.
The result looks like this:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
Label l = DetailsView1.FindControl("FirstName") as Label;
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//obtained from your sample
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
l.Text = memberp.fName;
}
else
{
l.Text = "Another text or nothing";
}
}
Be sure to define DataBound event in you Detailsview1 control.
REMARK: It can be affected depending the data bind mode. If so, let me know and put an example.
Add RowUpdating and RowEditing event to your gridview.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

selected index of DDL is always equal to Zero in asp .net

I have a Program Class, with properties as Id, ProgramName,ShortName and Code, im my app
I have a ASP DDL like
<asp:DropDownList ID="DDLProgram" runat="server"
OnSelectedIndexChanged ="OnDDLProgramChanged" AutoPostBack = "true">
</asp:DropDownList>
My OnDDLProgramChanged method is defined as
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
//My Problem goes here
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
My list is getting all the records correctly, I have checked it. But whenever I am changing an item in the DDL, the selected index in not changing. The selected index is remaining zero.Therefore, I am not being able to get the code of other items but the 0 index's item.
Can anyone help me in this case?
You are binding the data again in your selectedIndex Change event and it will reset your current SelectedIndex after rebinding. You don't need to rebind the data to your dropdown in SelectedIndex Change Event
It should be like..
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
You have to bind data to the DropDownList on page load method
if (!IsPostBack)
{
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
}
Otherwise it will bind data everytime and hence clear the selection
Why are you assigning the DataSource in OnDDLProgramChanged, this would be resetting the selection you make.

modifying the query string on dropdown index change in asp code behind

Hi
I have a drop down box that is in the edit template of a formview. I want to be able to add a parrameter to the querystring when the drop down selected index is changed.
I tried
Request.QueryString.Add("tabindex", (sender as WebControl).TabIndex.ToString());
But I got an exception saying the collection is readonly.
Here is my markup
<icms_ref:ReferenceDropDownList ReferenceDataManagerProviderName="ROCSQLReferenceDataProvider"
ID="ddlEnquirerHearType" TabIndex="2" runat="server" ReferenceSetName="EnquiryHearType"
AutoPostBack="true" OnSelectedIndexChanged="EnquirerHearType_SelectedIndexChanged"
DataTextField="ShortName" DataValueField="ReferenceId" />
and here is my code behind.
protected void EnquirerHearType_SelectedIndexChanged(object sender, EventArgs e)
{
var pnlEnquiryHearTypeOther = fvEnquiryInformation.FindControl("pnlEnquiryHearTypeOther") as Panel;
pnlEnquiryHearTypeOther.Visible = DdlEnquirerHearType.SelectedValue == ((int)EnquiryHearType.Other).ToString();
ResetTextBox("txtEnquiryHearTypeOther", fvEnquiryInformation);
Request.QueryString.Add("tabindex", (sender as WebControl).TabIndex.ToString());
}
You can't directly add to the QueryString collection, you have to Response.Redirect to the new Url (modifying the URL to add in the query parameter yourself).
string url = HttpContext.Current.Request.Url.AbsoluteUri + "?tabindex=blah";
Response.Redirect(url, true);

Categories

Resources