jquery append question - c#

In my ASP.net project, I am using a jquery script to update the id=person field in the URL bar:
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
This is triggered when the value of the drop-down box changes. It works great, except for the fact that when you change it...it just appends the new id behind the first id, which is exactly what it's supposed to be doing.
However, I only want one id, obviously. What can I do here to accomplish this? Is there a way to CLEAR that first, and then append it?
Thanks!

Check to see if it exists, if so, set the value. If it doesn't append it:
var input = $('input[name=hiddenEmployeeId]');
if(input [0]) {
input.val($('#EmployeeSelected').val());
} else {
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
}

Cant you just change the value on the input named 'hiddenEmployeeId' or does it not exists the first time?
$("input[name=hiddenEmployeeId]").val($('#EmployeeSelected').val());
you can do something like this:
$($('input[name=hiddenEmployeeId]')[0] || $('<input type="hidden" name="hiddenEmployeeId">').appendTo("form.selector")[0]).val($('#EmployeeSelected').val());
and change the "form.selector" to the form you are working with.

Related

How to pass a variable from .cs to .aspx

As the title suggests, I want to add something to a table (inside aspx page) depending on some infos I get from a database (so if I have an item x in db I will make a bool type variable in cs).
My question is, is there any method to send that bool to aspx so I can use it inside an if statement (in order to add the respective columns to my table).
You don't need a session variable here, you can simply declare a class level property and use it in your ASPX page:-
public bool variable { get; set; }
and then directly use it in your aspx page:-
<%=variable %>
You can store the value of bool in session and display it in aspx, something like that
bool val;
// Your code
Session["value"] = val;
and use it in aspx such as
<%= Session["value"]%>
You can use a div and make your own HTML code in the .cs code behind file.
Or, you could simply use some labels and give information like that.
html:
<div>
<table>
<tr>
<td><asp:Label id="somelabel" runat="server" /></td>
</tr>
</table>
.cs
somelabel.Text = "yourdata";
Or you could add all your html code to a single string. Then, if your div is called divTable:
divTable.InnerHtml = liststring;
An example:
liststring = liststring +
"<img src=" + SQLdata.Tables[0].Rows[x]["imageSource"].ToString() +
"<ul style='list-style-type: none;'>" +
"<li>" + SQLdata.Tables[0].Rows[x]["Name"].ToString() + "</li>" +
"<li><b>Adress: </b>" + SQLdata.Tables[0].Rows[x]["Adress"].ToString() + "</li>" +
"<li><b>Website: </b><a href='http://" + SQLdata.Tables[0].Rows[x]["Website"].ToString() + "'>" + SQLdata.Tables[0].Rows[x]["Website"].ToString() + "</a></li>" + "</ul></div>";
divTable.InnerHtml = liststring;
Ofcourse you have to css a bit to get the correct display, but this will get you going I guess...

Linq to strikeout Telerik datagrid row

I am using asp.net 3.5 with c#. I am getting records in dataset that contains a field with name IsDeleted. Now for this field, if it is set to true then I want to strikeout the entire row's text.
For example : I am making Capital Case the text of field Alert using linq code below :
ds.Tables[0].AsEnumerable().ToList().ForEach(i => i["Alert"] = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(i["Alert"].ToString()));
Something like this is possible to strikeout the entire row's text?
Thanks
Obviously TextInfo doesn't have any method to do such modification, since it's applied in HTML only.
You can however add the HTML text-decoration to every cell.
Try this:
ds.Tables[0].AsEnumerable().ToList()
.Where(i=>i["IsDeleted"].ToString().Equals("True"))
.ForEach(i =>
{
i["Alert"] = "<span style='text-decoration:line-through;'>" +
i["Alert"].ToString() +
"</span>";
i["Name"] = "<span style='text-decoration:line-through;'>" +
i["Name"].ToString() +
"</span>";
i["Date"] = "<span style='text-decoration:line-through;'>" +
i["Date"].ToString() +
"</span>";
});

ViewModel contains list which is not bound when form is posted

There is a property called List<AssignablePolicy> AssignablePolicies in my ViewModel. I want to create items that map to this list using Jquery. So I use this code:
$('#frmPolicy').append('<input type="hidden" name="AssignablePolicies[].CompanyName" value="' + company + '"/>');
$('#frmPolicy').append('<input type="hidden" name="AssignablePolicies[].PolicyNumber" value="' + policyNumber + '"/>');
$('#frmPolicy').append('<input type="hidden" name="AssignablePolicies[].Amount" value="' + amount + '"/>');
The AssignablePolicy contains CompanyName, PolicyNumber and Amount. However when I post the form I get null in AssignablePolicies in my viewmodel.
But when I do
Request.Form["AssignablePolicies[].CompanyName"]
the hidden form values are available. Any idea why is this happening?
When using a compound property, you need to specify the actual index of each element. When posted back these need to be consecutive (or you need to include an .Index element). Here's some javascript adapted from a similar answer that will fix up the properties on post. It assumes the first input in each set has the class policy-start.
$('form').submit( function() {
$('.policy-start').each( function(idx) {
var prefix = 'AssignablePolicies[' + idx + '].';
$(this).attr('name',prefix + 'CompanyName')
.next(':hidden')
.attr('name',prefix + 'PolicyNumber' )
.next(':hidden')
.attr('name',prefix + 'Amount' );
});
return true;
});
I have a problem like this one.
Model class with a collection. The Request contains the keys but the collection was null. Long story short, I added a ICollection parameter in my controller to let the mvc bind the data.
See this walkaround, maybe it helps you: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx. It works for me. But, i'm a newbie.

How do I toggle the display for a dynamic id table?

I have a table that displays rows based on a drop down list option. The rows are dynamically created. I use the value on the ddl for the initial row, then a simple counter to make it unique. Is there a way to say something like show all the rows that start with "tableShoes" even though the rows are something like tableShoes1, tableShoes2, etc?
$('#itemSelect').change(function() {
var item = $('#itemSelect').val();
$('#itemList tr').each(function() {
$(this).hide();
});
$('#' + item + 'Header').show();
$('#' + item + '1').show();
//Because the table is dynamic, I am not sure how many
//rows would be available so I only see the first option.
});
I would suggest adding the same CSS class name to each row as it is added, perhaps the same as the value of the selected item. That way your last line could just be:
$('.' + item).show();
And you wouldn't have to worry about the index.
this selector shoud solve your problem: $('[id|=tableShoes]')

Jquery and ASCX control rendered multiple times on a page

I have an ascx control which contains dropdownboxes I want to be able to reset with JavaScript.
Because the ascx control is rendered multiple times on the aspx page, I programatically add a distinguishing field to each dropdown such as this in the code behind of the ascx:
var g = Guid.NewGuid().ToString().Replace("-", "");
DropDownListBool.Attributes.Add("jqID", "ddBool" + g);
DropDownListEqual.Attributes.Add("jqID", "ddEq" + g);
On the rendered page, when I want to reset the dropdowns for one of the controls, I have a hyperlink which invokes a javascript function with g as an argument.
In the javascript, using jquery, I try to get both dropdowns for one specific ascx control like this:
function clearControl(g) {
var dds = $("select[jqID = 'dd\\S*" + g + "']");
}
I then do:
jQuery.each(dds, function(i, val) { val.select = 0; });
Should this work? Right now it is resetting seemingly random dropdown boxes.
Is there perhaps a limit to attribute length?
I think you might have better luck with a different selector, say the "ends with" attribute selector.
var dds = $("select[jqID$='" + g + "']");
If you needed to select based on starting with dd and ending with the value of g, you could use a filter and utilize both "ends with" and "starts with";
var dds = $("select[jqID^='dd']").filter( "[jqID$='" + g + '']");
As far as I know you can't use a regular expression when using the attribute equals selector. I'm surprised it works for you at all.

Categories

Resources