i have a datalist contains checkboxlist.
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<asp:CheckBoxList ForeColor="Gray" AutoPostBack="true" OnSelectedIndexChanged="chklist_SelectedIndexChanged" ID="chklist"
runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
can i get the rownumber of datalist on the SelectedIndexChanged event of the checkbox ie;if i have checkbox list control repeated 4times and if i check on the second one how can i get the value 2?
You should try fetch parent of checkboxlist like this
Protected void dtlstfilter_SelectedIndexChanged(object sender, EventArgs e)
{
var control= ((Control)sender).Parent;
if(control is DataListItem)
{
int index=((DataListItem)control).ItemIndex;
}
}
if you not get DataListItem as parent then try parent of parent.
U can Try Following. I added +1 as it is 0 based index..
Protected void dtlstfilter_SelectedIndexChanged(object sender, EventArgs e)
{
lblSelectedIndex.Text = (dtlstfilter.SelectedIndex + 1).ToString();
}
Related
I am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}
Seems simple enough, but I can't figure it out. I've tried this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
foreach (ListItem item in CheckBoxListDivision.Items)
item.Selected = true;
}
}
and this markup:
<asp:CheckBoxList ID="CheckBoxListDivision" runat="server" DataSourceID="SqlDataSourceDivisions" DataTextField="Divisions" DataValueField="Divisions" RepeatColumns="4">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSourceDivisions" runat="server" ConnectionString="<%$ ConnectionStrings:WebPortal_Call4HealthReports_ConnectionString %>" SelectCommand="usp_HR_DivisionsSelectAll" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Thank you for your time and effort.
The reason it's not selecting the items is that Page_Load event is called before binding the CheckBoxList items. So in order to select all the items when page loads you have two options:
First option: Put the same code you're using in the OnDataBound event of the CheckBoxList.
Modify the CheckBoxList markup to this:
<asp:CheckBoxList OnDataBound="CheckBoxListDivision_DataBound"
ID="CheckBoxListDivision" runat="server" DataSourceID="SqlDataSourceDivisions" DataTextField="Name" DataValueField="ID"
RepeatColumns="4" >
</asp:CheckBoxList>
And add this in code-behind:
protected void CheckBoxListDivision_DataBound(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxListDivision.Items)
{
item.Selected = true;
}
}
Second option: Remove the SqlDataSource from your markup and bind the CheckBoxList programatically in Page_Load event, then after binding the CheckBoxList, do the loop and you'll be able to select the items.
Hope this helps.
I want to do some actions in my database when user change dropdown's selectedIndex.Now I have the following.
<td class="shop-item-qty">
<asp:DropDownList ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" runat="server"/>
<asp:HiddenField ID="ItemId" runat="server" Value='<%#Eval("GiftVoucher.ID") %>'/>
</td>
All I want is to get my hidden fields value in changeCount method. The problem is that I can't directly get hidden fields value, because this code is in Repeater element. How can I achieve that functionality?
protected void qtyDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList control = (DropDownList)sender;
RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
if (rpItem != null)
{
HiddenField hiddenField = ((HiddenField)rpItem.FindControl("ItemId"));
}
}
You can bind GiftVoucher.ID value to DropDown's custom attribute and skip HiddenField:
<asp:DropDownList runat="server" ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" data-itemId='<%# Eval("ID") %>' />
protected void changeCount(object sender, EventArgs e)
{
var id = ((DropDownList)sender).Attributes["data-itemId"];
}
I have a DataList and inside it I have a DropDownList:
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
How can I get selectedindexchanged event of DropDownList on the server side? I tried this:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but it is not working.
You have defined the server side method:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but you have not told client side that there is an event for you, so in html code tell it like:
onselectedindexchanged="ddlitem_selectedindexchanged"
and also set AutoPostBack property to true.
From the SelectedIndexChanged event the easiest is to cast the sender to the DropDownList
var ddl = (DropDownList)sender;
The sender is always the control that is the source of the event.
For the sake of completeness, from ItemDataBound of the DataList:
protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
if (ddlitem != null)
{
// ...
}
}
Edit: Have you forgotten to register the event?
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
Note that you should not bind your DataList to it's DataSource on postbacks, otherwise events are not triggered. So check for the IsPostBack property of the page.
For example in page_load:
if(!IsPostBack)BindDataList();
Register the event and set AutoPostBack="true"
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
AutoPostBack="true"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
event (on selected index change you can get the selected value)
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList = (DropDownList)sender;
string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;
}
Not sure if you can't get the selected item on the server or you can't find the way to handle the event. In case your problem is with the event handling, try this
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
AutoPostBack="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
I'm trying to get the item count from a datalist that sits inside a datalist. I thought this is how I would do it but its returning null. (aspx code condensed for readability)
<asp:DataList id="searchResultsProductDataList" runat="server" >
<asp:DataList ID="productDataList" runat="server">
</asp:DataList>
</asp:DataList>
Here is the code-behind
DataList resultnumberDL = (DataList)e.Item.FindControl("productDataList");
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
I've also tried
DataList resultnumberDL = ((DataList)FindControl("productDataList"));
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
This is how I would go about doing this right?
This can be done like this in DataList1_ItemDataBound
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");
Label SalePrice = (Label)e.Item.FindControl("SalePrice");
//
// Do you calculations here ..
//
SalePrice.Text = "Your Final Value";
}
}
Maybe double-check your syntax...
If your ASP.NET controls are structured like this:
<asp:DataList ID="dl1" runat="server" onitemdatabound="dl1_ItemDataBound">
<ItemTemplate>
...
<asp:DataList ID="dl2" runat="server" Enabled="true">
<ItemTemplate>
...
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
and your C# code-behind for the nested DataList like this:
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataList dl2 = (DataList)e.Item.FindControl("dl2");
... // load DataTable
dl2.DataSource = dt;
dl2.DataBind();
}
in this case e.Item.FindControl("[id]") will find your nested DataList