ASP.NET DropDownList SelectedIndexChanged event not working - asp.net

I have a basic drop down list. Upon selection of an item in the list, I expect to update the label with the choice of drop down list. But my code section does not show any update. Here is the code:
MainPage.aspx.cs:
protected void Page_Load(Object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Use integers to index each item. Each item is a string.
Dictionary<int, string> fruit = new Dictionary<int, string>();
fruit.Add(1, "Kiwi");
fruit.Add(2, "Pear");
fruit.Add(3, "Mango");
fruit.Add(4, "Blueberry");
fruit.Add(5, "Apricot");
fruit.Add(6, "Banana");
fruit.Add(7, "Peach");
fruit.Add(8, "Plum");
// Define the binding for the list controls.
benimDropDownList.DataSource = fruit;
// Choose what you want to display in the list.
benimDropDownList.DataTextField = "Value";
// Activate the binding.
this.DataBind();
}
}
protected void benimDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
lblSonuc.Text = "You picked: " + benimDropDownList.SelectedItem.Text;
lblSonuc.Text += " which has the key: " + benimDropDownList.SelectedItem.Value;
}
MainPage.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="benimDropDownList" runat="server" OnSelectedIndexChanged="benimDropDownList_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblSonuc" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
What is wrong with it?

Add AutoPostBack="true" to the <asp:DropDownList>:
<asp:DropDownList ID="benimDropDownList" runat="server"
OnSelectedIndexChanged="benimDropDownList_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
By default <asp:DropDownList> do not post back changes to the server.

You might also want to set the DataValueField for your dropdownlist:
benimDropDownList.DataValueField = "Key";

Related

asp.net: ListView on the masterpage

I have some asp.net pages.
I used the masterpage to implement common control.
There is a listview on the masterpage.
And there is a label on the contentpage.
I want to implement below.
When user select a node on the listview of masterpage,
the label text on the contentpage is changed into the text of the node.
How can i implement this.
Could you give me some advice or some link about my question?
Thank you in advance.
On Master1.master, I used ItemCommand event on ListView
<form id="form1" runat="server">
<asp:ListView ID="List1" runat="server" onitemcommand="List1_ItemCommand">
<ItemTemplate>
<p>
<asp:label ID="ItemLabel" runat="server" text="<%#Container.DataItemIndex %>" />
<asp:LinkButton ID="ItemLink" runat="server" CommandName="SelectItem" Text="Select" />
</p>
</ItemTemplate>
</asp:ListView>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
And Master1.master.cs, store selected item text to public property
public partial class Master1 : System.Web.UI.MasterPage
{
public string selectedText { get; set; }
protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "SelectItem")
{
selectedText = ((Label)e.Item.FindControl("ItemLabel")).Text;
}
}
}
Then in Content1.aspx , add a label with id Label1
<asp:Label ID="Label1" runat="server" />
Finally in Conetnt1.aspx.cs, read the property "selectedText" on prerender event (which comes after select click)
protected void Page_PreRender(object sender, EventArgs e)
{
var myMaster = (Master1)this.Master;
Label1.Text = myMaster.selectedText;
}

Cannot update the data in ASPxGridControl

I found that I cannot update the data in my second grid when I clicked on the first grid control.
There are two grids in my program and there is overloaded function GetDataTable2() to get DataTable according to the focused row in grid1.
However, I dun know why the grid2 cannot be updated.
Please help!
protected void Page_Load(object sender, EventArgs e)
{
gv1.DataSource = GetDataTable1();
gv1.KeyFieldName = "ID";
gv1.DataBind();
gv2.DataSource = GetDataTable2();
gv2.DataBind();
}
protected void gv1_FocusedRowChanged(object sender, EventArgs e)
{
gv2.DataSource = GetDataTable2((int)gv1.GetRowValues(gv1.FocusedRowIndex, "ID"));
gv2.DataBind();
}
And the asp.net:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<dx:ASPxGridView ID="gv1" runat="server" SettingsBehavior-AllowFocusedRow="true" SettingsBehavior-ProcessFocusedRowChangedOnServer="true" OnFocusedRowChanged="gv1_FocusedRowChanged" >
</div>
<div>
<dx:ASPxGridView ID="gv2" runat="server" >
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
I think that if you set the ASPxGridView's EnableCallbacks property to false, your code will start working properly. This is the recommended way of using ASPxGridView when it is located within the MS UpdatePanel.
You don't need the UpdatePanel.
In the first grid, use the "RowClick" client side event and perform a callback in the second grid.
ClientSide Event
function gv1_RowClick(s, e) {
gv2.PerformCallback("UPDATE_SECOND_GRID|" + gv1.GetRowKey(e.visibleIndex));
}
ServerSide Event
protected void gv2_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
string[] args = e.Parameters.Split('|');
if (args == null || args.Length == 0)
return;
if (args[0].Equals("UPDATE_SECOND_GRID")) {
//your code with args[1] (key of the first grid)
gv2.DataSource = GetDataTable2();
gv2.DataBind();
}
}

ViewState on programmatically loaded usercontrols (restored after Page_Load)

I'm trying to create a simple web page with a navigation bar and some usercontrols (ascx) programmatically loaded.
All controls are inside an update panel.
When I click on a link button (from the navigation bar) I do the following things:
I save the current usercontrol using viewstate.
Than I reload the current usercontrol.
My 'page_load' always reloads the current control.
Always assigning the same ID to the programmatically loaded control allows me to save the usercontrol viewstate.
So everything look good except one little thing: the usercontrol viewstate in not available during the usercontrol Page_Load!
Look below for (* HERE).
The 'txtTest.Text' value is always "0" (also during postback).
It seems that the user control viewstate is restored after the (usercontrol) Page_Load.
How is it possible?
--- "DEFAULT.ASPX": ---
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="pnlMain" runat="server">
<ContentTemplate>
<div class="links">
<asp:LinkButton ID="lnkButton1" runat="server" OnClick="lnkButton1_Click" Text="Link 1"></asp:LinkButton>
<asp:LinkButton ID="lnkButton2" runat="server" OnClick="lnkButton2_Click" Text="Link 2"></asp:LinkButton>
</div>
<br />
<asp:Panel ID="pnlCtrl" runat="server"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
--- "DEFAULT.ASPX.CS": ---
private string CtrlAscx
{
get
{
if (ViewState["CtrlAscx"] == null)
{
ViewState["CtrlAscx"] = String.Empty;
}
return ViewState["CtrlAscx"].ToString();
}
set
{
ViewState["CtrlAscx"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
loadMyControl();
}
private void loadMyControl()
{
if (!String.IsNullOrEmpty(CtrlAscx))
{
pnlCtrl.Controls.Clear();
Control c = LoadControl(CtrlAscx);
c.ID = CtrlAscx + "ID"; // this line is mandatory in order to mantain the usercontrol viewstate
pnlCtrl.Controls.Add(c);
}
}
protected void lnkButton1_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control1.ascx";
loadMyControl();
}
protected void lnkButton2_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control2.ascx";
loadMyControl();
}
-- "CONTROL1.ASCX" --
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Control1.ascx.cs" Inherits="WebTest.Control1" %>
Control1: <asp:TextBox id="txtTest" runat="server" Text="0"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" />
-- "CONTROL1.ASCX.CS" --
public partial class Control1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (txtTest.Text == "0") // * HERE
{
txtTest.Text = "1";
}
}
}
Try the EnableViewState="true" attribure on txtTest as well as on your custom user control when creating it :
.
.
c.EnableViewState = true;
pnlCtrl.Controls.Add(c);

How to disable first checkbox after other one clicked in repeater

I have a repeater and there is a checkbox. When I click first one in checkedchanged event row information appear down below. But after first click I have a trouble. Sometimes informations are same. Because foreach always see the first click. For instance, I checked second one and I saw the informations. Then I click second one ok again I saw the informations, but this time I clicked again first one. Foreach can take first checkbox and the last one still checked before postback it's again doing second one's operation.
Is there any way to fix this?
Here is my sample code.
<asp:Repeater ID="rptInformations" runat="server">
<ItemTemplate>
<asp:CheckBox ID="ckChoose" runat="server" OnCheckedChanged="ckChoose_CheckedChanged" AutoPostBack="true" />
<div id="foo" runat="server"> ... some basic titles ...</div>
</ItemTemplate>
<asp:Repeater>
<div id="info" runat="server"> ... informations in here (textboxes, labels ..etc)</div>
CodeBehind:
foreach (RepeaterItem item in rptInformations.Items)
{
//CheckBox ckChoose= (CheckBox)sender;
CheckBox ckChoose= item.FindControl("ckChoose") as CheckBox;
if (cBoxChoose.Checked)
{
... database process ...
}
}
As for what I understand you are trying to trigger an event everytime you check the checkbox and it will show certain info according to the row of the repeater it belongs to, for this I have made this example on a page called Repeater.aspx, using an event that will only fire for one checkbox and not for all:
Repeater.aspx:
<asp:Repeater runat="server" ID="repeater">
<ItemTemplate>
<asp:CheckBox ID="ckChoose" runat="server" AutoPostBack="true" OnCheckedChanged="ckChoose_CheckedChanged"/>
<div id="foo" runat="server"> <%# Eval("Field2")%></div>
</ItemTemplate>
</asp:Repeater>
<div id="info" runat="server"> ... informations in here (textboxes, labels ..etc)</div>
Repeater.aspx.cs (Codebehind):
partial class Repeater1 : System.Web.UI.Page
{
private System.Collections.Generic.List<Item> Elements()
{
Generic.List<Item> itemList = new Generic.List<Item>();
itemList.Add(new Item("1", "One"));
itemList.Add(new Item("2", "Two"));
return itemList;
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
this.repeater.DataSource = this.Elements();
this.repeater.DataBind();
}
}
protected void ckChoose_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)sender;
this.info.InnerHtml = "checkbox:" + chk.ID + " foo:" + ((HtmlGenericControl)chk.Parent.FindControl("foo")).InnerText;
}
}
Hope this helps.
I guess I fix it with namingcontainer. It works perfect.
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox cBoxx = item.FindControl("ckChoose") as CheckBox;
current_cbId = cBoxx.NamingContainer.UniqueID.ToString();
if (cBoxx.Checked)
{
... some operations ...
}
if (cBoxChoose.NamingContainer.UniqueID.ToString() != current_cbId)
cBoxChoose.Checked = false;
}

Loop in code block on click argument

Basically what i am trying to do is display a list of categories. And if the admin is logged in
i want to show some buttons next to each category. For example a button to delete it. The problem is that i dont know how to pass a parameter to the function that does the action.
Like i specify that on button click the function 'DeleteCat' must be called but if i cant pass the ID of the category to be deleted this wont work.
I know this can be done with commands and a repeater, but its not an option, i cant use a repeater.
So apparanly this is what i am aiming for:
But of course it does not work.
<%For Each Cat In Category.Children%>
<p class="SubCategory">
<%=Cat.Name%>
<%If User.Identity.Name = "Admin" Then%>
<asp:LinkButton ID="LinkButton6" runat="server" OnClick="AddItem" Text="A+" CommandArgument=<%=Cat.ID %> />
<%End If%>
</p>
<%Next %>
Your event handler AddItem should be able to evaluate the sender of the event and the CommandEventArgs
void AddItem(Object sender, EventArgs e)
{
int result;
bool returnValue;
Button clickedButton = (Button)sender;
returnValue = Int32.TryParse(clickedButton.CommandArgument, result);
// then other stuff happens ...
}
See detailed example here.
edit
Completed code sample as suggested by Brandon.
Alternative solution, using a CheckBoxList
...
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
// bind data to controls
this.itemRepeater.DataSource = Category.Children;
this.adminList.DataSource = Category.Children;
this.itemRepeater.DataBind();
this.adminList.DataBind();
}
// set visibility according to user
this.itemRepeater.Visible = (User.Identity.Name != "Admin");
this.adminList.Visible = (User.Identity.Name == "Admin");
this.adminButton.Visible = (User.Identity.Name == "Admin");
}
protected void AddItem(object sender, EventArgs e)
{
foreach(ListItem currentitem in this.adminList.Items)
{
if(currentitem.Selected)
{
// do something with the selected value
int i;
bool isparsed = Int32.TryParse(currentitem.value, i);
}
}
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<asp:Repeater ID="itemRepeater" runat="server">
<ItemTemplate>
<p><%# DataBinder.Eval(Container.DataItem, "value") %></p>
</ItemTemplate>
</asp:Repeater>
<asp:CheckBoxList ID="adminList" runat="server" />
<br />
<asp:Button ID="adminButton" OnClick="AddItem" runat="server" Text="Add seleted Items" />
</form>
</body>

Resources