Postback is not working? - c#

I have a dropdown in which I am adding list items dynamically. I set autopostback to true, but nothing seems to happen when I select an item in dropdown.
Mark Up
`<asp:DropDownList runat="server" AutoPostBack="true" ID="restaurant_city_con" CssClass="selectboxindex"></asp:DropDownList>`
Code Behind
`if (!this.IsPostBack)
{
addStates();
showData();
dashboardPageFunction();
ordersPageFunction();
reportsPageFunction();
categoriesPageFunction();
menuPageFunction();
offersPageFunction();
bookingPageFunction();
}
else
{
addCities();
addZipCode();
}`
Is there anything I am doing wrong ?

You need to handle the OnSelectedIndexChanged event, like this:
Markup:
<asp:DropDownList runat="server" AutoPostBack="true" ID="restaurant_city_con"
CssClass="selectboxindex"
OnSelectedIndexChanged="restaurant_city_con_SelectedIndexChanged">
</asp:DropDownList>
Code-behind:
protected void restaurant_city_con_SelectedIndexChanged(object sender,
EventArgs e)
{
// Do something with selected item here
Label1.Text = "You selected " + restaurant_city_con.SelectedItem.Text +
" with a value of " + restaurant_city_con.SelectedItem.Value +
".";
}

Related

Display different Gridviews with a Dropdownlist onchange

I want to show different Gridviews when I select values from a dropdownlist.I use Visible="False" property to gridviews and I want to show only one on every value updated.
Example: when I select the Value "Points" I want to show GridView1 and when I select Names I want to show "GridView2". This is my ASP:
<asp:DropDownList ID="Stats_Ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Stats_Ddl_IndexChanged" >
<asp:ListItem>POINTS</asp:ListItem>
<asp:ListItem>NAMES</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" Visible="False"> blahblahblah1</asp:GridView>
<asp:GridView ID="GridView2" runat="server" Visible="False">blahblahblah12</asp:GridView>
and c# is:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
}
Any suggestions for c# code? thanks...
Hi inside event SelectedIndexChanged
you should put for example
If(Ddl.SelectedValue == "1"){
GridView1.Visible = true;
GridView2.Visible = false;
}else{
GridView1.Visible = false;
GridView2.Visible = true;
}
i see that you have a ListItem don't forget give to a value to property "Value" for example
Point = 1
Name = 2
You can use SelectedItem.Text property of DropDownList to branch
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
if(Stats_Ddl.SelectedItem.Text == "POINTS")
GridView1.Visible = true;
else
if(Stats_Ddl.SelectedItem.Text == "NAMES")
GridView2.Visible = true;
}

DropDown list not firing change event when it is altered

I have a page with a dropdownlist and a button on it. The initial selection on the dropdown is an empty string. I do not want this submitted to the server so I disable the button. I then want to enable the button if any other selection is made on the dropdown. However my ddlBusinessUnit_SelectedIndexChanged method is never hit when I make changes in the dropdown list.
html:
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsDate.Date = DateTime.Today;
PopulateBusinessUnits();
StatusMessages.Visible = false;
}
bGetFiles.Enabled = false;
}
public void ddlBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBusinessUnit.SelectedItem.Text != "")
bGetFiles.Enabled = true;
}
Set AutoPostBack="true" for your dropdown.
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
you're missing AutoPostBack="true" on your asp dropdown control

How I can fill the DropDownList in the page_load

I have a DropDownList and I want to check what language does the browser have and set the values in the dropdown accordingly.
protected void Page_Load(object sender, EventArgs e)
{
string language = Request.UserLanguages[0].ToString().Substring(0, 2);
drpAnrede.DataSource = Server.MapPath("~/App_Data/" + language + ".xml");
}
UPDATE:
I have the solution for this problem...
aspx:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="drpAnrede" runat="server" DataTextField="display" DataValueField="id">
</asp:DropDownList>
<asp:XmlDataSource ID="xmldata" runat="server"></asp:XmlDataSource>
</div>
</form>
c#:
protected void Page_Load(object sender, EventArgs e)
{
string language = Request.UserLanguages[0].ToString().Substring(0, 2);
//drpAnrede.DataSource = Server.MapPath("~/App_Data/" + language + ".xml");
xmldata.DataFile = Server.MapPath("~/App_Data/" + language + ".xml");
drpAnrede.DataSourceID = xmldata.ID;
}
You have to call DataBind() on your dropdown list after you set the datasource, no?
As in:
Databinding DropDown Control in .Net
Assuming the XML is Ok, you need to call
drpAnrede.DataBind();
after applying the datasource.

Problem with a hyperlink

I put a hyperlink inside a datalist..
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server">'<%# Eval("ThreadTitle") %>'</asp:HyperLink>
<br />
<br />
</ItemTemplate>
I want it to enable it to be pressed so that the datalist event will be triggered and transfer me to another page:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
Server.Transfer("AnswerQuestion.aspx?x=" + DataList1.DataKeyField + "&question=" + DataList1.SelectedValue + "&time=" + DateTime.Now);
}
Unfortunately, the link seems to be disabled and I cant press on it to trigger the DataList Selected event..
How can I make the hyperlink active ?
If you want to trigger a selecteditemchaned event use a LinkButton instead of hyperlink.
<asp:DataList ID="DataList1" runat="server"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<asp:LinkButton ID="sjdj" runat="server" CommandName="Select">
<%# Container.DataItem %></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
In the code behind have
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
Server.Transfer("~/jjtestjj.aspx?" + DataList1.DataKeyField);
}
why arent you using the Hyperlink as a hyperlink?
You can set the NavigationURL and Text using the OnItemDataBound (or equivalent) event.
this code works with an asp:Repeater:
protected void Row_DataItem(object row, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.AlternatingItem || args.Item.ItemType == ListItemType.Item)
{
var item = (DataItemPOCO)args.Item.DataItem;
var link = (HyperLink)args.Item.FindControl("HyperLink1");
link.Text = item.LinkText;
link.NavigateUrl = item.URL;
}
}

How to get the previous item on DropDownList before OnSelectedIndexChanged fires the event

How would I get the previous item on DropDownList before OnSelectedIndexChanged fires the event?
Example: I had a DropDownList that has names as its items ("John", "Mark"). By default the SelectedIndex is "John". Upon changing its index and selecting "Mark" the event OnSelectedIndexChanged will be triggered. When I use ddlName.SelectedIndex it will return only the index for "Mark" which I want to get is the index of "John".
You can't capture an event prior to the change, but you could easily store the previous value in a variable. Each time SelectedIndexChanged is fired, use the previous value and then set it to the new index (for the next time the event fires). To handle the case when it's a new selection (from the default), you can either set the variable when the page loads, or allow it to be null and have that alert you to the fact it's a new selection (which you can then handle however you like).
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlName_SelectedIndexChanged">
<asp:ListItem Text="John" Value="1"></asp:ListItem>
<asp:ListItem Text="Mark" Value="2"></asp:ListItem>
<asp:ListItem Text="Jim" Value="3"></asp:ListItem>
</asp:DropDownList>
.cs file code here:
public static int PreviousIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlName.AppendDataBoundItems = true;
ddlName.Items.Add(new ListItem("Other", "4"));
PreviousIndex = ddlName.SelectedIndex;
}
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
string GetPreviousValue = ddlName.Items[PreviousIndex].Text;
Response.Write("This is Previously Selected Value"+ GetPreviousValue);
//Do selected change event here.
PreviousIndex = ddlName.SelectedIndex;
}
You could use the e.OldValues property.
<asp:DropDownList ID="esDropDownList" runat="server" DataSourceID="SqlDataSourceddlEnrolmentStatus" DataTextField="EnrolmentStatusDescription" DataValueField="EnrolmentStatusID" SelectedValue='<%# Bind("StudentEnrolmentStatus") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceddlEnrolmentStatus" runat="server"
ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" SelectCommand="SELECT [EnrolmentStatusID], [EnrolmentStatusDescription] FROM [tblEnrolmentStatuses] ORDER BY [EnrolmentStatusID]">
</asp:SqlDataSource>
And in code behind (assuming your dropdownlist is in a formview) ...
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
..
String msg = "This is the new value " + e.NewValues["StudentEnrolmentStatus"].ToString()+ " and this is the old value " + e.OldValues["StudentEnrolmentStatus"].ToString();
..
}

Categories