Get fields within a panel within a repeater on button click - asp.net

I have an item repeater that displays items that have been bid on by a user. The items are contained within a panel as they contain form elements to update the bid for each item individually. What I would like is for the user to be able to submit the individual item to be updated and for me to know which item they are trying to update so I can ignore all other fields when processing the update.
Right now, my repeater looks like:
<asp:Repeater ID="itemRepeater" runat="server" onitemdatabound="itemRepeater_DataBound">
<ItemTemplate>
<!-- Auction Item MASTER-->
<asp:Panel id="pnlDefaultButton" runat="server" DefaultButton="absenteeBtnBid">
<div style="position: absolute; left: 0px; top: 0px; transform: translate(0px, 236px);" class="item auction_item firearm user_item isotope-item">
<div class="item_image">
<asp:HyperLink ID="item_img_link" runat="server" Visible="False" EnableViewState="False">
<asp:Image ID="item_img" runat="server" Visible="False" EnableViewState="False" Width="224" />
</asp:HyperLink>
</div>
<div class="item_overlay">
<div class="item_buttons">
Following
<asp:Label ID="absenteeBidLabel" runat="server" Text="" CssClass="absenteePlaceBidLabel" AssociatedControlID="absenteeBid" style="font-size:11px;" Visible="false">
<asp:TextBox ID="absenteeBid" runat="server" Wrap="False" CssClass="absenteePlaceBid" placeholder="Enter Bid" />
</asp:Label>
<asp:TextBox ID="absenteeBidId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:TextBox ID="absenteeBidClose" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:TextBox ID="absenteeBidSaleId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:Button runat="server" ID="absenteeBtnBid" cssClass="startbidding_button edit_button" OnClick="onclick_absenteeBid" Text="Edit Bid" />
<div class="bid_options">
Live Bid
Bid by Phone
<asp:HyperLink ID="bid_withdraw" runat="server" CssClass="withdrawbid"></asp:HyperLink>
</div>
</div>
<table class="item_bidstatus" border="0" cellpadding="0" cellspacing="0" width="190">
<tbody>
<tr>
<td class="status_label" width="50%">Bids:</td>
<td class="status_value" width="50%"><asp:Label ID="bid_count" runat="server" Text="0" /></td>
</tr>
<tr>
<td class="status_label">My Top Bid:</td>
<td class="status_value"><asp:Literal ID="bid_amount" runat="server"></asp:Literal></td>
</tr>
<tr>
<td class="status_label">Your Status:</td>
<td class="status_value status_low">LOW BID</td>
</tr>
</tbody>
</table>
<div class="item_description">
<h5>
<asp:HyperLink ID="labelLot" runat="server">Lot #<%# Eval("item_lot")%></asp:HyperLink> - <asp:HyperLink ID="item_title" runat="server"><%# Eval("item_title")%></asp:HyperLink>
</h5>
<asp:Label ID="labelEst" runat="server" Visible="false"></asp:Label>
<p class="item_details"><asp:Label ID="labelDesc" runat="server"><%# Eval("item_desc")%></asp:Label></p>
> Item Details
</div>
</div>
<table class="item_footer" width="100%">
<tbody>
<tr>
<td><div class="item_category"><asp:HyperLink ID="item_sale" runat="server"></asp:HyperLink></div></td>
<td><div class="item_daysleft">Bid left: <asp:Literal ID="bid_time" runat="server"></asp:Literal></div></td>
</tr>
</tbody>
</table>
</div>
</asp:Panel>
<!-- /Auction Item MASTER-->
</ItemTemplate>
</asp:Repeater>
So my question would be how do I make the method onclick_absenteeBid only look at the form fields within the panel where the submission was made? Or am I even going about this the right way at all using a panel within a repeater?

Thre's nothing wrong with this approach. You have to find the container panel in the button's click event and find controls inside it. Here's how you can do it:
protected void onclick_absenteeBid(object sender, EventArgs e)
{
Panel pnl = ((Button) sender).Parent as Panel;
if (pnl != null)
{
//Access controls inside panel here like this:
TextBox absenteeBidId = pnl.FindControl("absenteeBidId") as TextBox;
if(absenteeBidId != null)
{
string myAbsenteeBidId = absenteeBidId.Text;
}
//Access Repeater Item
RepeaterItem itm = pnl.NamingContainer as RepeaterItem;
if (itm != null)
{
// Do stuff
}
}
}

Related

get the value of all the checked checkboxes on button click

I have created a table in my .aspx file that looks like this:
Here is the code that does this:
<!-- code for generating the "add selected sessions" button -->
<table>
<tr>
<td><strong>Individual Sessions</strong></td>
<td >
<div class="addButton" style="text-align: center;">
<asp:LinkButton ID="LinkButton2" runat="server" Text="Add Selected Sessions" OnClick="btnAddToCart_Click" />
</div>
</td>
</tr>
</table>
<!-- add all the sessions for the user to select -->
<asp:Repeater ID="rptFeesSession" runat="server">
<HeaderTemplate>
<table >
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
<tr runat="server" visible='<%# Eval("isSession")%>'>
<td valign="top" colspan="2" style="position: relative;">
<asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;"/>
</td>
<td valign="top">
<div class="">
<asp:CheckBox ID="LinkButton3" CommandArgument='<%#Eval("id")%>'CssClass="checkB" OnClick="btnAddToCart_Click" runat="server" Text='<%#Eval("amount", "{0:C}")%>' />
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In my code behind file i want to capture all the checkboxes that have been checked and their respective CommandArgument values.
protected void btnAddToCart_Click(object sender, EventArgs e)
{
using (MyEntities db = new MyEntities())
{
//button was clicked. fetch all the check boxes from the rptFeesSession repeater into an int[]
}
}
There are several issues in your code (including conceptual / logic)
Item events in a Repeater should address item related things.
Click event handler has no access to CommandArgument attribute. Use Command instead.
Checkbox control doesn't support onclick event.
Checkbox events can run immediately only when there is AutoPostback="true".
If you want to refresh all repeater data on change of any checkbox then you can do something like this.
<asp:ScriptManager runat="server" ID="scriptMgr" /><%-- Strongly recommended --%>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater ID="rptFeesSession" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
<tr runat="server" visible='<%# Eval("isSession")%>'>
<td colspan="2" style="position: relative;">
<asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;" />
</td>
<td>
<div class="">
<asp:HiddenField runat="server" ID="hidID" Value='<%#Eval("id") %>' />
<asp:CheckBox ID="LinkButton3"
AutoPostBack="true" CssClass="checkB"
OnCheckedChanged="LinkButton3_CheckedChanged" runat="server"
Text='<%#Eval("amount", "{0:C}")%>' />
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
//.cs
protected void LinkButton3_CheckedChanged(object sender, EventArgs e)
{
decimal total = 0;
using (MyEntities db = new MyEntities())
{
foreach (RepeaterItem item in rptFeesSession.Items)
{
var chk = item.FindControl("LinkButton3") as CheckBox;
if(chk!=null && chk.Checked){
string id = (item.FindControl("hidID") as HiddenField).Value;
total += decimal.Parse(chk.Text);
//do stuff
}
}
}
}

Prevent Bootstrap Collapse toggle after postback

I am using bootstrap collapse in my project,in that collapse I have some buttons and dropdowns but when I click on any button or change dropdown index, postback occurs and collapse got uncollapsed,How can I stop this ?
Here is my Code
<h4>Search Training Profile<br />
</h4>
<div id="div_search" class="collapse" style="overflow-x: auto;">
<table class="table table-bordered table-striped">
<tr>
<td>Employee Name</td>
<td>
<asp:TextBox runat="server" ID="txt_name"></asp:TextBox>
<span class="err">optional</span>
</td>
<td>e.g First Name, Middle Name, Last Name</td>
</tr>
<tr>
<td>Designation</td>
<td><span class="err"></span>
<asp:DropDownList runat="server" ID="DDL_Desig" OnSelectedIndexChanged="DDL_Desig_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<span class="err">optional</span>
</td>
<td>
<asp:TextBox runat="server" ID="txt_desig" ReadOnly="true"></asp:TextBox>
<asp:Button Text="Clear" runat="server" />
</td>
</tr>
<tr>
<td>Location</td>
<td>
<asp:DropDownList runat="server" ID="DDL_Loc" OnSelectedIndexChanged="DDL_Loc_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>Select</asp:ListItem>
</asp:DropDownList>
<span class="err">optional</span>
</td>
<td>
<asp:TextBox runat="server" ID="txt_loc" ReadOnly="true"></asp:TextBox>
<asp:Button Text="Clear" runat="server" />
</td>
</tr>
<tr>
<td>Division</td>
<td>
<asp:DropDownList runat="server" ID="DDL_Divis" OnSelectedIndexChanged="DDL_Divis_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<span class="err"> optional </span>
</td>
<td>
<asp:TextBox runat="server" ID="txt_divis" ReadOnly="true"></asp:TextBox>
<asp:Button Text="Clear" runat="server" />
</td>
</tr>
<tr>
<td>Department</td>
<td>
<asp:TextBox runat="server" ID="tx"></asp:TextBox>
<span class="err">optional</span></td>
<td>e.g ISD, MKT, HR etc</td>
</tr>
<tr>
<td>Filter By</td>
<td>
<asp:DropDownList runat="server" ID="DDL_Assc">
</asp:DropDownList>
</td>
<td>e.g OLP, SOLC, MAF, etc</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3" style="text-align:center;">
<asp:Button Text="Search" runat="server" CssClass="btn" />
<asp:Button Text="Reset" runat="server" CssClass="btn" />
</td>
</tr>
</table>
</div>
Create public variable
string state = "collapse";
during the postback or dropdown changed set the value as
state = "expand";
and aspx page use this as below:-
<div id="div_search" class='<%= state %>' style="overflow-x: auto;">
Aditya's answer helped me initially and is a nice solution. As my code evolved wrapping the div inside an update panel and having the collapse div contain a couple calendar controls that cause postbacks to refresh data in a grid, I came up with another solution I thought worth sharing as it is handled server side only.
<asp:Panel ID="pnlDateRange" ClientIDMode="Static" runat="server" CssClass="collapse col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div id="divFrom" class="col-xs-12 col-sm-12 col-md-2 col-md-offset-4 col-lg-2 col-lg-offset-4">
<asp:Calendar ID="cStartDate" runat="server" CssClass="calendar" Font-Size="X-Small"
DayNameFormat="FirstTwoLetters" DayHeaderStyle-Font-Bold="false"
TitleStyle-Font-Bold="true" TitleStyle-Font-Size="Small" Caption="From"
OnSelectionChanged="cStartDate_SelectionChanged">
<SelectedDayStyle Font-Bold="true" />
<OtherMonthDayStyle ForeColor="SlateGray" />
</asp:Calendar>
</div>
<div id="divTo" class="col-xs-12 col-sm-12 col-md-2 col-lg-2">
<asp:Calendar ID="cEndDate" runat="server" CssClass="calendar" Font-Size="X-Small"
DayNameFormat="FirstTwoLetters" DayHeaderStyle-Font-Bold="false"
TitleStyle-Font-Bold="true" TitleStyle-Font-Size="Small" Caption="To"
OnSelectionChanged="cEndDate_SelectionChanged">
<SelectedDayStyle Font-Bold="true" />
<OtherMonthDayStyle ForeColor="SlateGray" />
</asp:Calendar>
</div>
</asp:Panel>
The control was originally an ordinary with the same classes applied, it is the collapse target. In the c# on server side I have defined a private boolean variable set to true in all cases when the code loads to run. In the OnPreRender event I do this:
if (_collapseRange)
{
pnlDateRange.CssClass = pnlDateRange.CssClass.Replace("collapse in", "collapse");
}
else
{
if (!pnlDateRange.CssClass.Contains("collapse in"))
{
pnlDateRange.CssClass = pnlDateRange.CssClass.Replace("collapse", "collapse in");
}
}
I know it's an old question, but i did it this way.
Maybe not the best way, but it works for me.
Just call the method when ever you need it.
It gets a bit messy when you got a lot to collapse.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetTextForInfoboxFrontPage();
multiCollapseExample1.Attributes["class"] = "collapse";
annualCollapse.Attributes["class"] = "collapse";
infoBox.Attributes["class"] = "collapse";
}
}
private void CollapseAllButAnnual()
{
annualCollapse.Attributes["class"] = "collapse in";
infoBox.Attributes["class"] = "collapse";
logoUpload.Attributes["class"] = "collapse";
}

Focus on TextBox inside TabContainer

I have a custom Login screen in a Tab Container
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" AutoPostBack="true" OnActiveTabChanged="TabContainer1_ActiveTabChanged" CssClass="MyTabStyle">
<asp:TabPanel ID="uxStandardTab" runat="server" HeaderText="Login">
<ContentTemplate>
<table width="320px" border="0" style="border-width: 0px;">
<tr>
<td>
<cc1:LabeledTextBox ID="Username" runat="server" Required="true" LabelText="Email/User Name" LabelWidth="120" ControlWidth="150" LabelCss="FormLabelText BoldText" ValidationGroup="Standard"></cc1:LabeledTextBox>
<cc1:LabeledTextBox ID="Password" runat="server" TextMode="Password" Required="true" LabelText="Password" LabelWidth="120" ControlWidth="150" LabelCss="FormLabelText BoldText" ValidationGroup="Standard"></cc1:LabeledTextBox>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 5px; margin-right: 20px; text-align: center;">
<asp:Button ID="btnLogin" runat="server" Text="Log In" CssClass="grnbutton" OnClick="btnLogin_Click" ValidationGroup="Standard" />
</div>
</td>
</tr>
</table>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
I'm trying to set focus on User Name field in the page load.
I have tried
var txtUserName = uxStandardTab.FindControl("UserName");
if (txtUserName != null)
{
Page.SetFocus(txtUserName);
}
and
Username.Focus();
But, none of them puts cursor in User Name Text box during page load. Please advise, if Tab Control behaves differently.
With Ajax don't use Page.SetFocus but Scriptmanager.SetFocus:
ScriptManager.GetCurrent(Page).SetFocus(txtUserName);

ASP:ListBox - no selected items on postback?

I have the following markup:
<tr>
<td valign="top" align="left">
<asp:Label ID="Label1" runat="server" Text="Available Roles" />
<br />
<asp:ListBox ID="availableRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
</td>
<td valign="top" align="center">
<br />
<asp:Button ID="addToRole" runat="server" Text="--->" OnClick="addToRole_Click" />
<br />
<asp:Button ID="removeFromRole" runat="server" Text="<---" OnClick="removeFromRole_Click" />
</td>
<td valign="top" align="left">
<asp:Label ID="Label2" runat="server" Text="User In Roles" />
<br />
<asp:ListBox ID="userInRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
</td>
</tr>
And the following in code-behind:
protected void addToRole_Click(object sender, EventArgs e)
{
// Add user to the selected role...
foreach (ListItem myItem in availableRolesListBox.Items)
{
if (myItem.Selected)
{
Roles.AddUserToRole(userListBox.SelectedItem.Value, myItem.Text);
}
}
Refresh();
}
When I step into the code-behind absolutely no items are selected! What am I forgetting?
Are you perhaps rebinding the availableRolesListBox each time, instead of if(!IsPostback)?
You could check a few things.
CHeck that you are NOT reloading the listbox after each postback. Also, you might want to make sure you do not have ViewStateEnabled="false" for a parent container.
Other than that your code looks like it should be ok, debugging any further would require more code or information.

Refresh gridview inside update panel after modal popup closes

I'm having a heck of a time trying to get a gridview to refresh its data after a modalpopup adds a new record to the database. Ive tried the following with no luck.
<cc2:ModalPopupExtender ID="mdlPopup" runat="server" OnOkScript="__doPostBack('<%= gvRecommendations.ClientID %>', '');" BackgroundCssClass="modalBackground"
TargetControlID="lbtnRecommendationsAddNew" PopupControlID="pnlAddNewRecommendation">
</cc2:ModalPopupExtender>
<asp:Panel ID="pnlAddNewRecommendation" runat="server" CssClass="confirm-dialog" style="display:none;" Width="500px">
<div class="inner">
<h2>New Suppressed Recomendation</h2>
<div class="base">
<table width="100%" cellpadding="5" cellspacing="0">
<tr>
<td align=left>
<asp:DropDownList ID="ddlRecomendations" runat="server" />
</td>
</tr>
<tr>
<td align="left">
<asp:Button ID="btnAddRecommendation" OnClick="btnAddRecommendation_Click" runat="server" Text="Submit" />
|
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" ForeColor="Blue" />
<asp:LinkButton id="lbtnTopLeft" runat="server" CssClass="close" />
</div>
</td>
</tr>
</table>
</div>
</div>
</asp:Panel>
Ive also tried adding this with no luck after adding the record to the DB:
this.gvSupressedRecommendations.DataBind();
this.UpdatePanel1.Update();
I know im close but can't seem to get this to refresh.
Try reassigning your datasource before you rebind. This should work. I.e.
gvSupressedRecommendations.DataSource = <...>;
gvSupressedRecommendations.DataBind();

Resources