I have a checkbox list inside a datalist:
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
<p>
<asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server" ></asp:CheckBoxList>
</p>
</div>
</ItemTemplate>
</asp:DataList>
And I loaded two list items in this to say 'yes' and 'no'. How can i get the event in the selected checkbox?
You need to bind SelectedIndexChanged event and pass the arguments to get the current rowNumber or anything else you need using custom attributes (user defined attributes).
In html
<asp:DataList ID="dtlstfilter" runat="server" >
<ItemTemplate>
<div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
<p>
<asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server" onselectedindexchanged="chklist_SelectedIndexChanged" CommandName="myCommand" CommandArguments="1" DataListRowNumber="1" ></asp:CheckBoxList>
</p>
</div>
</ItemTemplate>
</asp:DataList>
In Code behind
protected void chklist_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList chklst = (CheckBoxList)sender;
string commandName = chklst.Attributes["CommandName"].ToString();
string commandArguments = chklst.Attributes["commandArguments"].ToString();
string dataListRowNumber = chklst.Attributes["DataListRowNumber"].ToString();
}
Another way, just to think about. This solution minimizes the roundtrips between client and server.
Use a button's click event. You can iterate through the DataListItems and use the FindControl method to find the CheckBoxList. Now you can determine which elements are checked:
foreach (DataListItem item in dtlstfilter.Items)
{
if (item.ItemType == ListItemType.Item)
{
CheckBoxList checkBox = item.FindControl("chklist") as CheckBoxList;
}
}
Related
Get LinkButton Embedded Label value?
Also issue with LinkButton on postback comes back empty.
I basically need a clickable row to run a server side function is there a better way then a LinkButton?
I'm basically creating a search dropdown.
ASPX
<asp:ListView ID="listView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<ItemTemplate>
<asp:Repeater ID="SearchResults" OnItemCommand="SetValues_ItemCommand" runat="server">
<ItemTemplate>
<div class="form-row">
<asp:LinkButton ID="MemberInfo" runat="server" class="list-group-item list-group-item-action flex-column align-items-start mb-2" OnClick="MemberInfo_Click" CommandArgument='<%# Container.ItemIndex %>'>
<div class="col-lg-12 mb-2">
<h5 class="mb-1">
<asp:Label ID="PrimaryOrganization" runat="server" Text='<%# Eval("PrimaryOrganization") %>'></asp:Label>
</h5>
</div>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
CS
protected void MemberInfo_Click(object sender, EventArgs e)
{
LinkButton MemberInfoBNT = (LinkButton)sender;
bool bIsConverted = int.TryParse(MemberInfoBNT.CommandArgument.ToString(), out int index);
if (bIsConverted)
{
Repeater SearchResultsObject = MemberInfoBNT.Parent.Parent as Repeater;
ListViewDataItem listViewData = MemberInfoBNT.Parent.Parent.Parent as ListViewDataItem;
int Listviewindex = listViewData.DataItemIndex;
Label PrimaryOrganization = (Label)SearchResultsObject.Items[index].FindControl("PrimaryOrganization");
TextBox registrantEmailValue = (TextBox)listView.Items[Listviewindex].FindControl("registrantEmail");
registrantEmailValue.Text = PrimaryOrganization.Text;
}
}
Image of results
Select2 provides you a customizable dropdownlist with support for searching
I have a dropdown list inside a repeater which has data in its list item I need to access the selected data when the button inside the repeater is clicked. my html code is as follows
<asp:Repeater runat="server" ID="rptrData" OnItemCommand="rptrData_ItemCommand">
<ItemTemplate>
<tr role="row" class="odd">
<td>
<asp:DropDownList ID="ddlProgress" runat="server">
<asp:ListItem Value="0">No Basement</asp:ListItem>
<asp:ListItem Value="1">Basement</asp:ListItem>
<asp:ListItem Value="2">Lintel</asp:ListItem>
<asp:ListItem Value="3">Roof</asp:ListItem>
</asp:DropDownList></td>
<td>
<div class="btn-group btn-group-xs">
<asp:Button ID="Update" runat="server" Text="Update" UseSubmitBehavior="False" CommandName="Update" />
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
You can use FindControl on the RepeaterCommandEventArgs Item since the Repeater is the sender.
protected void rptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//use findcontrol to locate the DDL and cast it
DropDownList drp = e.Item.FindControl("ddlProgress") as DropDownList;
//show result
Label1.Text = drp.SelectedValue;
}
Note that all your values in ddlProgress are 0, that can cause problems. Make those unique.
I have 2 textboxes and a button in a row, and a Repeater dynamically generates some rows at Page_Load function.
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><asp:Label CssClass="form-control" disabled="true" runat="server"><%# DataBinder.Eval(Container.DataItem, "sid") %></asp:Label></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz1"></asp:TextBox></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz2"></asp:TextBox></td>
<td><asp:Button ID="add" CommandName="add" runat="server" OnClick="addQuiz" Text="Add" CssClass="btn btn-success btn-sm form-control"/></td>
</tr>
</ItemTemplate>
</asp:Repeater>
How do I access the textboxes in the Repeater upon clicking the button in the row?
You use FindControl on the RepeaterItem which is the NamingContainer of the TextBoxes. You get it by casting the sender to Button (or Control) and cast it's NamingContainer property accordingly:
protected void addQuiz(Object sender, EventArgs e)
{
var btn = (Button) sender;
var item = (RepeaterItem) btn.NamingContainer;
var quiz1 = (TextBox) item.FindControl("quiz1");
var quiz2 = (TextBox) item.FindControl("quiz2");
}
I have an ASP.NET DataList, with a footer defined thus:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" type="text" placeholder="Notes" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button>
</FooterTemplate>
What I'm looking to do, is, on click of btnAdd, get the values from txtNotes and ddlStatusList, but I can't work out how to access the controls, let alone the values.
I can't follow something like this because I won't be able to check if my button has been clicked (as is possible with a checkbox), and even then, I'm not sure if I'll be able to use findControl as demonstrated. (Does a DataList's footer behave differently to an item?)
I can't use the Button's commandName & commandValue attributes because, when databound, the text inputted won't exist and therefore I can't set the CommandValue.
I have tried using a LinkButton rather than a normal .NET Button, but come accross the same issue, whereby I cannot work out how to get the values from the TextBox/DropDownList
Following should work. See I added runat="Server" for txtNotes:
aspx:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" runat="server" type="text" placeholder="Notes" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button>
</FooterTemplate>
C#:
protected void btnAdd_Click(object sender, EventArgs e)
{
var txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)(((Button)sender).Parent).FindControl("txtNotes");
var ddlStatusList = (DropDownList)(((Button)sender).Parent).FindControl("ddlStatusList");
}
You can use Control.NamingContainer to access other controls in a row:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" type="text" placeholder="Notes" runat="server" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd" OnClick="btnAdd_Click"></asp:Button>
</FooterTemplate>
protected void btnAdd_Click(object sender, EventArgs e)
{
Button btnAdd = (Button)sender;
DropDownList ddlStatusList = (DropDownList)btnAdd.NamingContainer.FindControl("ddlStatusList");
System.Web.UI.HtmlControls.HtmlInputText txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)btnAdd.NamingContainer.FindControl("txtNotes");
int index = ddlStatusList.SelectedIndex;
string text = txtNotes.Value;
}
this is what i have done:
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1">
<HeaderTemplate>
Pro
</HeaderTemplate>
<ContentTemplate>
<div style="height: 100px; width: 200px; overflow: Auto">
<asp:DataList ID="DataList1" runat="server" CellPadding="2" CellSpacing="2" ForeColor="Red">
<ItemTemplate>
<asp:CheckBox ID="checkbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkLike_CheckedChanged" />
<asp:LinkButton Text='<%#Eval("UPP")%>' ID="lnkpro" runat="server" CssClass="linkbutton"
OnClick="btn_Click" CommandArgument='<%# Eval("UCode") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
<ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="services">
<HeaderTemplate>
Sets
</HeaderTemplate>
<ContentTemplate>
<div style="height: 100px; width: 200px; overflow: Auto">
<asp:DataList ID="DataList2" runat="server" CellPadding="2" CellSpacing="2">
<ItemTemplate>
<asp:CheckBox ID="checkbox" runat="server" />
<asp:LinkButton Text='<%#Eval("SNA")%>' ID="lnkpro1" runat="server" CssClass="linkbutton"
OnClick="btn_Click1" CommandArgument='<%# Eval("Code") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
Now my question :
This code block results in a list of link buttons in front of them all checkboxes. So what i want is to get the respective linkbutton value on checkbox checkchanged.
Is it possible?
for example
[] linkbtn1
[] linkbtn2
when i check on first checkbox the id not text of linkbutton1 should be fetched.([] is checkbox )
The ID of each link button is going to be the same on each row of the data list, the text value of each link button will be the SNA value bound to that link button. You can retrieve that SNA value by using the check box's CheckChanged event, like this:
In the DataList's ItemTemplate markup:
<asp:CheckBox ID="checkbox" runat="server"
OnCheckedChanged="checkbox_CheckChanged" />
Now that we have wired up the check changed event, it is time to implement the handler, like this:
protected void checkbox_CheckChanged(object sender, EventArgs e)
{
CheckBox theCheckBox = sender as CheckBox;
// Make sure the cast to check box worked before we try to use it
if(theCheckBox != null)
{
LinkButton theLinkButton = theCheckBox.Parent.FindControl("lnkpro1") as LinkButton;
// Verify that the link button was found before we try to use it
if(theLinkButton != null)
{
// Get the text value from the link button in the same row
// as the check box checked changed
string theLinkButtonText = theLinkButton.Text;
// Do something with text value here
}
}
}
Try this
protected void checkbox_CheckedChanged(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("checkbox");
LinkButton lnkbtnActivate = (LinkButton)item.FindControl("lnkpro");
if (chk.Checked == true)
{
string Result = lnkbtnActivate.Text;
}
}
}