I have troubles with adding blocks with a specific attributes on my asp.net mvc project using jquery.
I need to add block into the page depending on what i have in a dropdownlist(created via c#)
I had created a hidden block in my view(view the code below) wich makes "select" tag with options from my "List"
<div id="other" class="EqItem">
<p>Else: #Html.DropDownList("else", Model.ModEq("",""))</p>
<p>Serial №: #Html.TextBox("serialelse")</p>
</div>
After loading the page i use a function which removes this block from DOM and makes the copy of html code of this block into a variable(blockToAdd )
function BlockDelete()
{
blockToAdd = $("#other").html();
$("#other").remove();
}
then i use this variable to dinamically add blocks into my page(the function below)
function ShowListEtc()
{
for (c = 0; c < 15; c++)
{
if (!$("#divotherlist" + c + "").length) {
id = "divotherlist" + c + "";
clas = "EqItem";
blockToAdd = blockToAdd.trim();
$(".rightside").append('<div id="' + id + '">' + blockToAdd
+ '<p><img id ="' + id + 'D"src="/Images/delete.png"
onclick="Delete(id)"></p></div>');
$("#" + id).attr('class', clas);
$("#"+id).show();
selectid = "othersel" + c;
$("#" + id).find("select").attr('id', selectid);
$('#' + selectid + ' option').each(function () {
this.setAttribute('id', TransL(this.text) + c);
this.setAttribute('onclick', 'MakeNameParent(id)')
});
break;
}
}
}
The point is that i try to change attributes within created block(append(') but they are not applying!
When i made the code first time, it worked, but suddenly it had stopped to work, then i tried to use .prepend instead of .append, it began to work for a while but after reopening the project, it had stopped to work again!
Please tell me what i've done incorrect!
Thank you!
Related
I'm using ASP.NET to make a website for a hotel and at this point to show the hotel rooms I've created a javacript file to generate div's, now I want to get the value of the room number that by clicking "learn more "transfer the value of the number of the room to the other page
I already tried to use cookies but it does not work
here's the js file that generates the div:
$(document).ready(function () {
$.get('http://localhost/quartos.php', function (data) {
var results = JSON.parse(data);
console.log(results);
for (i = 0; i < results.length; i++) {
var div = "<div class='col-sm col-md-6' height='600px' width='400px'><div class='room'><a href='' class='img d-flex justify-content-center align-items-center' style='background-image: url(images/Quartos/" + results[i].imagem + ");'></a><div class='text p-3 text-center><h3 class=' mb-3'><a href=''>Quarto " + results[i].descricao + "</a></h3 > <p><span class='price mr-20'>" + results[i].Preco_quarto + "\u20AC</span><asp:Label ID='Label1' runat='server' Text='ç aop'></asp:Label><span class='per'> por noite</span></p> <ul class='list'><li><span>Max:</span>" + results[i].Lotacao_Maxima + " Pessoas</li><li><span>Vista:</span>" + results[i].Vista + "</li></ul><hr><p class='pt-1'><button class='btn btn-primary' runat='server' onserverclick='btn_quartos'>Ver Detalhes<span class='icon-long-arrow-right'></button></span></p></div></div></div>";
document.cookie = "CookieName=" + results[i].Num_Quarto + ";";
$("#quartos").append(div);
}
});
});
and the cs of the "next" page:
protected void Page_Load(object sender, EventArgs e)
{
string num_quarto = Request.Cookies["CookieName"].Value.ToString();
}
You're overwriting the cookie to something new each time in the for loop, so the cookie is always going to hold the last result in results, despite what they select.
Although, using a cookie to pass info from one page to another isn't an ideal way to do what you want anyway.
A better way would be to make your "learn more" page able to accept a room number as a query string value.
Example: http://localhost/learnmore.php?cuarto=123
Then that looks up the room information and renders the information into the html.
In doing that, you can simplify the link in your div element to point to learmore.php?cuarto="+results[i].Num_Quarto and not have to repurpose the cookie functionality for a behavior it's not really meant for.
I'm trying to make a dynamic javascript from code behind that would be added to the page and would enable thus a little jwplayer to open up, my code so far:
void playAudioFile(string path)
{
string script =
#" var div = document.getElementById('playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);
jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id='" + path +
#"'&date=' + '<%=Encrypt.EncryptString(String.Format(""{0:dd/MM/yyyy}"",
DateTime.UtcNow))%>',
width: ""100%"",
height: ""30"",
type: 'mp3',
stretching: ""uniform"",
controlbar: 'bottom'
});
";
//Add the script to it's tags
HtmlGenericControl playScript = new HtmlGenericControl("script");
playScript.InnerHtml = script;
//Add it on his place
playerDiv.Controls.Add(playScript);
But this turns out to a "could not make contact with localhost:55323 does anyone know what mistake I make? + is there a better way? The purpose would be to display a player if a person clicked on an element on my website, and the players path would change on hand of the selected item. I'm kinda making a system like youtube if you want something to compare with.
Final Solution
string date = Encrypt.EncryptString(String.Format("{0:dd/MM/yyyy}", DateTime.UtcNow));
string script =
#" var div = document.getElementById('ContentPlaceHolder1_playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);
jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id=" + path + #"&date=" + date + #"',
width: '100%',
height: '30',
type: 'mp3',
stretching: 'uniform',
controlbar: 'bottom'
});
";
//Add the script to it's tags
HtmlGenericControl playScript = new HtmlGenericControl("script");
playScript.InnerHtml = script;
//Add it on his place
playerDiv.Controls.Add(playScript);
You should be using ClientScript.RegisterStartupScript to do this:
Page.ClientScript.RegisterStartupScript(this.GetType(), "FooKey", script, true);
You have a code block inside the script, not sure why you don't build that string in code and then append it to the script:
string date = Encrypt.EncryptString(String.Format("{0:dd/MM/yyyy}", DateTime.UtcNow));
string script =
#" var div = document.getElementById('playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);
jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id='" + path + #"'&date=" + date + ", width: '100%', height: '30',type: 'mp3', stretching: 'uniform',controlbar: 'bottom'});";
You can not add scriptlet in the string and assign it to some controls InnerHtml.
I would write javascript directly to aspx (html) side as I belive that is simple and more straight forward.
Change
+ '<%=Encrypt.EncryptString(String.Format(""{0:dd/MM/yyyy}"", DateTime.UtcNow))%>',
To
+ Encrypt.EncryptString(String.Format("{0:dd/MM/yyyy}", DateTime.UtcNow))
C#
HtmlButton btnSave = new HtmlButton();
btnSave.ID = "btnSave" + i.ToString();
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(" + btnSave.ID + ");");
javascript
function save(e)
{
var getId=e.id;
}
I am not getting id in Mozilla firefox,bout its working fine in IE
You can pass the Client ID instead of the ID btnSave.ClientID as ClientID is the perfect choice to deal with such a situation.
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(" + btnSave.ClientID + ");");
function save(e)
{
var getId=e; // Now you have Client ID, you can use directly instead getting through e.id
}
That is because you are using the id as a variable. Some browsers add the id:s as properties in the window object so that you can access them directly, but to make it work in anything but those few browsers you should use the getElementByID method to locate the element.
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(document.getElementByID('" + btnSave.ID + "'));");
I'm using the auto complete control here:http://www.ramirezcobos.com/labs/autocomplete-for-jquery-js/comment-page-2/
And i've modified it as:
var json_options;
json_options = {
script:'ReportSearch.aspx/GetUserList?json=true&limit=6&',
varname:'input',
json:true,
shownoresults:true,
maxresults:16,
callback: function (obj) { $('#json_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};
$('#ctl00_contentModule_txtJQuerySearch').autoComplete(json_options);
I have the following method in C# Code behind (aspx.cs)
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetUserList(string input)
{
List<string> lUsers = new List<string>();
Server.DAL.SQLServer2005.User user = new Server.DAL.SQLServer2005.User();
Server.Info.AuthUser aUser = (Server.Info.AuthUser)HttpContext.Current.Session["AuthUser"];
List<Server.Info.User.UserDetails> users = user.GetUserList(aUser, input, 16, true);
users.ForEach(delegate(ReportBeam.Server.Info.User.UserDetails u)
{
lUsers.Add("(" + u.UserName + ")" + u.LastName + ", " + u.FirstName);
});
return lUsers.ToArray();
}
The error I get back is:
Server Error in '/WebPortal4' Application. Unknown web method
GetUserList. Parameter name: methodName
If I change any of the paraemeter names I get an error telling me the paremeter names are not in match. now that everything is as it should, it's bombing.
Any help would rock.
If your code is in a user control, (and not in the actual aspx), that might cause problems. I guess it shouldn't, but I've had problems with it myself, don't remember exactly how they looked, but in the end I retorted to placing my web methods in asmx files instead of aspx files, if they are to be reached from anything but the aspx itself, and it's been working out great =)
I would like to open a popup window using javascript in my c#.net app. This is the code in the body tag in my webform
<script language=javascript>
function openWindow(strEmail)
{
window.open('CheckEmail.aspx?email=' + strEmail + , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
return false;
}
</script>
this is my code in the Page_Load section
this.btnCheck.Attributes.Add("onclick", "return openWindow(" + txtEmail.Text + ");");
right now I'm trying to pass the string from my textbox "txtEmail" so in my popup window i can get the request.querystring but Im a little unsure of how the syntax is.
No need of the last +
window.open('CheckEmail.aspx?email=' + strEmail,'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
and in CheckEmail.aspx page you can get the query string as
Request.QueryString["email"]
Use a ' in the CS side inside the function around the textEmail.Text
this.btnCheck.Attributes.Add("onclick", "return openWindow('" + txtEmail.Text + "');");
Why don't you get the email in the client code if the txtEmail control is visible.
function openWindow()
{
var email = document.getElementById('<%=txtEmail.ClientID%>').value;
window.open('CheckEmail.aspx?email=' + email + , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
return false;
}