<script>
function ShowCommentBox() {
$("#dialog").dialog({ modal: true });
}
function GrabDetails() {
var obj = jQuery.parseJSON('{"name":"John"}');
$("#item").val(obj.name);
}
</script>
<div id="dialog" title="Comments" style="display:none;">
<table class="detailstable FadeOutOnEdit">
<tr>
<th>Item</th>
</tr>
<tr>
<td><asp:Label ID="ItemIdLabel" Text="item" runat="server"/></td>
</tr>
</table>
</div>
<input id="SubmitCommentsToInvoice" type="button" value="Comments" onclick="ShowCommentBox()" />
In my asp.net project when the user clicks the 'Comments' button the div appears containing the label.
I'm trying to use JSON to display the string "John" - stored in them #item object in the 'GrabDetails()'
Then in the label text="" How do I pull across the value stored in the #item object.
Thanks
#item is an ID selector in jQuery, which there is no element here with ID "item". Also, <asp:Label /> renders from the server as html in a different way. However, it appears you are not using this label on the server side at all? if this is the case i would just make an html element like
<td id="WhereNameGoes"></td>
then
function GrabDetails() {
var obj = jQuery.parseJSON('{"name":"John"}');
$("#WhereNameGoes").text(obj.name);
// this still needs to be called somewhere, perhaps in ShowCommentBox()?
}
jQuery $.val() is more for <input /> elements
Related
I am not getting label id in jquery code , i use anchor tag , it is working , the text changes to today's date on page load , but when i apply it to label control of asp.net , it doesn't work , search may pages on web , but doesn't get proper solution how to change text of label using java script or jquery , is here anybody can solve it.
here is the code to look out .
<div>
<div>
<table class="foruploadtable">
<tr>
<td>
<span class="foruploadtabletitle" >Share From Here .</span>
<hr />
</td>
</tr>
<tr>
<td>Max Size 1MB :</td><td>
<asp:FileUpload ID="FileUpload1" ToolTip="Select File By Clicking Browse ." CssClass="foruploadtableupload" runat="server" /></td>
</tr>
<tr>
<td>
<asp:Button ID="b1" runat="server" CssClass="foruploadtablebutton" ToolTip="Click Here To Upload." Text="Upload." OnClick="b1_Click" />
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<a id="me">hello</a>
</td>
</tr>
</table>
</div>
</div>
<script>
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#Label1').text(date.getDate());
// alert(date.getDate());
});
</script>
Try this:
change label add attribute ClientIDMode="Static"
<asp:Label ClientIDMode="Static" ID="Label1" runat="server" Text="Label"></asp:Label>
$('#Label1').text(date.getDate());
or
$('#<%= Label1.ClientID %>').text(date.getDate());
You can try this.
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#<%= Label1.ClientID %>').text(date.getDate());
// alert(date.getDate());
});
Make sure that your page does not have master page. If it is so then you can use Client ID as below
<script>
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#<%=Label1.ClientID%>').text(date.getDate());
// alert(date.getDate());
});
You can try this,
$('#<%=Label1.ClientID%>').text(date.getDate());
This helps to grab the id generated by ASP.Net for the id parameter..
I have a HomePageViewModel that takes various models and shows a summary view on the home page.
I'd like to filter the Announcements model based on the result of a drop down list, which I'm rendering as an unordered list. This is my first time using partial views or anything like that, and after some reading decided to do it a HTML.Action call which calls DisplayAnnouncements from the Announcement Controller.
However, I'm unsure how to pass the parameter of what is clicked in the list (the "#" in the example below), to my HTML.Action method.
This is HomePage view:
<ul class="dropdown-menu">
<li>All</li>
<li>Legal</li>
<li>Accounting</li>
</ul>
#Html.Action("DisplayAnnouncments", "Announcement", new { filter = "All" })
And this is my announcement controller:
public ActionResult DisplayAnnouncments(string filter)
{
return PartialView("_Announcements",
(from a in db.Announcements
where a.Type == filter
select a).Take(4));
}
My _Annoucments partial view:
<table class="table" style="table-layout: fixed">
#foreach (var item in Model)
{
<tr>
<td style="vertical-align: top; width: 70px">
#item.AnnouncmentDate.ToShortDateString()
</td>
<td>
<strong>#item.Title</strong><br />
#item.Announcment
</td>
</tr>
}
So to reiterate, I have a few questions:
How to I get the list item that the user clicked to populate the appropriate filter in my HTML.Action?
Is HTML.Action the best thing here? Should I be using something else?
How do I only refresh the partial view so the whole page doesn't refresh?
Thanks for your help, I'm quite new to this!
Robbie
The simplest way could be to use some javascript to copy the drop down selected value inside a hidden field of your partial view then simulate a click of one of your partial view (hidden) submit button.
partial view:
<table class="table" style="table-layout: fixed">
<input type="hidden" id="MyHiddenInput" name="selectedValue" />
#foreach (var item in Model)
{
<tr>
<td style="vertical-align: top; width: 70px">
#item.AnnouncmentDate.ToShortDateString()
</td>
<td>
<strong>#item.Title</strong><br />
#item.Announcment
</td>
</tr>
}
<div style="display: none;"><input type="submit" id="myHiddenButton" /></div>
Parent view:
<ul class="dropdown-menu">
<li>All</li>
<li>Legal</li>
<li>Accounting</li>
</ul>
and some jQuery:
$("dropdown-menu > LI > A").live("click", function() {
$("#MyHiddenInput").val($(this).data("hval"));
$("#myHiddenButton").click();
});
On button click I want to get the value of a dropdown but for somereason when I go into the code it fails with error 'Exdd' does not exist in the current context. The ExTypeDD is in my listview as a dropdown and I'm just trying to get the value from it. The server side is generating this code and not my browser.
$('.Updt')
.click(function() {
var parent = $(this).parent().prev();
var TypeNode = parent.children("#<%=Exdd.ClientID %>").first();
<asp:ListView runat="server" id="ListView1" >
<LayoutTemplate>
<table id="tablesorter" style="border:solid 1px black;width:55%;">
<thead>
<tr>
<th>
Address
</th>
</tr>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server" />
</tbody>
<tfoot>
</tfoot>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<select id="Exdd"
class="Nbr1" style="width: 90px" >
<option value=""><%# Eval("Type")%></option>
<option value="0">Home</option></select>
</td>
<td>
<input type="button" id="btn_update" class="Updt" value="Update" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
You are in a ListView. You have as ExTypeDD as you have rows.
So you can not reference ExTypeDD successfully outside of the item template (there are more than one)
You may try ( after wrapping that up in a $(document).ready )
$('.Updt')
.click(function() {
var tr = $(this).closest('tr');
var TypeNode = tr.find("select.Nbr1").first();
//...
});
This is because you are trying to access an HTML element on the server side as a server side control. You could make your select element into a server side control by simply adding runat="server" to it like such:
<select id="ExTypeDD" runat="server" class="Nbr1" style="width:90px">
But after that you would still have the issue that this control, although accessible on the server side now, it does not have the id of ExTypeDD because it is dynamically created in the ItemTemplate of your ListView which generates a unique ID value for each newly created control . You can verify this by viewing source on the page rendered in the browser and checking the ID value on the select element.
To do what you're trying to do on the server side you would need to do something along the lines of this:
Add an onItemCreated event handler to your ListView:
<asp:ListView runat="server" id="ListView1" onItemCreated="ListView1ItemCreated">
Replace this:
<input type="button" id="btn_update" class="Updt" value="Update" />
With this:
<asp:button id="btn_update" text="Update" runat="server" />
Add the event handler to the code behind:
protected void ListView1ItemCreated(object sender, ListViewItemEventArgs e){
var myselect = (HtmlSelect)e.Item.FindControl("ExTypeDD");
var mybutton = (Button)e.Item.FindControl("btn_update");
mybutton.OnSubmit += (o,e) => {
// do something with myselect selected value
var selectedvalue = myselect.Value;
};
}
NOTE: the above code is not tested and probably won't compile, you will need to make necessary corrections, but it should give you the idea for a way to solve your problem. Hope it helps.
I am currently having an issue with radio buttons and grouping. I have an asp radio button within a repeater control. I have the group name attribute set to "Customer". When the page loads, the radio buttons are not grouped. Instead of the id fields being set to the group name, it is setting the value fields of the radio buttons. I know that I have tried setting radio buttons up outside of a repeater control and have had the same issue. What is going on here?
aspx
<asp:Repeater ID="repCustomers" runat="server">
<HeaderTemplate>
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="radCustomer" GroupName="Customer" runat="server" ValidationGroup="Customer" ToolTip='<%#Eval("CustomerNumber") %>' />
</td>
<td><%#Eval("CustomerNumber")%></td>
<td><%#Eval("Name") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
output html
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
<tr>
<td>
<span title="111111"><input id="ctl00_PrimaryContent_repCustomers_ctl01_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl01$Customer" value="radCustomer" /></span>
</td>
<td>111111</td>
<td>Jeremy's Test</td>
</tr>
<tr>
<td>
<span title="222222"><input id="ctl00_PrimaryContent_repCustomers_ctl02_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl02$Customer" value="radCustomer" /></span>
</td>
<td>222222</td>
<td>My Test</td>
</tr>
<tr>
<td>
<span title="333333"><input id="ctl00_PrimaryContent_repCustomers_ctl03_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl03$Customer" value="radCustomer" /></span>
</td>
<td>333333</td>
<td>Jim Bob's BBQ</td>
</tr>
<tr>
<td>
<span title="444444"><input id="ctl00_PrimaryContent_repCustomers_ctl04_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl04$Customer" value="radCustomer" /></span>
</td>
<td>444444</td>
<td>New Hope Hamburgers</td>
</tr>
<tr>
<td>
<span title="555555"><input id="ctl00_PrimaryContent_repCustomers_ctl05_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl05$Customer" value="radCustomer" /></span>
</td>
<td>555555</td>
<td>Pied Piper Pizza</td>
</tr>
<tr>
<td>
<span title="666666"><input id="ctl00_PrimaryContent_repCustomers_ctl06_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl06$Customer" value="radCustomer" /></span>
</td>
<td>666666</td>
<td>Sandy's Subs</td>
</tr>
<tr>
<td>
<span title="777777"><input id="ctl00_PrimaryContent_repCustomers_ctl07_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl07$Customer" value="radCustomer" /></span>
</td>
<td>777777</td>
<td>Leonard's Lambchops</td>
</tr>
<tr>
<td>
<span title="888888"><input id="ctl00_PrimaryContent_repCustomers_ctl08_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl08$Customer" value="radCustomer" /></span>
</td>
<td>888888</td>
<td>Dave's Diamond Deli</td>
</tr>
<tr>
<td>
<span title="999999"><input id="ctl00_PrimaryContent_repCustomers_ctl09_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl09$Customer" value="radCustomer" /></span>
</td>
<td>999999</td>
<td>Ernie's Eatery</td>
</tr>
</table>
I finally got around this by creating a plain radio button and setting the value using an server-side eval.
<input type="radio" name="radCustomer" value='<%#Eval("CustomerNumber") %>' />
Now when the application performs a postback, I check for the value of Request.Form["radCustomer"]. This works flawlessly.
Unfortunately, this is a well known issue with radio buttons within a repeater. One of your only options would be to create a custom server control derived from the RadioButton class and override how it renders.
EDIT: Here's a sample of what the derived class may look like:
public class MyRadioButton : RadioButton
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input id=\"" + base.ClientID + "\" ");
writer.Write("type=\"radio\" ");
writer.Write("name=\"" + base.ID + "\" ");
writer.Write("value=\"" + base.ID + "\" />");
writer.Write("<label for=\"" + base.ClientID + "\">");
writer.Write(base.Text);
writer.Write("</label>");
}
}
I fixed it in javascript:
$(function () {
$("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I had the same issues. I am using Literal as placeholder to render radio button onItemCreated event.
ASP.Net
<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated">
<ItemTemplate>
<asp:Literal ID="lit" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
C#
protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) {
Literal lit = (Literal)e.Item.FindControl("lit");
lit.Text = "<input type=\"radio\" name=\"myGroup\">";
}
I had to modify slightly the answer posted above by r3dsky.
Here's what worked for me:
$(document).ready(function () {
$(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I would start by adding a value on my radiobutton Value='<%#Eval("CustomerNumber") %>'.
I made my radiobutton have autopostback set to true, and then in the event handler set all the other radio buttons to BE unselected.
Not ideal, but I need lots control over the visibility and enabled attributes of the radiobutton, and it seemed easier to let ASP.NET control that rather than resorting to client side script.
This is a well known bug with the ASP.NET Repeater using RadioButtons:
here best solution in my opinion
I did this:
$("input:radio").attr("name", $("input:radio").first().attr("name"));
Why? because if you replace the name property for any string you want, you will get an 'not found error'. So, you need to get the name of the first radiobutton, and rename all of them with that name. It works like a sharm ;)
My solution, similar to others:
<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" >
// Fixes this ASP.NET bug: if radio input is inside repeater you can't set its name.
// Every input gets set different name by ASP.NET.
// They don't behave as a group. You can select multiple radios.
function fixRadiogroupBug()
{
$('[type="radio"][data-fixgroupbug]').click(function () {
$(this).siblings('[type="radio"]').prop('checked', false);
});
}
$(document).ready(function () {
fixRadiogroupBug();
});
I have an aspx page that uses a master page. On this page I have a group of radio buttons. I need to get at the checked property for each of the elements in the radio button group in javascript. Before the page was converted to use a master page, the following javascript code worked just fine:
if((!f.rblRegUseVehicles[0].checked) && (!f.rblRegUseVehicles[1].checked))
{
isValid = "false";
}
f is the reference to the form. This worked just fine.
When the page was chanded to use a Master Page, I changed the javascript to the following:
if((!f.<%=rblRegUseVehicles.ClientID%>[0].checked) && (!f.<%=rblRegUseVehicles.ClientID%>[1].checked))
{
isValid = "false";
}
Now, the javascript is failing because it can't find the element. In the "View Source" I have elements with the name:
<input id="ctl00_cphContent_rblRegUseVehicles_0" type="radio" name="ctl00$cphContent$rblRegUseVehicles" value="Yes" />
<input id="ctl00_cphContent_rblRegUseVehicles_1" type="radio" name="ctl00$cphContent$rblRegUseVehicles" value="No" />
The only code that works is
document.<%=Form.ClientID%>.<%=rblRegUseVehicles.ClientID%>_0.checked
I want the javascript to reference the array like before the page was converted to use a Master Page. How can I do that?
This is a part of ASP.NET that has always been difficult. You need to have static javascript that references dynamically created element ids!
This is how I have always solved the problem:
Wrap your RadioButtonList in a div or p or whatever you want:
<div id="yourId">
<asp:RadioButtonList id="radiolist1" runat="server">
<!-- ... -->
</asp:RadioButtonList>
</div>
Which renders to something like this:
<div id="yourId">
<table id="radiolist1" border="0">
<tr>
<td>
<input id="radiolist1_0" type="radio" name="radiolist1" value="Item 1" checked="checked" />
<label for="radiolist1_0">Item 1</label>
</td>
</tr>
<tr>
<td>
<input id="radiolist1_1" type="radio" name="radiolist1" value="Item 2" />
<label for="radiolist1_1">Item 2</label>
</td>
</tr>
</table>
</div>
And this would allow you to have a static javascript function that targets yourId which doesn't get runat="server". Then your javascript would look something like this:
var rbl = document.getElementById("yourId");
isValid = (!rbl.getElementsByTagName("input")[0].checked
&& !rbl.getElementsByTagName("input")[1].checked);
If you want to explore the idea of using jQuery rather than straight javascript, this should do the trick for you.
if ( $('input[id*=rblRegUseVehicles]:checked').length < 0 ) isValid = "false";
The selector gets all checked radiobuttons who's id contains 'rblRegUseVehicles'.
Edit
If you want to stick with your original script,using UniqueID rather than ClientID may work
if((!f.<%=rblRegUseVehicles.UniqueID%>[0].checked) && (!f.<%=rblRegUseVehicles.UniqueID%>[1].checked))
{
isValid = "false";
}