I read data from database in sreverside (for example a name) and I want to use this name in clientside. But I don't pass the name to clientside(in javascript method). How can I achieve this?
(using asp.net)
using HiddenField:
serverside:
protected void callbackPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
hiddenFieldGenel.Add("license", "license1");
}
clientside:
<dx:ASPxHiddenField ID="hiddenFieldGenel" runat="server" ClientInstanceName="hiddenFieldGenel">
</dx:ASPxHiddenField>
function saveClick(s, e) {
var licence = hiddenFieldGenel.Get('license');
}
If its just a single value, you can use Hidden variable:-
<asp:HiddenField ID="NameHidden" runat="server" ClientIDMode="Static" />
And you can simply access it using Javascript or jQuery:-
$('#NameHidden').val();
document.getElementById('NameHidden');
Related
In my web app I have a series of ImageButtons. Upon clicking any of these ImageButtons, the ImageButton that was clicked needs to have it's image changed.
I would like one OnClick method that performs its action on the ImageButton that was clicked. Here is my attempt so far:
ASP.NET Code:
<asp:ImageButton ID="ImageToChange1" runat="server" OnClick="ChangeImage" CommandArgument="ImageToChange1"/>
<asp:ImageButton ID="ImageToChange2" runat="server" OnClick="ChangeImage" CommandArgument="ImageToChange2"/>
<asp:ImageButton ID="ImageToChange3" runat="server" OnClick="ChangeImage" CommandArgument ="ImageToChange3"/>
For my C# Code I want to do something like the following:
public void ChangeImage(object sender, CommandEventArgs e)
{
e.CommandArgument.ImageUrl = "~/Images/Penguins.jpg";
}
I'm wondering if there is a more appropriate way to do this than using the "OnClick" event or if I can accomplish this by manipulating "e" or "sender" somehow. Any insight would be greatly appreciated. Thank you.
In your ChangeImage, you can get the CommandArgument and the control as below
public void ChangeImage(object sender, CommandEventArgs e)
{
ImageButton imageButton = sender as ImageButton;
string imageToChange = e.CommandArgument;
//Then you can assign the appropreate image
imageButton.ImageUrl = "~/Images/Penguins.jpg";
}
I have the following code which defined my input field and my button:
<p>
<asp:TextBox ID="input" runat="server"></asp:TextBox>
<asp:Button ID="submit" runat="server" OnClick="submit_Click" Text="Button" />
</p>
<p>
<asp:GridView ID="searchDisplay" runat="server" OnSelectedIndexChanged="searchDisplay_SelectedIndexChanged">
</asp:GridView>
</p>
I added the a Click event to my Button once pressed it should invoke the following method:
protected void submit_Click(object sender, EventArgs e)
{
String keyword = input.Text; // Gets text inputed
List<T> = service.getTitles(keyword); // Two problems here
}
I have 2 problems i need to solve:
1) List should be defined as List<Publication> however type Publication is not part of this solution so i dont know how to correctly bind the data that WCF service returns to it. I tried E, T, and ? as parameters to List but all i get are errors.
Solved
2) I have a WCF service runing in another VS2012 instance how and in the first one i have it refrenced with keyword "service" why can't i access it's methods. I have a method in my WCF service that returns List<Publication> i tried writing List<?> data = service.getTitles(keyword); however this gives an error that the following is a type not valid in current context
you can try the following
protected void submit_Click(object sender, EventArgs e)
{
String keyword = input.Text; // Gets text inputed
var v= service.getTitles(keyword);
grd.dataSource=v;
grd.dataBind();
}
I am actually buidling a web application and I need to get data of the local environment of the machine viewing the application.To do this, I use a little javascript script such as:
<script language="javascript">
function GetUserName()
{
//Permits to get username of client machine
var wshell = new ActiveXObject("WScript.Shell");
var arpege = wshell.ExpandEnvironmentStrings("%USERNAME%");
document.getElementById("arpege").value=arpege;
}
</script>
<input type=hidden id="arpege" runat=server />
My problem is that this script is executed after the page is loaded and so I can't use it on the page load...
My code on the page load is:
protected void Page_Load(object sender, EventArgs e)
{
myConnection.ConnectionString = ActionSource.ConnectionString;
myConnection.Open();
String account = arpege.value;
...
}
But i just get "" in account...
Thanks for your help,
Quentin
Page_Load occurs on the server side before any data is being sent to the client.
you can't do that without initiating a new postback once the page is loaded.
Solution coming up...
EDIT:
So, it (might) be possible to achieve this using some jQuery
$(document).ready(function() {
var wshell = new ActiveXObject("WScript.Shell");
var arpege = wshell.ExpandEnvironmentStrings("%USERNAME%");
__doPostBack('__Page', arpege);
});
And catch it on the server side:
public void Page_Load(object sender, EventArgs e)
{
string arpege= Request["__EVENTARGUMENT"];
}
Worth's a shot!
A variant of the proposition made by Shai could be the following:
generate an asyncpostback on a hidden button.
Like that, the logic could be separated from the Page_Load, and
put instead in the handler attached to the input which generated the asyncpostback.
I don't like to put too much logic in the page_load event, i find it confusing.
the implementation will be a little more complicated though
Get the username server side in Page_Load instead of with an ActiveX control.
protected void Page_Load(object sender, EventArgs e)
{
myConnection.ConnectionString = ActionSource.ConnectionString;
myConnection.Open();
String account = User.Identity.Name;
// ...
}
Also, ActiveX only works in Internet Explorer - User.Identity.Name works on the server and therefor all browsers.
The ASPX code:
<body>
<form id="form1" runat="server">
<input type="hidden" id="hdnUser" runat="server" />
<input type="hidden" id="hdnRun" runat="server" value="true" />
<asp:Label ID="lblSayHello" runat="server" Text="I dont know you"></asp:Label>
</form>
<script type="text/javascript">
$(function () {
var run = $('#hdnRun').val();
if (run == 'true') {
var wshell = new ActiveXObject("WScript.Shell");
$('#hdnUser').val(wshell.ExpandEnvironmentStrings("%USERNAME%"));
$('#form1').submit();
}
});
</script>
</body>
The Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(hdnUser.Value))
{
hdnRun.Value = "false";
lblSayHello.Text = String.Format(#"Hello, {0}", hdnUser.Value);
}
}
The best (easiest) way that I know of sending data from the client to the server is to
1. Store it in an asp hidden field
2. Click an ASP button via javascript
3. Handle the data in the OnClick Event
For example:
ASPX Page
<asp:HiddenField ID="myHf" class="myHf" runat="server" />
<asp:Button ID="myButton" class="myButton" runat="server" OnClick="myButton_Click" style="display: none;" />
Javascript
function SendMyData(data) {
document.getElementByClassName('myHf').value = data;
document.getElementByClassName('myButton').click();
//I use classes since those won't change - IDs become really long and messed up
//in the actual page, and may change depending on where the element is placed.
}
CodeBehind
protected void myButton_Click(object sender, EventArgs e)
{
//Do whatever you want with myHf.Value
MyServerMethod(myHf.Value);
}
I think that doing it this way is better than using the Page_Load and submitting the form, just because if you are doing enough of these, the Page_Load method can get huge. But that's just my opinion.
i have this button on my C# context:
<button id='Update' OnServerClick='UpdateCart'>Update</button>
now I wanna to pass parameter (num) to (UpdateCart) Method :
protected void UpdateCart(object sender, EventArgs e)
{
Response.Cookies["Cart"]["Qty(" + num + ")"] = Request.Form["Qty" + num];
}
How I can do that ?
Use ASP.NET Button control instead of <button/> markup that allow you to set value via CommandArgument property and use Command event (do not use Click) to retrieve value of CommandArgument property.
Markup:
<asp:Button
ID="Button1"
runat="server"
CommandArgument="10"
oncommand="Button1_Command"
Text="Button" />
Code:
protected void Button1_Command(object sender, CommandEventArgs e)
{
string value = e.CommandArgument.ToString();
}
You would use the 'commandArgument' attribute. Here's an example on the MSDN
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
I noticed that command argument does not accept dynamically set values from client side, command argument must be set server side. So to combat this I used asp:hidden field updated its value and then from code behind got the value by just doing hiddenfield.value.
I've done this with a asp.net Gridview. but does anybody have any examples on how to pass row information from an asp.net button in SharePoint Dataview rows to an inline C# function?
the button Repeats as follows:
<asp:Button runat="server" Text="Delete" id="Delete{#ID}" OnClick="DeleteDoc()"/>
My function looks like this:
void DeleteDoc(object sender, EventArgs e) { }
}
I tried adding another parameter but get:
No overload for 'DeleteDoc' matches delegate 'System.EventHandler'
Try to change your method access modifier to protected and OnClick="DeleteDoc()" to OnClick="DeleteDoc".
Also i didn't managed to make it work with id="Delete{#ID}" so try without that to.
This worked on my test case
<asp:Button runat="server" Text="Delete"
OnClick="DeleteDoc"
CommandArgument='<%# Eval("ID")%>'/>
and
protected void DeleteDoc(object sender, EventArgs e)
{
Button b = sender as Button;
YourDeleteDocumentHelperMethod(b.CommandArgument);
}