SelectedIndexChanged doesn't work! - c#

My code:
*.aspx:
<asp:DropDownList ID="CountryList" CssClass="CountryList" runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged" />
*.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
CountryList.SelectedIndexChanged +=
new EventHandler(CountryList_SelectedIndexChanged);
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
But this doesn't work.

Try setting AutoPostBack="true" on this dropdown list:
<asp:DropDownList
ID="CountryList"
CssClass="CountryList"
runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged"
AutoPostBack="true"
/>
Also you don't need to manually wire-up the event handler in the Page_Load method. It will be automatically done by ASP.NET when it compiles the webform:
protected void Page_Load(object sender, EventArgs e)
{
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}

I think you missed AutoPostBack="true" Property in aspx file

Add AutoPostBack="true" in ur aspx code and everything will work as you thought.

Related

Checking if the value of a textbox is changed or not

I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}

persist ddl values and selected value in between postbacks

I have a save button and a drop down list on a page. Inside the page Load, the drop down list is populated if !Page.PostBack (AutoPostBack=false). So, the first time I load the page, the drop down list is populated. I also have a save method to go with the save button. When this button is clicked, it should do something with the selected value of the drop down list. My problem is that the drop down list has no value (is null) inside the button save method. How would you fix this?
Markup:
MyClass.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MyClass" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainRegion" runat="server">
<div>
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="myDdlChange" ViewStateMode="Enabled" EnableViewState="true" />
</div>
<br />
<div style="min-width: 300px; max-width: 770px;">
<asp:TextBox id="txtBox" runat="server" TextMode="MultiLine" />
</div>
<div class="buttonContainer">
<span >
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</span>
</div>
</asp:Content>
Then, in the code behind:
MyClass.aspx.cs
public class MyClass
{
protected global::System.Web.UI.WebControls.DropDownList myDdl;
protected global::System.Web.UI.WebControls.TextBox txtBox;
protected global::System.Web.UI.WebControls.Button btnSave;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack)
Session["selectedID"] = myDdl.SelectedValue; // my attempt to put the selected value from ddl in a session var, to use it later inside the save method but it didn't work
if (!Page.IsPostBack)
{
//create array1 here
myDdl.Items.Clear();
myDdl.Items.AddRange(array1);
Session["selectedID"] = myDdl.SelectedValue;
myDdlChange(null, null);
this.DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
//do something based on myDdl.SelectedValue (which shouldn't be null)
}
protected void myDdlChange(object source, EventArgs e)
{
txtBox.Text = myDdl.SelectedValue;
}
}
}
I think the main problem is you should be using Page_Load instead of OnLoad.
You don't need to use Session to remember the SelectedValue.
Try something like this which works for me...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateDropdown();
}
private void PopulateDropdown()
{
myDdl.Items.Clear();
var array1 = new ListItem[3];
array1[0] = new ListItem("item1", "item1");
array1[1] = new ListItem("item2", "item2");
array1[2] = new ListItem("item3", "item3");
myDdl.Items.AddRange(array1);
myDdl.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
var selectedVal = myDdl.SelectedValue; // putting a breakpoint here shows myDdl.SelectedValue is not null
}
protected void myDdlChange(object sender, EventArgs e)
{
}

What is wrong in this code snippet, writing server side variable on the page as property of asp.net server controls

ASPX Page
<asp:Label ID="lbk" runat="server" Text='<%= _imgPath %>' />
Code behind
protected void Page_Load(object sender, EventArgs e)
{
_imgPath = "MyName";
}
My expectation was that it should render
<span id="lbk">MyName</span>
But it is rendering
<span id="lbk"><%= _imgPath %></span>
Is this correct behavior?
Try this:
<asp:Label ID="lbk" runat="server" Text='<%# _imgPath %>' />
protected void Page_Load(object sender, EventArgs e)
{
lbk.Text = "MyName";
}
you dont need _imgPath.
For something simple like setting the text of a Label, use the Page_Load event in the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lbk.Text = "MyName";
}
}
Of if you'd rather use a script on the page, you can do this:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lbk.Text = "MyName";
}
}
</script>

Modify an asp:BulletedList in C#

I've different buttons like this
<asp:Button ID="button1" runat="server" Text="single" title="single" EnableViewState="false" />
When this button is clicked I want that the text in a list like this changes
<asp:BulletedList ID="list1" runat="server" EnableViewState="false">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
<asp:ListItem>Item4</asp:ListItem>
</asp:BulletedList>
How in C# can I select the first, the second or the third element of the list?
protected button_Click(object sender, EventArgs e)
{
list1. ...;
}
Thanks.
Use ListControl.Items Property.
protected button_Click(object sender, EventArgs e)
{
list1.Items[0] ...;
}
For example if you want to change you Item1 to dir, use this :
list1.Items[0].Attributes["class"] = "dir";

OnClick event not firing on ModalDialog

I have a Modal Dialog:
function ShowPopup()
{
window.showModalDialog('dialog.aspx', null, 'status:no;dialogWidth:950px;dialogHeight:150 px');
}
Then its called in the code behind
Page.ClientScript.RegisterStartupScript(this.GetType(), "popUpScript", "ShowPopup();", true);
The dialog.aspx has two buttons:
<asp:Button id="btn1" runat="server" Text="Button 1" OnClick="btn1_Click"></asp:Button>
<asp:Button id="btn2" runat="server" Text="Button 2" OnClick="btn2_Click"></asp:Button>
However, the Click events in the code behind are never getting fired.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, System.EventArgs e)
{
Response.Redirect(url)
}
protected void btn2_Click(object sender, System.EventArgs e)
{
Response.Redirect(url);
}
}
I recall seeing this problem before and I think it is related to caching. Try adding this to your dialog.aspx page_load method when !IsPostBack
Response.AddHeader("Pragma", "no-cache")
To stop the browser caching the page and reusing it
Are you receiving any hidden JavaScript errors preventing the POST?

Categories