I'm handling the onSelectIndexChanged event. An event is raised when the DropDownList selection changes. the problem is that the DropDownList still returns the old values for SelectedValue and SelectedIndex. What am I doing wrong?
Here is the DropDownList definition from the aspx file:
<div style="margin: 0px; padding: 0px 1em 0px 0px;">
<span style="margin: 0px; padding: 0px; vertical-align: top;">Route:</span>
<asp:DropDownList id="Select1" runat="server" onselectedindexchanged="index_changed" AutoPostBack="true">
</asp:DropDownList>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
Here is the DropDownList OnSelectedIndexChanged event handler:
protected void index_changed(object sender, EventArgs e)
{
decimal d = Convert.ToDecimal( Select1.SelectedValue );
Literal1.Text = d.ToString();
}
Do you have any code in page load that is by chance re-defaulting the value to the first value?
When the page reloads do you see the new value?
If you are using AJAX you may also be doing a callback, not a full postback. In that case you may want to use this in your page load method:
if (!IsCallback && !IsPostBack)
{
// Do your page setup here
}
add this:
if page.isnotpostback {
}
around your code to bind the dropdownlist.
This may seem obvious, but anyway.
Do you initialize this dropdown with an initial value in some other event handler like OnLoad ?
If so you should check if that event is risen by a postback or by the first load. So you should have something like
if(!IsPostback) d.SelectedValue = "Default"
Is it possible that you have items copied throughout your datasource for the drop down list?
Related
In my DataList I want a button to be displayed with a FA icon, so I used a html button and made it runat="server", now when I click the button I want to know which Datalist Item is 'bounded' with this button.
I tried to use a asp.net button, but I can't use FA icons then.
This is what my html and c# code looks like:
<asp:DataList Width="100%" ID="dtlFAQSections" runat="server" DataSourceID="dtsFAQSections" DataKeyField="FAQSectionID">
<ItemTemplate>
<h2>
<button id="btnFAQSection" runat="server" onserverclick="btnFAQSection_Click" style="background-color:#f24646; border:none; color:white; margin-left:25px; font-size:16px; cursor:pointer; height: 26px; width: 26px; margin-right: 5px; border-radius:30%;"><i class="fas fa-plus"></i></button>
<asp:Label Font-Size="18px" ID="FAQSectionNameLabel" runat="server" Text='<%# Eval("FAQSectionName") %>' />
</h2>
<hr style="border: 1px dotted #000000; border-style: none none dotted; color: #fff; background-color: #fff;"/>
</ItemTemplate>
</asp:DataList>
protected void btnFAQSection_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
}
First, you need to use HtmlButton since you are not using an ASP control as a Button. Then simply find the Parent.
protected void btnFAQSection_Click(object sender, EventArgs e)
{
HtmlButton btn = (HtmlButton)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
//now you can access the DataListItem
Label label = item.FindControl("FAQSectionNameLabel") as Label;
label.Text = "DataListItem Found";
//or if you want to get the parent DataList
DataList dl = btn.Parent.Parent as DataList;
Label1.Text = dl.ID;
}
Hello guys i have a problem with iframe. I want to play video in asp.net and i used iframe.
<iframe runat="server" id="videoPlayer" onended="handlerEnd" style="align-content: center; position:absolute; border:0; top:0; left:0; right:0; bottom:0; width:100%; height:100%" src="http://www.w3schools.com/html/mov_bbb.mp4" frameborder="0" allowfullscreen></iframe>
I want to use onended event from iframe to use in aspx.cs file.
How to get it ?
Put a hidden field to your page and set it's value to 0.
Set event handler to onended event, when it fires, set value 1 to hidden field and do postback.
On Page_Load event check if hidden field contains 1.
Aspx file
<asp:HiddenField runat="server" ID="videoEndedFlag" />
<iframe onended="raiseVideoEnded" />
function raiseVideoEnded()
{
var field = document.getElementById('<%=videoEndedFlag.ClientID%>');
field.value = '1';
// do postback
window.location.reload(true);
// or window.location.href = window.location.href;
// or __doPostBack('','');
}
Aspx.cs file
protected void Page_Load()
{
if(IsPostBack)
{
if(videoEndedFlag.Value == "1")
{
// video ended
}
}
}
#layer1 {
width: 575px;
height: 400px;
background-color: #E0E0EB;
position: absolute;
top: 36px;
left: 222px;
}
#layer2 {
width: 575px;
height: 400px;
background-color: #E0E0EB;
position: absolute;
top: 36px;
left: 222px;
visibility:hidden;
}
And i have some controls on both of the divs....
<asp:HyperLink ID="HyperLink1" runat="server" onclick="f1();" NavigateUrl="#">Add Personal Details</asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" onclick="f2();" NavigateUrl="#">Add Personal Details</asp:HyperLink>
On Click Of HyperLink i have following Code...
function f1() {
document.getElementById("layer1").style.visibility = "visible";
document.getElementById("layer2").style.visibility = "hidden";
}
function f2() {
document.getElementById("layer1").style.visibility = "hidden";
document.getElementById("layer2").style.visibility = "visible";
}
And i have a button..
<asp:Button ID="Button5" runat="server" Text="Button" />
Everything works fine when i click on HyperLink but when i CLICK on BUTTON which i hv in div2 due to postback Page Reset occurs and div1 is showing.which is true as per PostBack.But I want div2 only to displayed aftr button click.Can anyone provide me code for that...Please Help...
If you're trying to prevent Button5 from causing postback then add onclientclick="return false;" to the control. Otherwise you can just handle the visibility of your divs in the click event handler. Something like this:
protected void Button5_Click(object sender, EventArgs e)
{
div1.Style["display"] = "block";
div2.Style["display"] = "none";
}
In order to access your divs in codebehind you may need to make them server controls by adding runat="server"
I think you know what happens when there is a postback, Page is reloaded. So, you have to handle the case that when it is postback retain the old value (IsPostback)
Its been a while since I used asp but here goes.
You can store varables in a Viewstate object and check the varable upon loading the program. For example
function f1() {
document.getElementById("layer1").style.visibility = "visible";
document.getElementById("layer2").style.visibility = "hidden";
ViewState("layer") = "1"
}
function f2() {
document.getElementById("layer1").style.visibility = "hidden";
document.getElementById("layer2").style.visibility = "visible";
ViewState("layer") = "2"
}
When you load the page you do something similar to this
String strLayer = ViewState("layer").ToString();
if(strLayer.equals("2"))
f2();
You may find more information here that could help
http://www.dotnetuncle.com/aspnet/75_viewstate.aspx
Hope this helps.
All of this happens within the same user control so that shouldnt make a difference.
<asp:Repeater ID="rptActivity" runat="server" OnItemCreated="rptActivity_ItemCreated">
<ItemTemplate>
<div class="under-label">
<div class="activity">
<%#Eval("ActivityName")%>
<input type="hidden" name="activityId" value='<%#Eval("ActivityId")%>' />
</div>
<div class="status">
<asp:DropDownList ID="ddlStatuses" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id" runat="server"></asp:DropDownList>
</div>
<div class="comment">
<textarea name="comments" cols="35" rows="3" name="comment" style="float: left; margin: 0px 0px 0px 25px; font-family: Geneva, Arial, Helvetica, sans-serif;"><%#Eval("Comment")%></textarea>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
The i have the following code in the repeater's itemcreated event:
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
and when i try to use ddl it throws NullReferenceException.
Any ideas?
Since your drop down list is inside the repeater, make sure you reference the DataItem to find the control.
Make sure to use e.Item.FindControl rather than Page.FindControl -- Page.FindControl will not find this item because it will not recursively search the page
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
Try to modify your ItemCreated eventHandler like below and see if it works.
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item){
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
}
I need to find the div which belongs my current clicked Link button from the code behind file in c#. Then i need to apply the class for that div. So far i tried to access by html table cell. But i cant able to access my div. So please any valuable suggestion to find the div?
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" style="width:100%">
<ItemTemplate>
<div runat="server" id="DivContent" style="padding-top: 25px; height: 65px;" align="center"
onmouseover="this.className='MsgClick'" onmouseout="this.className=''" >
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("UserName") %>' CommandName="show"
class="InnerMenuFont"></asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("AdminId") %>' Visible="False"></asp:Label>
<br />
</div>
<hr style="width: 80%" />
</ItemTemplate>
</asp:DataList>
In the above code i need to access the current div in the id "DivContent"
.MsgClick
{
background-image: url('Images/AdminHighlight.png');
background-repeat: no-repeat;
vertical-align: top;
margin-right: -38px;
padding-right: 30px;
}
above code is my class file.
use below code
Control objDiv = e.Item.FindControl("DivContent");
hope it will help you
Might be wrong, but I don't believe it's possible to access a div from code behind if the div is part of a data bound control (DataList, GridView, etc), even if you have runat="server" set.
You can however swap the div for asp:Panel - which renders as a div when the page is served up to the browser. e.g.
<asp:Panel ID="DivContent" runat="server">
div content... LinkButton, Label etc...
</asp:Panel>
When the LinkButton is clicked, you can find the asp:Panel in your code behind using the item index of the DataList to which the LinkButton belongs:
LinkButton OnClick event:
protected void LinkButton2_Click(object sender, EventArgs e)
{
// Get the Link Button which has been clicked.
LinkButton btn = (LinkButton)sender;
// Get the DataListItem in the DataList which contains the LinkButton which was clicked.
DataListItem listItem = (DataListItem)btn.NamingContainer;
// Get the ItemIndex of the DataListItem.
int itemIndex = listItem.ItemIndex;
// Find the asp:Panel in the DataListItem of the DataList (e.g. DataList1).
Panel currentPanel = (Panel)DataList1.Items[itemIndex].FindControl("DivContent");
}
You should then hopefully be able to change style settings form code behind.
You could possibly use the CssClass property to change what you need:
...
Panel currentPanel = (Panel)DataList1.Items[itemIndex].FindControl("DivContent");
currentPanel.CssClass = "NewClassName";
Just did this myself good sir. There is a much simpler solution!
From your c# code behind, a div would be refereed to as type HtmlGenericControl.
You will need to add a onitemdatabound="DataList1_ItemDataBound" to your DataList control markup.
Inside your itemdatabound event you can modify the div.
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
// Find the div control as htmlgenericcontrol type, if found apply style
System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("DivContent");
if(div != null)
div.Style.Add("border-color", "Red");
}