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.
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..
<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
I am using validation summary control to display error messages of asp.net validation controls.
There are some validations in page for which validation controls are not being used. I am using custom javascript and jquery code for these. Kindly guide how I can display messages of these errors in validation summary control along with asp.net validation controls.
The key is to use the Custom Validation control. This control supports client side scripting (so you can still use your javascript code to do it) but still ties into the validation framework asp.net provides.
From the microsoft article, something like this:
<SCRIPT LANGUAGE="JavaScript">
function validateLength(oSrc, args){
args.IsValid = (args.Value.length >= 8);
}
</SCRIPT>
<asp:Textbox id="text1" runat="server" text="">
</asp:Textbox>
<asp:CustomValidator id="CustomValidator1" runat=server
ControlToValidate = "text1"
ErrorMessage = "You must enter at least 8 characters!"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
As Patrick said you can add the custom validator controls in place of your custom javascript/jQuery code. And then accordingly you can add the summary validation control. Also you can change the error message in your javascript code. Please check the below code for your reference.
<script type="text/javascript">
function validatetxtLength(source, args)
{
var txtVal=document.getElementById('<%=txtusername.ClientID %>').value;
if(txtVal=="")
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Please Enter the User Name");
args.IsValid=false;
}
else if(txtVal.length>9)
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Username must have less than 10 characters");
args.IsValid=false;
}
else
{
args.IsValid=true;
}
return;
}
</script>
<div>
<table cellpadding="0" cellspacing="0" border="0" width="712px">
<tr>
<td colspan="3" align="center">
Validator Testing
</td>
</tr>
<tr>
<td>
Please Enter your User Name:
</td>
<td>
<asp:TextBox ID="txtusername" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="custxtValidator" runat="server" ErrorMessage="User Name must have less than 10 characters"
Text="*" ForeColor="Red" ClientValidationFunction="validatetxtLength"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please check below validations:"
runat="server" DisplayMode="BulletList" EnableClientScript="true" />
</div>
This might be useful for you.
I define some controls inside repeater itemtemplate, the problem is with the Id that are generated automatically.
This is my page:
<asp:Repeater ID="rptThreads" runat="server"
onitemcreated="rptThreads_ItemCreated">
<HeaderTemplate>
<table cellpadding="0px" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr style="height:50px">
<td>
<asp:PlaceHolder ID="plcItemTitle" runat="server">
<asp:Panel id="titleContainer" runat="server" style="position:absolute;">
<asp:HyperLink ID="lnkTitle" runat="server" style="float:left;padding-right:10px;" Text='<%# Container.DataItem%>'/>
<asp:Panel id="pnlEditButtons" runat="server" Visible="false" style="vertical-align:middle;z-index:100;display:none;float:left;" >
<asp:ImageButton ID="imgbtn1" runat="server" ImageUrl="~/Images/misc/edit.png" />
<asp:ImageButton ID="imgbtn2" runat="server" ImageUrl="~/Images/misc/Rename.png" />
</asp:Panel>
</asp:Panel>
</asp:PlaceHolder>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Now I will try to describe the problem:
code-behind:
protected void Page_Load(object sender, EventArgs e)
{
int [] array = {1,2,3,4,5};
rptThreads.DataSource = array;
rptThreads.DataBind();
}
protected void rptThreads_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel editButtonsPanel = e.Item.FindControl("pnlEditButtons") as Panel;
editButtonsPanel.Visible = true;
Panel containerPanel = e.Item.FindControl("titleContainer") as Panel;
//Point of Interest!!!!
containerPanel.Attributes.Add("onmouseover", "ShowEditButtons('" + editButtonsPanel.ClientID + "');");
}
}
If I run the page as is, the generated html will be the following (I show only the first 2 items):
<table cellpadding="0px" cellspacing="0">
<tr style="height:50px">
<td>
<div id="titleContainer" onmouseover="ShowEditButtons('pnlEditButtons');" style="position:absolute;">
<a id="lnkTitle" style="float:left;padding-right:10px;">1</a>
<div id="pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="imgbtn1" id="imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="imgbtn2" id="imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
<tr style="height:50px">
<td>
<div id="titleContainer" onmouseover="ShowEditButtons('pnlEditButtons');" style="position:absolute;">
<a id="lnkTitle" style="float:left;padding-right:10px;">2</a>
<div id="pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="imgbtn1" id="imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="imgbtn2" id="imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
As you can see all divs get the SAME ID, THIS I DONT WANT!!!
But If I omit this line form the ItemCreated event:
containerPanel.Attributes.Add("onmouseover", "ShowEditButtons('" + editButtonsPanel.ClientID + "');");
The generated HTML will be the following:
<table cellpadding="0px" cellspacing="0">
<tr style="height:50px">
<td>
<div id="rptThreads_ctl01_titleContainer" style="position:absolute;">
<a id="rptThreads_ctl01_lnkTitle" style="float:left;padding-right:10px;">1</a>
<div id="rptThreads_ctl01_pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="rptThreads$ctl01$imgbtn1" id="rptThreads_ctl01_imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="rptThreads$ctl01$imgbtn2" id="rptThreads_ctl01_imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
<tr style="height:50px">
<td>
<div id="rptThreads_ctl02_titleContainer" style="position:absolute;">
<a id="rptThreads_ctl02_lnkTitle" style="float:left;padding-right:10px;">2</a>
<div id="rptThreads_ctl02_pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="rptThreads$ctl02$imgbtn1" id="rptThreads_ctl02_imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="rptThreads$ctl02$imgbtn2" id="rptThreads_ctl02_imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
All divs get unique IDs, and this I do want
My questions are:
1)why it happens? why this line of code messup the ids?
2)how can have the unique ID's and assign javascript in codebehind?
I can add this on aspx (it will wotk and I will get unique ids):
onmouseover='<%# "javascript:ShowEditButtons(\""+ Container.FindControl("pnlEditButtons").ClientID+ "\");" %>'
But I must do it in codebehind because I need to set the javascript only if server validate some things.
Well as to why this is happening I'm not real sure. I suspect it may know that you used the ClientID so it didn't change it according to the naming container when the HTML was rendered.
As to what you can do to fix the problem, don't pass the ID to the javascript function. When an event fires in javascript the event object will be passed to the function for Firefox, IE has an explicit windows.event object. The event object will have a reference to the object that fired the event which you can then use to access the ID, but my guess is you were going to use the ID to get a reference to the element anyway.
An odd problem, can't say why this is happening, BUT... try moving this code to ItemDataBound instead of ItemCreated, I think you'll have more luck. I've written code exactly like this, but using OnItemDataBound and not had a problem.
Theoretically, any control inside a NamingContainer should get a unique ID, so there is definitely something fishy going on here.
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();
});