showing message box on control's modal popup extender - asp.net

I have a control inside a page that has the following modal popup extender:
<asp:Panel ID="pnl_Completed" runat="server">
<asp:Image ID="exit_Completed" runat="server" ImageUrl="" />
<h3 style="text-align:center;">Completed</h3>
<asp:Panel ID="pnl_inner" runat="server">
<table style="width:100%;height:100%" cellpadding="5px">
<tr style="height:40px;">
<td valign="top">Comment: </td>
<td>
<telerik:RadTextBox ID="txt_CompletedComment" runat="server" TextMode="MultiLine" Rows="6" Width="400" Height="100"></telerik:RadTextBox>
</td>
</tr>
<tr style="height:40px;">
<td colspan="2" align="center"><asp:Button id="btn_SaveCompleted" runat="server" Text="Complete" OnClick="btn_SaveCompleted_Click" /></td>
</tr>
</table>
</asp:Panel>
</asp:Panel>
<cc1:ModalPopupExtender ID="ModalPopupExtender_Completed" runat="server" PopupControlID="pnl_Completed" CancelControlID="exit_Completed" TargetControlID="dummy_btn_Completed" >
</cc1:ModalPopupExtender>
<asp:Button id="dummy_btn_Completed" runat="server" CssClass="display_none" />
I want to show a messagebox on btn_SaveCompleted_Click event when the textbox is empty I have tried this:
If txt_CompletedComment.Text.Trim().Length = 0 Then
ScriptManager.RegisterStartupScript(Me, Me.GetType, "key", "alert('Please enter a comment.');", True)
End If
but this doesnt work it just hides the modal popup extender, with no errors. Am I doing it wrong? Is there any other way to show it?

Just use JavaScript, there a bunch of ways to do it here is one example:
<asp:Button id="btn_SaveCompleted" runat="server" Text="Complete"
OnClientClick="return ValidateTheTextbox();"OnClick="btn_SaveCompleted_Click" />
<script>
function ValidateTheTextbox()
{
var txtbox = document.getElementById('<%= txt_CompletedComment.ClientID %>');
if(txtbox.value=="")
{
alert('Please enter a comment.');
return false; //suppress the submit button
}
return true; //let the form submit
}
</script>

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
}
}
}
}

Change Label text in gridview on checking checkbox

I am using this Gridview which have checkboxes and labels in it.Now I want when i click the checkbox the text of label must change.
<asp:GridView ID="grdData" runat="server" style="Text-align:center;">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="changeTextValue(this)"/>
</ItemTemplate>
<HeaderTemplate>
<%--<asp:CheckBox ID="CheckBox2" runat="server" OnClick="CheckAllEmp(this)"/>--%>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Status_Header" runat="server" Text="Status" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text=0 ClientIDMode="Static"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
First Question
Because the label rendered on html will be:
<span id="grdData_ctl02_Label1" ClientIDMode="Static">1</span>
So,you should use "span[id*='Label1']" to find it out:
function changeTextValue(chk) {
var currentTextID = $(chk).parents("tr").find("span[id*='Label1']");
if (chk.checked == true) {
currentTextID.html(1);
}
else {
currentTextID.html(0);
}
}
Second Question
Gridview
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="changeTextValue(this)" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" OnClick="CheckAllEmp(this)" />
</HeaderTemplate>
</asp:TemplateField>
Javascript
function CheckAllEmp(chk) {
var currentTextID = $(chk).parents("table").find("span[id*='Label1']");
var childCheckBoxID = $(chk).parents("table").find("input[id*='CheckBox1']");
if (chk.checked == true) {
currentTextID.html(1);
childCheckBoxID.prop("checked", true);
}
else {
currentTextID.html(0);
childCheckBoxID.prop("checked", false);
}
}
For what reason do you use ClientIDMode="Static"?
You have to create function CheckAllEmp(this) event in your_program_file.cs.
Code in that function would be smth like that:
CheckAllEmp(...){
Label1.Text="Something new";
}
When you want this kind of operations on GridView, it is always good to check the html source of your page. In this scenario, for example, you will have the following html code for your gridview:
<table cellspacing="0" rules="all" border="1" id="grdData" style="border-collapse:collapse;text-align: center;">
<tr>
<th scope="col"></th>
<th scope="col">
<span id="grdData_Status_Header">Status</span>
</th>
</tr>
<tr>
<td>
<input id="grdData_CheckBox1_0" type="checkbox" name="grdData$ctl02$CheckBox1" onclick="changeTextValue(this);" />
</td>
<td>
<span id="Label1">Unique Perspective</span>
</td>
</tr>
<tr>
<td>
<input id="grdData_CheckBox1_1" type="checkbox" name="grdData$ctl03$CheckBox1" onclick="changeTextValue(this);" />
</td>
<td>
<span id="Label1">Challenging</span>
</td>
</tr>
</table>
As you may see, checkbox and label controls are in adjacent table cells. You can use .parent().next() to access the cell containing the label control. Also, you may see that label was rendered as span. To get the label control, therefore, you can use: .parent().next().find('span'). One final note is that you can use .text property of a span to change its contents. See the whole script below:
function changeTextValue(chk) {
var currentTextID = $(chk).parent().next().find('span');
if (chk.checked == true)
currentTextID.text('1');
else currentTextID.text('0');
}

Strange UpdatePanel and Extender control error

I have the below markup in a master page, and when I access a page using that master, I get the error:
An extender can't be in a different UpdatePanel than the control it
extends.
<asp:UpdatePanel ID="UpdatePanel99" runat="server">
<ContentTemplate>
<script type="text/javascript">
function OnAutoCompleteSelected(source, eventArgs) {
$("#autoCompleteHidden").val(eventArgs._text);
__doPostBack("autoCompleteHidden", "");
}
</script>
<table>
<tr>
<td style="height: 25px">Search By
</td>
<td>
<asp:DropDownList ID="ddlSearchBy" runat="server" OnSelectedIndexChanged="ddlSearchBy_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>Emp No</asp:ListItem>
<asp:ListItem>ID No</asp:ListItem>
<asp:ListItem>Surname</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:ImageButton ID="imgBtnSearch" runat="server" Height="20px" ImageUrl="~/Images/Apps/mySearch2.png"
Width="20px" OnClick="imgBtnSearch_Click" />
</td>
</tr>
<tr>
<td colspan="3" style="height: 25px; margin-left: 80px;">
<div>
<asp:HiddenField runat="server" ID="autoCompleteHidden" OnValueChanged="autoCompleteHidden_ValueChanged" />
<asp:TextBox ID="txtSearchField" runat="server" Width="200px"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txtSearchField"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetEmployees" Enabled="False" CompletionInterval="100"
MinimumPrefixLength="1" CompletionSetCount="10000" OnClientItemSelected="OnAutoCompleteSelected"
CompletionListCssClass="CompletionList" CompletionListItemCssClass="CompletionListItem" CompletionListHighlightedItemCssClass="CompletionListHighlightedItem">
</ajaxToolkit:AutoCompleteExtender>
</div>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
There is only one UpdatePanel on the master page, and the extended control, txtSearchField is contained well within it only. What am I doing wrong?
I found another AutoCompleteExtender for the same textbox outside of the UpdatePanel.

Get fields within a panel within a repeater on button click

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
}
}
}

ASP.NET AJAX ModalPopupExtender - opening with wrong button

I am having trouble with the AJAX ModalPopupExtender control. What I am trying to do is click a button, trigger a postcode lookup using Yahoo API with the button click event, display this in an asp panel in the ShowModalDialog box, and then close the dialog. I also need a separate button on the page to submit information using a form that is completely separate from this.
I have set the target button up as follows:
<asp:Button ID="btnStockist" runat="server" BackColor="#2C3473" OnClick="btnStockist_Click" />
The modalpopup is as follows:
<ajax:ModalPopupExtender ID="mpeStockist" runat="server" okcontrolid="btnOkay" targetcontrolid="btnStockist" popupcontrolid="pnlDisplay"
popupdraghandlecontrolid="PopupHeader" drag="true" backgroundcssclass="ModalPopupBG" ></ajax:ModalPopupExtender>
What is actually happening is that when clicking the target button, the popup opens but the event does not trigger (no postcode lookup is done). However, clicking the separate, unrelated button on the same page DOES trigger the event, and the lookup works perfectly. I am able to close the dialog as normal. I have tried hiding the target button with CSS as a quick workaround, but the problem is that any other buttons on the page seem to trigger the lookup method and modalpopup no matter what their click event is programmed to do, and I need them to work separately.
In case it's of any use, the panel code is here:
<asp:Panel ID="pnlDisplay" style="display:none" runat="server">
<div class="PopupContainer">
<div class="PopupBody">
<div align="center">Local Suppliers</div>
<br /><br />
<asp:label ID="lblError" runat="server"></asp:label>
<asp:UpdatePanel ID="upAlternatives" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DataList ID="dlStockist_Select" runat="server">
<HeaderTemplate>
<table cellpadding="5" width="680">
<tr>
<td>Name</td>
<td>Address</td>
<td>Town/City</td>
<td>Miles (Approx)</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "CNAM") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "CADD1") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "CADD3")%></td>
<td align="center"><%# DataBinder.Eval(Container.DataItem, "Distance")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:LinkButton ID="btnOkay" runat="server" visible="true" Text="Close" CommandName="Update" BorderColor="#FFFFFF" BackColor="#000000"
BorderWidth="3" BorderStyle="Double" ForeColor="White" Font-Size="13pt" style="cursor: pointer; padding: 1px 15px 1px 15px;
margin-top: 10px;" Font-Underline="False"></asp:LinkButton>
</div>
</div>
</asp:Panel>
Thanks in advance for the help
Add one more button as per below
<asp:Button Text="targetbutton" ID="tgtbtn" runat="server" Style="display: none" />
Set id of this button in targetcontrolid of modulpopup (because it can not be null)
and in click event of btnStockist
open your modulapopup by code
protected void btnStockist_Click(object sender, EventArgs e)
{
mpeStockist.Show();
}

Resources