Access data-href in backend code in c# - c#

How to change the value of data-href in dotnet code behind Approach. Please i want to write different value in datahref attribute using query string
<div class="fb-comments" id="abc" data-href="https://www.facebook.com/Khaanaabaadosh" data-numposts="5" data-colorscheme="light" runat="server"></div>

Is this what you are looking for?
In your C# code, you should be able to do
abc.Attributes["data-href"] = "your custom link";

Related

Get user-selected value from HTML5 dropdown list using C#

I want my C# code to get the currently-selected option from an HTML5 dropdown list. Our situation is perhaps a bit different from many others, since this is not a client-server web app, but rather a desktop app which uses HTML for its UI in order to be more portable to Linux. To support this, we use an embedded Firefox browser with the Gecko engine.
I'm new to working with HTML using C#, and I can't seem to find the right API call for this.
The HTML looks like this:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
And the C# code I'm using to try to access it is:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.Items[selectElement.SelectedIndex].Value;
string text = selectElement.Items[selectElement.SelectedIndex].Text;
This is based on examples I have seen from other posts, but those posts are using asp.net and in my case Items and SelectedIndex are not defined for selectElement.
Also, I have seen some posts about using Javascript, but I would prefer to keep this all in C#, if possible.
I looked at what it would take to have a callback method on the dropdown, so I could get the selected value whenever it changed and save it to an instance variable in my class, but I wasn't able to figure out how to specify the callback method, either.
Any help would be highly appreciated!
Since there were no answers to my question, I went ahead with a JavaScript solution:
function updateDDLSelection(elid) {
var el = document.getElementById(""ddlMyOptions"");
var val = el.options[el.selectedIndex].value;
el.setAttribute('value', val);
}
Then, I had to change the HTML from what was shown above to this:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions" onchange=""updateDDLSelection(event)"">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
And here's the revised C# code to get the value that was set by the JavaScript function:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.GetAttribute("value", 0).ToString();
Hope this is helpful to someone else who is in a similar situation...

Best way to create and post a form in codebehind in C#

I'm using webforms and I need to create and POST an http form to a payment gateway for one of my clients. I'm using a user control that is displayed in a masterpage, however they already have an existing <form> on the page, so I'm pretty sure I cannot add another.
What is the best practice method to create and post a form in C# from the code behind? Currently I have the following:
var nvc = new System.Collections.Specialized.NameValueCollection
{
{"EWAY_ACCESSCODE", ewayResponse.AccessCode},
{"EWAY_CARDNAME", txtCcName.Text},
{"EWAY_CARDNUMBER", txtCcNumber.Text},
{"EWAY_CARDEXPIRYMONTH", ddlCcExpMonth.SelectedValue},
{"EWAY_CARDEXPIRYYEAR", ddlCcExpYear.SelectedValue},
{"EWAY_CARDCVN", txtCcv.Text}
};
var client = new WebClient();
client.UploadValues(ewayResponse.FormActionURL, nvc);
This does work, but is there a better / alternative way to do it?
Aside from the previous suggestion:
why wouldn't a (simpler) Button.PostbackUrl work?
<asp:Button ID="externalPost" runat="server" PostBackUrl="http://www.somewhere.com/" Text="Post Somewhere" />
Also, assuming that's a payment gateway you're referring to, you're doing a server-side POST, which has implications on PCI Compliance (vs. a direct form post from client/browser).
Hth.
Here is the solution I use:
http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html
Caveat (own blog post):
http://codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx

save a certain number into session on href in ASP.Net, C#

I already found this on this website.
how to pass session variable in href
But what I want is vice versa. and I can't get it done..
I need some correct syntax..
is it ......
..../home.aspx?<%Session["egSession"]=1%>
or
..../home.aspx?=<%Session["egSession"]=1%>
or
..../home.aspx?<%=Session["egSession"]=1%>
But i believe all of the above are wrong.. coz none of them are working..
Tkz..
Session.Add("egSession", 1) will add 1 to the session cookie egSession.
You could also probably be sure it doesn't already exist by doing so:
Session.Remove("egSession");
Session.Add("egSession", 1);
To get the querystring value from the address you would (code behind do)
var value = Request["egSession"];
So that means you could do:
Session.Remove("egSession");
Session.Add("egSession", Request["egSession"]);
Hope that helps!
** UPDATE **
If you can't touch the .cs files, you can do this in the ASPX-file, by wrapping your code in <% ... code goes here ... %>
if the new session variable value is know, e.g. 1 in your sample setting it can be done anywhere
<% Session["egSession"]=1; %>
if you want to pass it through as a query parameter do this:
..../home.aspx?egSession=<%=Session["egSession"]%>
The point is, you need a name for the value, i.e. egSession but you may call it what ever you want.
However, if you alldready know the value you can simply do:
..../home.aspx?egSession=1
From what I am understanding you want something like this:
APage
Take Me home
Home.aspx.cs: the code behind page, in say the OnPageLoad Event
Session["egSession"] = Request.QueryString["egSession"];
Home.aspx
<div>Session: <% =Session["egSession"] %></div>
<div>Query String: <% = Request.QueryString["egSession"] %></div>
If you are trying to to it all in one I would try the following:
APage.aspx.cs
Create a public method (change the type of the input parameter if needed)
public string SessionMagic(object input)
{
Session["egSession"] = input;
return Session["egSession"].ToString();
}
APage.aspx
A Link
*UPDATE: *
If you can not update the .cs files you can add server side code in the aspx page, not great practice but it can be done. Encapsulate the code in script tags with the run a server attribute set. E.g.:
<script runat="server">
public string ServerSideFunction(string input)
{
Session["egSession"] = Request.QueryString["egSession"];
public string SessionMagic(object input)
{
Session["egSession"] = input;
return Session["egSession"].ToString();
}
}
</script>
you can use javascript with cookie to store the value that you want to set into Session.
eg:- url- url.apsx/egValue=1 read this url using javascript. Put the key and value into cookie.
Then you can read cookie value from server and put it into session.
hope it helps.

Explicitly define control prefix in ASP.NET 1.1?

I've got some (badly written) legacy code that I have to modify but I'm having an odd problem with the prefixing of the controls.
The main page has several include files, including cpct_cost.aspx.
Within that included page is a control EquipmentCost:
<INPUT id="EquipmentCost" type="text" name="EquipmentCost" runat="server" size="10" onchange="TotalCost();" class="bk">
Another page references that control in client side javascript:
function TotalCost()
{
var a = document.CPCT_FORM.cpct_cost_EquipmentCost.value;
This is throwing a client error because when I run the code locally, the control is being named with a $ instead of an _:
<input name="cpct_cost$EquipmentCost" type="text" id="cpct_cost_EquipmentCost" size="10" onchange="TotalCost();" class="bk" />
And as a result, the client side javascript can't find the element in question.
The legacy code on the server handles this differently - the control name there is processed as:
<input name="cpct_cost:EquipmentCost" id="cpct_cost_EquipmentCost" type="text" size="10" onchange="TotalCost();" class="bk" />
and this is being processed correctly.
I thought at first it might be a local IIS setting, so I compiled my code and deployed it to the dev server, but the behavior was identical to my local execution.
I'm using what are supposed to be the latest source files, I haven't changed any project settings, so ... is there some way I can force the code from my machine to use the : instead of $? Or ... what am I missing?
The project is currently on the proposal list to be completely rearchitected, (so please, no suggestions to redesign the app ;) ) but in the mean time, I have a short term requirement to implement some minor new functionality in this ugly old beast, and I've got to get it done ASAP. What's frustrating is - I haven't changed these parts of the code at all, but the behavior is changing anyway.
UPDATE
Apparently the naming prefix used in at least .NET 1.1 is somewhat random, because after several builds, while I was trying various ways to work around this, the controls ended up getting the right name. So now I'm just not changing the code, which sucks because I really hate taking this "freeze it while it's randomly right" approach.
You could pass in a reference to the input control as a parameter to the JS function, ie:
<INPUT id="EquipmentCost" type="text" name="EquipmentCost" runat="server" size="10" onchange="TotalCost(this);" class="bk">
function TotalCost(txtEquipCost) {
var a = txtEquipCost.value;
}
Then it doesn't matter what the id is.
EDIT:
If you have more controls, create JS variables on the page, eg:
var txtEquipCost = document.getElementById('<%= YourControl.ClientID %>');
var txtOtherCost = document.getElementById('<%= YourOtherControl.ClientID %>');
Then the onChange function call could be TotalCost(txtEquipCost, txtOtherCost)
EDIT2:
See this question about ClientId and UniqueId which may be useful:
C# asp.net Why is there a difference between ClientID and UniqueID?
You could change your Javascript to use the id that is getting generated.
function TotalCost()
{
var a = document.getElementById('<%= YourControl.ClientID %>').value;
}
Also if you need absolute control over the generated id of that control it turns out that in asp.net 4.0 the ClientIDMode property was introduced so that developers have more control over how that id is generated.
Check out these two sources
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

JavaScript Error: conditional compilation is turned off in MVC2 View

I am trying to call a JavaScript function on click in a MVC2 View Page.
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>,<%=o.ba_Object_Code %>)" href="#">Select</a>
JavaScript function
function SelectBenefit(id,code) {
alert(id);
alert(code);
}
Here ba_Object_Id and Code are the values from the ViewModel. If I use SelectBenefit(<%=o.ba_Object_id %>) in this way, its working fine. But when I have two paramaters its not.I am getting this error:
conditional compilation is turned off.
I think that you need to put quotes around the second parameter if it is a string:
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>, '<%=o.ba_Object_Code %>')" href="#">Select</a>
This being said your parameters need to be properly encoded and I wouldn't pass them likse this. I would serialize them as JSON object to ensure that everything is OK. Like this:
<a onclick="SelectBenefit(<%= new JavaScriptSerializer().Serialize(new { id = o.ba_Object_id, code = o.ba_Object_Code }) %>)" href="#">Select</a>
and then the SelectBenefit function might look like this:
function SelectBenefit(benefit) {
alert(benefit.id);
alert(benefit.code);
}
I'm guessing o.ba_Object_Code is not a number? Try putting quotes around it:
<a onclick="SelectBenefit(<%=o.ba_Object_id %>,'<%=o.ba_Object_Code %>')" href="#">Select</a>
You could also write this function like this:
Select
Or use Jquery (best approach, imo):
$('#yourlinkid').click(function(){
SelectBenefit(<%=o.ba_Object_id %>,'<%=o.ba_Object_Code %>');
return false;
});

Categories